Geant4-11
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#if !defined(PTL_DEFAULT_OBJECT)
56# define PTL_DEFAULT_OBJECT(NAME) \
57 NAME() = default; \
58 ~NAME() = default; \
59 NAME(const NAME&) = default; \
60 NAME(NAME&&) = default; \
61 NAME& operator=(const NAME&) = default; \
62 NAME& operator=(NAME&&) = default;
63#endif
64
65#include <atomic>
66#include <complex>
67#include <functional>
68#include <limits>
69#include <memory>
70#include <utility>
71
72namespace PTL
73{
74//--------------------------------------------------------------------------------------//
75//
76namespace api
77{
78struct native
79{};
80struct tbb
81{};
82} // namespace api
83//
84//--------------------------------------------------------------------------------------//
85//
86template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
87 typename Pair = std::pair<Ptr, Ptr>>
88Pair&
90{
91 static auto _master = std::make_shared<Tp>();
92 static std::atomic<int64_t> _count(0);
93 static thread_local auto _inst =
94 Pair(_master, Ptr((_count++ == 0) ? nullptr : new Tp()));
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 thread_local auto& _pinst = GetSharedPointerPair<Tp, Tag>();
106 static thread_local auto& _inst = _pinst.second.get() ? _pinst.second : _pinst.first;
107 return _inst;
108}
109//
110//--------------------------------------------------------------------------------------//
111//
112template <typename Tp, typename Tag = api::native, typename Ptr = std::shared_ptr<Tp>,
113 typename Pair = std::pair<Ptr, Ptr>>
114Ptr
116{
117 static auto& _pinst = GetSharedPointerPair<Tp, Tag>();
118 static auto _inst = _pinst.first;
119 return _inst;
120}
121
122//======================================================================================//
123
125{
126 template <typename FuncT>
127 ScopeDestructor(FuncT&& _func)
128 : m_functor(std::forward<FuncT>(_func))
129 {}
130
131 // delete copy operations
134
135 // allow move operations
137 : m_functor(std::move(rhs.m_functor))
138 {
139 rhs.m_functor = []() {};
140 }
142 {
143 if(this != &rhs)
144 {
145 m_functor = std::move(rhs.m_functor);
146 rhs.m_functor = []() {};
147 }
148 return *this;
149 }
150
152
153private:
154 std::function<void()> m_functor = []() {};
155};
156
157//======================================================================================//
158
159} // namespace PTL
160
161// Forward declation of void type argument for usage in direct object
162// persistency to define fake default constructors
163//
164class __void__;
G4double(* function)(G4double)
Definition: AutoLock.hh:254
Pair & GetSharedPointerPair()
Definition: Types.hh:89
Ptr GetSharedPointerPairInstance()
Definition: Types.hh:103
Ptr GetSharedPointerPairMasterInstance()
Definition: Types.hh:115
ScopeDestructor(ScopeDestructor &&rhs) noexcept
Definition: Types.hh:136
ScopeDestructor & operator=(const ScopeDestructor &)=delete
ScopeDestructor(const ScopeDestructor &)=delete
ScopeDestructor(FuncT &&_func)
Definition: Types.hh:127
std::function< void()> m_functor
Definition: Types.hh:154
ScopeDestructor & operator=(ScopeDestructor &&rhs) noexcept
Definition: Types.hh:141