options.h 2.7 KB

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