options.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Getter methods (return the global state):
  37. static bool show_user_defined_docstrings() {
  38. return global_state().show_user_defined_docstrings;
  39. }
  40. static bool show_function_signatures() { return global_state().show_function_signatures; }
  41. // This type is not meant to be allocated on the heap.
  42. void *operator new(size_t) = delete;
  43. private:
  44. struct state {
  45. bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
  46. bool show_function_signatures = true; //< Include auto-generated function signatures
  47. // in docstrings.
  48. };
  49. static state &global_state() {
  50. static state instance;
  51. return instance;
  52. }
  53. state previous_state;
  54. };
  55. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)