You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

8 years ago
  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 "common.h"
  9. NAMESPACE_BEGIN(pybind11)
  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() {
  19. global_state() = previous_state;
  20. }
  21. // Setter methods (affect the global state):
  22. options& disable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = false; return *this; }
  23. options& enable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = true; return *this; }
  24. options& disable_function_signatures() & { global_state().show_function_signatures = false; return *this; }
  25. options& enable_function_signatures() & { global_state().show_function_signatures = true; return *this; }
  26. // Getter methods (return the global state):
  27. static bool show_user_defined_docstrings() { return global_state().show_user_defined_docstrings; }
  28. static bool show_function_signatures() { return global_state().show_function_signatures; }
  29. // This type is not meant to be allocated on the heap.
  30. void* operator new(size_t) = delete;
  31. private:
  32. struct state {
  33. bool show_user_defined_docstrings = true; //< Include user-supplied texts in docstrings.
  34. bool show_function_signatures = true; //< Include auto-generated function signatures in docstrings.
  35. };
  36. static state &global_state() {
  37. static state instance;
  38. return instance;
  39. }
  40. state previous_state;
  41. };
  42. NAMESPACE_END(pybind11)