File indexing completed on 2024-06-02 05:48:37

0001 /*
0002     pybind11/options.h: global settings that are configurable at runtime.
0003 
0004     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #pragma once
0011 
0012 #include "detail/common.h"
0013 
0014 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0015 
0016 class options {
0017 public:
0018 
0019     // Default RAII constructor, which leaves settings as they currently are.
0020     options() : previous_state(global_state()) {}
0021 
0022     // Class is non-copyable.
0023     options(const options&) = delete;
0024     options& operator=(const options&) = delete;
0025 
0026     // Destructor, which restores settings that were in effect before.
0027     ~options() {
0028         global_state() = previous_state;
0029     }
0030 
0031     // Setter methods (affect the global state):
0032 
0033     options& disable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = false; return *this; }
0034 
0035     options& enable_user_defined_docstrings() & { global_state().show_user_defined_docstrings = true; return *this; }
0036 
0037     options& disable_function_signatures() & { global_state().show_function_signatures = false; return *this; }
0038 
0039     options& enable_function_signatures() & { global_state().show_function_signatures = true; return *this; }
0040 
0041     // Getter methods (return the global state):
0042 
0043     static bool show_user_defined_docstrings() { return global_state().show_user_defined_docstrings; }
0044 
0045     static bool show_function_signatures() { return global_state().show_function_signatures; }
0046 
0047     // This type is not meant to be allocated on the heap.
0048     void* operator new(size_t) = delete;
0049 
0050 private:
0051 
0052     struct state {
0053         bool show_user_defined_docstrings = true;  //< Include user-supplied texts in docstrings.
0054         bool show_function_signatures = true;      //< Include auto-generated function signatures in docstrings.
0055     };
0056 
0057     static state &global_state() {
0058         static state instance;
0059         return instance;
0060     }
0061 
0062     state previous_state;
0063 };
0064 
0065 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)