18 #ifndef ION_BASE_STATICSAFEDECLARE_H_
19 #define ION_BASE_STATICSAFEDECLARE_H_
23 #include <type_traits>
70 #define ION_SAFE_ASSIGN_STATIC_POINTER( \
71 type, variable, new_variable, add_func, destroyer) \
73 if (variable.compare_exchange_strong(null, new_variable)) { \
74 add_func(#type, new_variable); \
76 destroyer new_variable; \
80 #define ION_DECLARE_SAFE_STATIC( \
81 type, variable, constructor, add_func, destroyer) \
82 static std::atomic<type> atomic_##variable; \
83 ION_STATIC_ASSERT(std::is_pointer<type>::value, \
84 "static variables must be of pointer type"); \
85 type variable = atomic_##variable.load(std::memory_order_acquire); \
86 if (variable == 0) { \
87 type new_##variable = constructor; \
88 ION_SAFE_ASSIGN_STATIC_POINTER( \
89 type, atomic_##variable, new_##variable, add_func, destroyer); \
90 variable = atomic_##variable.load(std::memory_order_acquire); \
102 #define ION_DECLARE_SAFE_STATIC_ARRAY(type, variable, count) \
103 ION_DECLARE_SAFE_STATIC( \
107 ion::base::StaticDeleterDeleter::GetInstance()->AddArrayToDelete, \
111 #define ION_DECLARE_SAFE_STATIC_POINTER(type, variable) \
112 ION_DECLARE_SAFE_STATIC( \
116 ion::base::StaticDeleterDeleter::GetInstance()->AddPointerToDelete, \
122 #define ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR( \
123 type, variable, constructor) \
124 ION_DECLARE_SAFE_STATIC( \
128 ion::base::StaticDeleterDeleter::GetInstance()->AddPointerToDelete, \
155 pointer_to_delete_(pointer_to_delete) {}
161 delete pointer_to_delete_;
165 T* pointer_to_delete_;
173 pointer_to_delete_(pointer_to_delete) {}
179 delete [] pointer_to_delete_;
183 T* pointer_to_delete_;
198 template <
typename T>
204 template <
typename T>
211 template <
typename T>
220 return index < deleters_.size() ? deleters_[index] : NULL;
233 static void DestroyInstance();
239 static void SetInstancePtr(
const std::string&,
242 std::vector<StaticDeleterBase*> deleters_;
251 #endif // ION_BASE_STATICSAFEDECLARE_H_
StaticDeleter(const std::string &name, T *pointer_to_delete)
const std::string & GetTypeName() const
Returns the name of the type this deleter deletes.
const StaticDeleterBase * GetDeleterAt(size_t index) const
Returns the deleter at the passed index.
size_t GetDeleterCount() const
Returns the number of deleters in this.
void IgnoreInstance(const std::string &name, const T &ptr)
Does nothing, but simplifies the above macros for static non-pointer instances.
StaticDeleterDeleter is an internal class that holds and deletes StaticDeleters; it should not be use...
Shareable is an abstract base class for any object that can be shared via the SharedPtr class...
void AddPointerToDelete(const std::string &name, T *ptr)
Adds a regular pointer to be deleted when this class is destroyed.
A LockGuard locks a mutex when created, and unlocks it when destroyed.
virtual ~StaticDeleterBase()
StaticDeleterBase(const std::string &name)
void AddArrayToDelete(const std::string &name, T *ptr)
Adds an array pointer to be deleted when this class is destroyed.
This class should not be used directly.
port::Mutex mutex_
Protects shared access to the Allocator and FT_Library.
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
StaticDeleter(const std::string &name, T *pointer_to_delete)