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