33 #ifndef itkAtomicInt_h
34 #define itkAtomicInt_h
41 #if !ITK_COMPILER_CXX_ATOMIC
76 #if !ITK_COMPILER_CXX_ATOMIC
101 return static_cast<T
>(Impl::PreIncrement(&this->
m_Object));
106 return static_cast<T
>(Impl::PostIncrement(&this->
m_Object));
111 return static_cast<T
>(Impl::PreDecrement(&this->
m_Object));
116 return static_cast<T
>(Impl::PostDecrement(&this->
m_Object));
121 return static_cast<T
>(Impl::AddAndFetch(&this->
m_Object,
122 static_cast<typename Impl::ValueType>(val)));
127 return static_cast<T
>(Impl::SubAndFetch(&this->
m_Object,
128 static_cast<typename Impl::ValueType>(val)));
133 return static_cast<T
>(Impl::Load(&this->
m_Object));
138 Impl::Store(&this->
m_Object, static_cast<typename Impl::ValueType>(val));
150 return static_cast<T
>(Impl::Load(&this->
m_Object));
155 Impl::Store(&this->
m_Object, static_cast<typename Impl::ValueType>(val));
163 template <
typename T>
175 : m_Object(reinterpret_cast<typename Impl::ValueType>(val))
180 : m_Object(reinterpret_cast<typename Impl::ValueType>(ai.load()))
186 return reinterpret_cast<T*
>(Impl::AddAndFetch(&this->m_Object,
sizeof(T)));
191 T* val =
reinterpret_cast<T*
>(Impl::AddAndFetch(&this->m_Object,
sizeof(T)));
197 return reinterpret_cast<T*
>(Impl::SubAndFetch(&this->m_Object,
sizeof(T)));
202 T* val =
reinterpret_cast<T*
>(Impl::AddAndFetch(&this->m_Object,
sizeof(T)));
208 return reinterpret_cast<T*
>(Impl::AddAndFetch(&this->m_Object,
214 return reinterpret_cast<T*
>(Impl::SubAndFetch(&this->m_Object,
220 return reinterpret_cast<T*
>(Impl::Load(&this->m_Object));
225 Impl::Store(&this->m_Object,
226 reinterpret_cast<typename Impl::ValueType>(val));
232 this->store(ai.
load());
238 return reinterpret_cast<T*
>(Impl::Load(&this->m_Object));
243 Impl::Store(&this->m_Object,
244 reinterpret_cast<typename Impl::ValueType>(val));
263 : m_Object(reinterpret_cast<Impl::ValueType>(val))
268 : m_Object(reinterpret_cast<Impl::ValueType>(ai.load()))
272 operator void*()
const
274 return reinterpret_cast<void*
>(Impl::Load(&this->m_Object));
279 Impl::Store(&this->m_Object,
280 reinterpret_cast<Impl::ValueType>(val));
286 this->store(ai.
load());
292 return reinterpret_cast<void*
>(Impl::Load(&this->m_Object));
297 Impl::Store(&this->m_Object,
298 reinterpret_cast<Impl::ValueType>(val));
305 #else // C++11 ITK_COMPILER_CXX_ATOMIC == 1
307 template <
typename T>
312 typedef typename Impl::ValueType ValueType;
321 :
m_Object(static_cast<ValueType>(val))
352 return static_cast<T
>(
m_Object+=
static_cast<ValueType
>(val));
357 return static_cast<T
>(
m_Object-=
static_cast<ValueType
>(val));
362 return static_cast<T
>(
m_Object.load());
367 m_Object.store(static_cast<ValueType>(val));
371 AtomicInt<T>&
operator=(
const AtomicInt<T> &ai)
373 this->
store(ai.load());
379 return static_cast<T
>(
m_Object.load());
384 m_Object.store(static_cast<ValueType>(val));
388 std::atomic<typename Detail::AtomicOps<sizeof(T)>::ValueType >
m_Object;
392 template <
typename T>
396 typedef Detail::AtomicOps<sizeof(T*)> Impl;
397 typedef typename Impl::ValueType ValueType;
400 AtomicInt() : m_Object(0)
405 : m_Object(reinterpret_cast<ValueType>(val))
409 AtomicInt(
const AtomicInt<T*> &ai)
410 : m_Object(reinterpret_cast<ValueType>(ai.load()))
416 return reinterpret_cast<T*
>(m_Object.fetch_add(
sizeof(T))+
sizeof(T));
421 return reinterpret_cast<T*
>(m_Object.fetch_add(
sizeof(T)));
426 return reinterpret_cast<T*
>(m_Object.fetch_sub(
sizeof(T))-
sizeof(T));
431 return reinterpret_cast<T*
>(m_Object.fetch_sub(
sizeof(T)));
434 T* operator+=(std::ptrdiff_t val)
436 return reinterpret_cast<T*
>(m_Object += val *
sizeof(T));
439 T* operator-=(std::ptrdiff_t val)
441 return reinterpret_cast<T*
>(m_Object -= val *
sizeof(T));
446 return reinterpret_cast<T*
>(m_Object.load());
451 m_Object.store(reinterpret_cast<ValueType>(val));
455 AtomicInt<T*>& operator=(
const AtomicInt<T*> &ai)
457 this->store(ai.load());
463 return reinterpret_cast<T*
>(m_Object.load());
468 m_Object.store(reinterpret_cast<ValueType>(val));
472 std::atomic<typename Detail::AtomicOps<sizeof(T*)>::ValueType > m_Object;
476 template <>
class AtomicInt<void*>
479 typedef Detail::AtomicOps<sizeof(void*)> Impl;
480 typedef Impl::ValueType ValueType;
483 AtomicInt() : m_Object(0)
488 : m_Object(reinterpret_cast<ValueType>(val))
492 AtomicInt(
const AtomicInt<void*> &ai)
493 : m_Object(reinterpret_cast<ValueType>(ai.load()))
497 operator void*()
const
499 return reinterpret_cast<void*
>(m_Object.load());
502 void* operator=(
void* val)
504 m_Object.store(reinterpret_cast<ValueType>(val));
508 AtomicInt<void*>& operator=(
const AtomicInt<void*> &ai)
510 this->store(ai.load());
516 return reinterpret_cast<void*
>(m_Object.load());
519 void store(
void* val)
521 m_Object.store(reinterpret_cast<ValueType>(val));
525 std::atomic<typename Detail::AtomicOps<sizeof(void*)>::ValueType > m_Object;
T * operator-=(std::ptrdiff_t val)
Detail::AtomicOps< sizeof(void *)> Impl
Provides support for atomic integers.
AtomicInt< void * > & operator=(const AtomicInt< void * > &ai)
Impl::AtomicType m_Object
void * operator=(void *val)
Detail::AtomicOps< sizeof(T *)> Impl
Detail::AtomicOps< sizeof(T)> Impl
AtomicInt(const AtomicInt< T * > &ai)
AtomicInt< T > & operator=(const AtomicInt< T > &ai)
AtomicInt(const AtomicInt< void * > &ai)
AtomicInt(const AtomicInt< T > &ai)
Impl::AtomicType m_Object
AtomicInt< T * > & operator=(const AtomicInt< T * > &ai)
#define itkConceptMacro(name, concept)
T * operator+=(std::ptrdiff_t val)
Impl::AtomicType m_Object