Warning, file /include/Geant4/G4TaskSingletonDelegator.hh was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include "G4AutoLock.hh"
0035 #include "G4Threading.hh"
0036
0037 #include <functional>
0038 #include <map>
0039 #include <memory>
0040 #include <set>
0041 #include <thread>
0042 #include <tuple>
0043 #include <type_traits>
0044 #include <vector>
0045
0046 namespace G4Traits
0047 {
0048 template <typename T>
0049 struct TaskSingletonKey
0050 {
0051 using type = G4int;
0052 using compare_type = std::less<type>;
0053 };
0054 }
0055
0056
0057
0058
0059
0060
0061
0062 template <typename T>
0063 struct G4TaskSingletonEvaluator;
0064
0065
0066
0067 template <typename T>
0068 class G4TaskSingletonDelegator;
0069
0070
0071
0072 template <typename T>
0073 class G4TaskSingletonData
0074 {
0075 using key_type = typename G4Traits::TaskSingletonKey<T>::type;
0076 using compare_type = typename G4Traits::TaskSingletonKey<T>::compare_type;
0077
0078 friend struct G4TaskSingletonEvaluator<T>;
0079 friend class G4TaskSingletonDelegator<T>;
0080 using this_type = G4TaskSingletonData<T>;
0081
0082 private:
0083 static std::unique_ptr<this_type>& GetInstance()
0084 {
0085 static auto _instance = std::unique_ptr<this_type>(new this_type);
0086 return _instance;
0087 }
0088
0089 private:
0090 std::map<key_type, T*, compare_type> m_data;
0091 };
0092
0093
0094
0095 template <typename T>
0096 struct G4TaskSingletonEvaluator
0097 {
0098 using key_type = typename G4Traits::TaskSingletonKey<T>::type;
0099 using data_type = G4TaskSingletonData<T>;
0100
0101 template <typename... Args>
0102 G4TaskSingletonEvaluator(key_type&, Args&&...)
0103 {
0104 throw std::runtime_error("Not specialized!");
0105 }
0106 };
0107
0108
0109
0110 template <typename T>
0111 class G4TaskSingletonDelegator
0112 {
0113 public:
0114 using pointer = T*;
0115 using evaluator_type = G4TaskSingletonEvaluator<T>;
0116 using data_type = G4TaskSingletonData<T>;
0117 using key_type = typename G4Traits::TaskSingletonKey<T>;
0118
0119 template <typename... Args>
0120 static void Configure(Args&&... args)
0121 {
0122 auto& _data = data_type::GetInstance();
0123 evaluator_type _eval(std::forward<Args>(args)...);
0124 auto _ptr = _data->m_data.at(_eval.index);
0125 _eval.modifier(_ptr);
0126 T::SetInstance(_data->m_data.at(_key));
0127 }
0128 };
0129
0130