24#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
29# pragma warning(disable : 4786)
34# if defined G4LIB_BUILD_DLL
35# define DLLEXPORT __declspec(dllexport)
36# define DLLIMPORT __declspec(dllimport)
44# if defined PTL_ALLOC_EXPORT
45# define PTL_DLL DLLEXPORT
47# define PTL_DLL DLLIMPORT
74template <
typename Tp,
typename Tag = api::native,
typename Ptr = std::shared_ptr<Tp>,
75 typename Pair = std::pair<Ptr, Ptr>>
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()));
88template <
typename Tp,
typename Tag = api::native,
typename Ptr = std::shared_ptr<Tp>,
89 typename Pair = std::pair<Ptr, Ptr>>
93 static thread_local auto& _pinst = GetSharedPointerPair<Tp, Tag>();
94 static thread_local auto& _inst = _pinst.second.get() ? _pinst.second : _pinst.first;
100template <
typename Tp,
typename Tag = api::native,
typename Ptr = std::shared_ptr<Tp>,
101 typename Pair = std::pair<Ptr, Ptr>>
105 static auto& _pinst = GetSharedPointerPair<Tp, Tag>();
106 static auto _inst = _pinst.first;
112template <
typename CountedType>
121 static int64_t
live() {
return count(); }
122 static constexpr int64_t
zero() {
return static_cast<int64_t
>(0); }
125 static void enable(
const bool& val) { fenabled = val; }
129 template <
typename Tp = CountedType,
130 typename std::enable_if<std::is_same<Tp, void>::value>::type* =
nullptr>
133 return fenabled && fmax_depth > count();
136 template <
typename Tp = CountedType,
137 typename std::enable_if<!std::is_same<Tp, void>::value>::type* =
nullptr>
141 fmax_depth > count();
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;
162template <
typename CountedType>
164CountedObject<CountedType>::thread_number()
166 static std::atomic<int64_t> _all_instance;
167 static thread_local int64_t _instance = _all_instance++;
173template <
typename CountedType>
175CountedObject<CountedType>::master_count()
177 static int64_t _instance = 0;
183template <
typename CountedType>
185CountedObject<CountedType>::count()
187 if(thread_number() == 0)
188 return master_count();
189 static thread_local int64_t _instance = master_count();
195template <
typename CountedType>
196int64_t CountedObject<CountedType>::fmax_depth = std::numeric_limits<int64_t>::max();
200template <
typename CountedType>
201bool CountedObject<CountedType>::fenabled =
true;
CountedObject< void > void_type
CountedObject(this_type &&)
CountedObject(const this_type &)
CountedObject< CountedType > this_type
static void enable(const bool &val)
static int64_t max_depth()
static void set_max_depth(const int64_t &val)
static constexpr int64_t zero()
Pair & GetSharedPointerPair()
Ptr GetSharedPointerPairInstance()
Ptr GetSharedPointerPairMasterInstance()