options.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. pybind11/options.h: global settings that are configurable at runtime.
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #pragma once
  9. #include "detail/common.h"
  10. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  11. class options {
  12. public:
  13. // Default RAII constructor, which leaves settings as they currently are.
  14. options() : previous_state(global_state()) {}
  15. // Class is non-copyable.
  16. options(const options &) = delete;
  17. options &operator=(const options &) = delete;
  18. // Destructor, which restores settings that were in effect before.
  19. ~options() { global_state() = previous_state; }
  20. // Setter methods (affect the global state):
  21. options &disable_user_defined_docstrings() & {
  22. global_state().show_user_defined_docstrings = false;
  23. return *this;
  24. }
  25. options &enable_user_defined_docstrings() & {
  26. global_state().show_user_defined_docstrings = true;
  27. return *this;
  28. }
  29. options &disable_function_signatures() & {
  30. global_state().show_function_signatures = false;
  31. return *this;
  32. }
  33. options &enable_function_signatures() & {
  34. global_state().show_function_signatures = true;
  35. return *this;
  36. }
  37. options &disable_enum_members_docstring() & {
  38. global_state().show_enum_members_docstring = false;
  39. return *this;
  40. }
  41. options &enable_enum_members_docstring() & {
  42. global_state().show_enum_members_docstring = true;
  43. return *this;
  44. }
  45. // Getter methods (return the global state):
  46. static bool show_user_defined_docstrings() {
  47. return global_state().show_user_defined_docstrings;
  48. }
  49. static bool show_function_signatures() { return global_state().show_function_signatures; }
  50. static bool show_enum_members_docstring() {
  51. return global_state().show_enum_members_docstring;
  52. }
  53. // This type is not meant to be allocated on the heap.
  54. void *operator new(size_t) = delete;
  55. private:
  56. struct state {
  57. bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
  58. bool show_function_signatures = true; //< Include auto-generated function signatures
  59. // in docstrings.
  60. bool show_enum_members_docstring = true; //< Include auto-generated member list in enum
  61. // docstrings.
  62. };
  63. static state &global_state() {
  64. static state instance;
  65. return instance;
  66. }
  67. state previous_state;
  68. };
  69. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
  70. #else
  71. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  72. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)