BFLibCPP 0.1
CPP Library
Loading...
Searching...
No Matches
atomic.hpp
Go to the documentation of this file.
1
6#ifndef ATOMIC_HPP
7#define ATOMIC_HPP
8
9#include "access.hpp"
10#include "object.hpp"
11#include <stdbool.h>
12
13extern "C" {
14#include <bflibc/lock.h>
15}
16
17namespace BF {
18
19template <typename T>
20class Atomic : public Object {
21public:
22 Atomic() : Object() {
23 BFLockCreate(&this->_objlock);
24 }
25
26 // initializes with object
27 Atomic(T obj) : Atomic() {
28 this->set(obj);
29 }
30
32 if (this->_objlock) {
33 BFLockDestroy(&this->_objlock);
34 this->_objlock = NULL;
35 }
36 }
37
38 void unsafeset(T obj) {
39 this->_obj = obj;
40 }
41
42 T & unsafeget() {
43 return this->_obj;
44 }
45
46 void set(T obj) {
47 BFLockLock(&this->_objlock);
48 this->unsafeset(obj);
49 BFLockUnlock(&this->_objlock);
50 }
51
52 // returns a reference to object
53 //
54 // caller does NOT own
55 T & get() {
56 BFLockLock(&this->_objlock);
57 T & res = this->unsafeget();
58 BFLockUnlock(&this->_objlock);
59 return res;
60 }
61
66 void lock() {
67 BFLockLock(&this->_objlock);
68 }
69
73 void unlock() {
74 BFLockUnlock(&this->_objlock);
75 }
76
77 Atomic <T> & operator=(const T & obj) {
78 this->set(obj);
79 return *this;
80 }
81
83 this->set(a._obj);
84 return *this;
85 }
86
87 bool operator==(const Atomic<T> & a) {
88 return this->_obj == a._obj;
89 }
90
91 bool operator!=(const Atomic<T> & a) {
92 return this->_obj != a._obj;
93 }
94
95 operator T () {
96 return this->get();
97 }
98
99private:
100 T _obj;
101 BFLock _objlock;
102};
103
104}
105
106#endif // ATOMIC_HPP
107
Definition atomic.hpp:20
Atomic< T > & operator=(const Atomic< T > &a)
Definition atomic.hpp:82
void set(T obj)
Definition atomic.hpp:46
void unsafeset(T obj)
Definition atomic.hpp:38
Atomic()
Definition atomic.hpp:22
bool operator==(const Atomic< T > &a)
Definition atomic.hpp:87
void lock()
Definition atomic.hpp:66
void unlock()
Definition atomic.hpp:73
T & unsafeget()
Definition atomic.hpp:42
bool operator!=(const Atomic< T > &a)
Definition atomic.hpp:91
~Atomic()
Definition atomic.hpp:31
Atomic< T > & operator=(const T &obj)
Definition atomic.hpp:77
T & get()
Definition atomic.hpp:55
Atomic(T obj)
Definition atomic.hpp:27
Definition object.hpp:21
Definition array.hpp:18