• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)14 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
15 
16 class options {
17 public:
18 
19     // Default RAII constructor, which leaves settings as they currently are.
20     options() : previous_state(global_state()) {}
21 
22     // Class is non-copyable.
23     options(const options&) = delete;
24     options& operator=(const options&) = delete;
25 
26     // Destructor, which restores settings that were in effect before.
27     ~options() {
28         global_state() = previous_state;
29     }
30 
31     // Setter methods (affect the global state):
32 
33     options& disable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = false; return *this; }
34 
35     options& enable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = true; return *this; }
36 
37     options& disable_function_signatures() & { global_state().show_function_signatures = false; return *this; }
38 
39     options& enable_function_signatures() & { global_state().show_function_signatures = true; return *this; }
40 
41     // Getter methods (return the global state):
42 
43     static bool show_user_defined_docstrings() { return global_state().show_user_defined_docstrings; }
44 
45     static bool show_function_signatures() { return global_state().show_function_signatures; }
46 
47     // This type is not meant to be allocated on the heap.
48     void* operator new(size_t) = delete;
49 
50 private:
51 
52     struct state {
53         bool show_user_defined_docstrings = true;  //< Include user-supplied texts in docstrings.
54         bool show_function_signatures = true;      //< Include auto-generated function signatures in docstrings.
55     };
56 
57     static state &global_state() {
58         static state instance;
59         return instance;
60     }
61 
62     state previous_state;
63 };
64 
65 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
66