Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Types.hh
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19// Tasking native types
20//
21
22#pragma once
23
24#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
25// Disable warning C4786 on WIN32 architectures:
26// identifier was truncated to '255' characters
27// in the debug information
28//
29# pragma warning(disable : 4786)
30//
31// Define DLL export macro for WIN32 systems for
32// importing/exporting external symbols to DLLs
33//
34# if defined G4LIB_BUILD_DLL
35# define DLLEXPORT __declspec(dllexport)
36# define DLLIMPORT __declspec(dllimport)
37# else
38# define DLLEXPORT
39# define DLLIMPORT
40# endif
41//
42// Unique identifier for global module
43//
44# if defined PTL_ALLOC_EXPORT
45# define PTL_DLL DLLEXPORT
46# else
47# define PTL_DLL DLLIMPORT
48# endif
49#else
50# define DLLEXPORT
51# define DLLIMPORT
52# define PTL_DLL
53#endif
54
55#include <atomic>
56#include <complex>
57#include <limits>
58#include <memory>
59
60namespace PTL
61{
62//--------------------------------------------------------------------------------------//
63//
64namespace api
65{
66struct native
67{};
68struct tbb
69{};
70} // namespace api
71//
72//--------------------------------------------------------------------------------------//
73//
74template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
75 typename Pair = std::pair<Ptr, Ptr>>
76Pair&
78{
79 static auto _master = std::make_shared<Tp>();
80 static std::atomic<int64_t> _count(0);
81 static thread_local auto _inst =
82 Pair(_master, Ptr((_count++ == 0) ? nullptr : new Tp()));
83 return _inst;
84}
85//
86//--------------------------------------------------------------------------------------//
87//
88template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
89 typename Pair = std::pair<Ptr, Ptr>>
90Ptr
92{
93 static thread_local auto& _pinst = GetSharedPointerPair<Tp, Tag>();
94 static thread_local auto& _inst = _pinst.second.get() ? _pinst.second : _pinst.first;
95 return _inst;
96}
97//
98//--------------------------------------------------------------------------------------//
99//
100template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
101 typename Pair = std::pair<Ptr, Ptr>>
102Ptr
104{
105 static auto& _pinst = GetSharedPointerPair<Tp, Tag>();
106 static auto _inst = _pinst.first;
107 return _inst;
108}
109
110//--------------------------------------------------------------------------------------//
111
112template <typename CountedType>
114{
115public:
118
119public:
120 // return number of existing objects:
121 static int64_t live() { return count(); }
122 static constexpr int64_t zero() { return static_cast<int64_t>(0); }
123 static int64_t max_depth() { return fmax_depth; }
124
125 static void enable(const bool& val) { fenabled = val; }
126 static void set_max_depth(const int64_t& val) { fmax_depth = val; }
127 static bool is_enabled() { return fenabled; }
128
129 template <typename Tp = CountedType,
130 typename std::enable_if<std::is_same<Tp, void>::value>::type* = nullptr>
131 static bool enable()
132 {
133 return fenabled && fmax_depth > count();
134 }
135 // the void type is consider the global setting
136 template <typename Tp = CountedType,
137 typename std::enable_if<!std::is_same<Tp, void>::value>::type* = nullptr>
138 static bool enable()
139 {
140 return void_type::is_enabled() && void_type::max_depth() > count() && fenabled &&
141 fmax_depth > count();
142 }
143
144protected:
145 // default constructor
146 CountedObject() { ++count(); }
147 ~CountedObject() { --count(); }
148 CountedObject(const this_type&) { ++count(); }
149 explicit CountedObject(this_type&&) { ++count(); }
150
151private:
152 // number of existing objects
153 static int64_t& thread_number();
154 static int64_t& master_count();
155 static int64_t& count();
156 static int64_t fmax_depth;
157 static bool fenabled;
158};
159
160//--------------------------------------------------------------------------------------//
161
162template <typename CountedType>
163int64_t&
164CountedObject<CountedType>::thread_number()
165{
166 static std::atomic<int64_t> _all_instance;
167 static thread_local int64_t _instance = _all_instance++;
168 return _instance;
169}
170
171//--------------------------------------------------------------------------------------//
172
173template <typename CountedType>
174int64_t&
175CountedObject<CountedType>::master_count()
176{
177 static int64_t _instance = 0;
178 return _instance;
179}
180
181//--------------------------------------------------------------------------------------//
182
183template <typename CountedType>
184int64_t&
185CountedObject<CountedType>::count()
186{
187 if(thread_number() == 0)
188 return master_count();
189 static thread_local int64_t _instance = master_count();
190 return _instance;
191}
192
193//--------------------------------------------------------------------------------------//
194
195template <typename CountedType>
196int64_t CountedObject<CountedType>::fmax_depth = std::numeric_limits<int64_t>::max();
197
198//--------------------------------------------------------------------------------------//
199
200template <typename CountedType>
201bool CountedObject<CountedType>::fenabled = true;
202
203//======================================================================================//
204
205} // namespace PTL
206
207// Forward declation of void type argument for usage in direct object
208// persistency to define fake default constructors
209//
210class __void__;
CountedObject< void > void_type
Definition: Types.hh:117
CountedObject(this_type &&)
Definition: Types.hh:149
CountedObject(const this_type &)
Definition: Types.hh:148
static int64_t live()
Definition: Types.hh:121
CountedObject< CountedType > this_type
Definition: Types.hh:116
static bool is_enabled()
Definition: Types.hh:127
static void enable(const bool &val)
Definition: Types.hh:125
static int64_t max_depth()
Definition: Types.hh:123
static void set_max_depth(const int64_t &val)
Definition: Types.hh:126
static constexpr int64_t zero()
Definition: Types.hh:122
static bool enable()
Definition: Types.hh:131
Definition: AutoLock.hh:254
Pair & GetSharedPointerPair()
Definition: Types.hh:77
Ptr GetSharedPointerPairInstance()
Definition: Types.hh:91
Ptr GetSharedPointerPairMasterInstance()
Definition: Types.hh:103