gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
PythonSingleton.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2017, Dr Franck P. Vidal (franck.p.vidal@fpvidal.net),
4 http://www.fpvidal.net/
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation and/or
15 other materials provided with the distribution.
16 
17 3. Neither the name of the Bangor University nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 */
33 
34 
35 #ifndef __PythonSingleton_h
36 #define __PythonSingleton_h
37 
38 
66 //******************************************************************************
67 // Include
68 //******************************************************************************
69 #ifndef __gVirtualXRayConfig_h
71 #endif
72 
73 #include <string>
74 #include <memory>
75 
76 #ifdef HAS_PythonLibs
77 #include <Python.h>
78 #endif
79 
80 
81 //******************************************************************************
82 // namespace
83 //******************************************************************************
84 namespace gVirtualXRay {
85 
86 
88 public:
90  {
91  if (!m_p_instance)
92  {
93  // std::make_unique will not work
94  m_p_instance = std::unique_ptr<PythonSingleton>(new PythonSingleton());
95  m_p_instance->initPython();
96  }
97  return *m_p_instance.get();
98  }
99 
100  bool hasSpekpy();
101  bool hasXpecgen();
102 
103 #ifdef HAS_PythonLibs
104  PyObject* getModule();
105 #endif
106 
108 
109 private:
110  PythonSingleton();
111 
112  void initPython();
113  void releasePython();
114 
115 
116  PythonSingleton(const PythonSingleton&)= delete;
117  PythonSingleton& operator=(const PythonSingleton&)= delete;
118 
119  static std::unique_ptr<PythonSingleton> m_p_instance;
120  bool m_initialised;
121  bool m_has_spekpy;
122  bool m_has_xpecgen;
123  std::string m_python_interpretor;
124 
125 #ifdef HAS_PythonLibs
126  PyObject* m_p_global;
127  PyObject* m_p_module;
128  PyObject* m_p_local;
129 #endif
130 
131 };
132 
133 
134 } // namespace gVirtualXRay
135 
136 
137 //******************************************************************************
138 #include "PythonSingleton.inl"
139 
140 
141 #endif // __PythonSingleton_h
static PythonSingleton & getInstance()
Singleton to handle the initialisation and release of Python.