Beamlib 3.1.2
This is the Beam C++ class library.
Loading...
Searching...
No Matches
BAtomic.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BAtomic.h BAtomic Atomic variables
3 * T.Barnaby, BEAM Ltd, 2010-10-07
4 * Copyright (c) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5 *******************************************************************************
6 *
7 * For license see LICENSE.txt at the root of the beamlib source tree.
8 */
9#ifndef BAtomic_H
10#define BAtomic_H 1
11
12#include <BTypes.h>
13
15template <class Type> class BAtomic {
16public:
17 BAtomic(Type value = 0) : ovalue(value){}
18
19 Type getValue() const {
20 return __sync_fetch_and_add(&ovalue, 0);
21 }
22 Type add(long value){
23 return __sync_add_and_fetch(&ovalue, value);
24 }
25 Type operator++(int){
26 return __sync_fetch_and_add(&ovalue, 1);
27 }
28 Type operator++(){
29 return __sync_add_and_fetch(&ovalue, 1);
30 }
31 Type operator--(int){
32 return __sync_fetch_and_add(&ovalue, -1);
33 }
34 Type operator--(){
35 return __sync_add_and_fetch(&ovalue, -1);
36 }
37 operator Type() const {
38 return getValue();
39 }
40private:
41 mutable Type ovalue;
42};
43
48
49#endif
BAtomic< BUInt32 > BAtomicUInt32
Definition: BAtomic.h:46
BAtomic< BInt64 > BAtomicInt64
Definition: BAtomic.h:45
BAtomic< BUInt64 > BAtomicUInt64
Definition: BAtomic.h:47
BAtomic< BInt32 > BAtomicInt32
Definition: BAtomic.h:44
BAtomic class increments/decrements different integer types.
Definition: BAtomic.h:15
Type getValue() const
Definition: BAtomic.h:19
BAtomic(Type value=0)
Definition: BAtomic.h:17
Type operator--()
Definition: BAtomic.h:34
Type operator--(int)
Definition: BAtomic.h:31
Type operator++(int)
Definition: BAtomic.h:25
Type add(long value)
Definition: BAtomic.h:22
Type operator++()
Definition: BAtomic.h:28