| 1 | /* | 
|---|
| 2 | * AtomicInstance.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Apr 26, 2016 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 |  | 
|---|
| 9 | #ifndef ATOMICINSTANCE_HPP_ | 
|---|
| 10 | #define ATOMICINSTANCE_HPP_ | 
|---|
| 11 |  | 
|---|
| 12 | // include config.h | 
|---|
| 13 | #ifdef HAVE_CONFIG_H | 
|---|
| 14 | #include <config.h> | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 | #include <boost/thread/mutex.hpp> | 
|---|
| 18 |  | 
|---|
| 19 | class AtomicInstanceTest; | 
|---|
| 20 |  | 
|---|
| 21 | /** The AtomicInstance class wraps another class with a mutex that is locked | 
|---|
| 22 | * on construction and unlocked on destruction. Hence, only a single entity | 
|---|
| 23 | * of this instance can be present at any single given time and thus allows | 
|---|
| 24 | * convenient atomic access to the state of the wrapped class. | 
|---|
| 25 | * | 
|---|
| 26 | * This is not perfect. The ref to the internal class may be stored elsewhere | 
|---|
| 27 | * and be used after the scope is left and the wrapping AtomicInstance is | 
|---|
| 28 | * destroyed. | 
|---|
| 29 | * | 
|---|
| 30 | * It is up to the user to maintain access to the class only within the lifetime | 
|---|
| 31 | * of this AtomicInstance object, i.e. to use it only within scopes. | 
|---|
| 32 | * | 
|---|
| 33 | * Note that this instance can be moved but not copied. That is on assignment | 
|---|
| 34 | * the contained pointer is NULL'ed in the right-hand side, regardless of whether | 
|---|
| 35 | * the rhs is const or not. | 
|---|
| 36 | */ | 
|---|
| 37 | template <class T> | 
|---|
| 38 | class AtomicInstance | 
|---|
| 39 | { | 
|---|
| 40 | //!> grant unit test access to private parts | 
|---|
| 41 | friend class AtomicInstanceTest; | 
|---|
| 42 | public: | 
|---|
| 43 | /** Restrict copy cstor to only moves. | 
|---|
| 44 | * | 
|---|
| 45 | * \warning This resets \a content in \a rhs to mark it as being copied. | 
|---|
| 46 | * | 
|---|
| 47 | * \param rhs right-hand side, its \a content is NULL after this | 
|---|
| 48 | */ | 
|---|
| 49 | AtomicInstance(AtomicInstance<T>& rhs); | 
|---|
| 50 |  | 
|---|
| 51 | /** Restrict copy cstor to only moves. | 
|---|
| 52 | * | 
|---|
| 53 | * \warning This resets \a content in \a rhs to mark it as being copied. | 
|---|
| 54 | * | 
|---|
| 55 | * \param rhs right-hand side, its \a content is NULL after this | 
|---|
| 56 | */ | 
|---|
| 57 | AtomicInstance(const AtomicInstance<T>& rhs); | 
|---|
| 58 |  | 
|---|
| 59 | /** Cstor for wrapping the given instant \a _content. | 
|---|
| 60 | * | 
|---|
| 61 | * \param _content instance to wrap | 
|---|
| 62 | */ | 
|---|
| 63 | explicit AtomicInstance(T* _content); | 
|---|
| 64 |  | 
|---|
| 65 | /** Dstor to unlock the mutex. | 
|---|
| 66 | * | 
|---|
| 67 | */ | 
|---|
| 68 | ~AtomicInstance(); | 
|---|
| 69 |  | 
|---|
| 70 | /** Getter for the ref to the internal object. | 
|---|
| 71 | * | 
|---|
| 72 | * \warning This must not be stored outside the scope of the AtomicInstance. | 
|---|
| 73 | *          We cannot prevent its copying. | 
|---|
| 74 | */ | 
|---|
| 75 | T& operator*(); | 
|---|
| 76 |  | 
|---|
| 77 | /** Getter for the ref to the internal object. | 
|---|
| 78 | * | 
|---|
| 79 | * \warning This must not be stored outside the scope of the AtomicInstance. | 
|---|
| 80 | *          We cannot prevent its copying. | 
|---|
| 81 | */ | 
|---|
| 82 | const T& operator*() const; | 
|---|
| 83 |  | 
|---|
| 84 | private: | 
|---|
| 85 | /** Disallow empty instance cstor. | 
|---|
| 86 | * | 
|---|
| 87 | */ | 
|---|
| 88 | AtomicInstance(); | 
|---|
| 89 |  | 
|---|
| 90 | /** Restrict assignment operator. | 
|---|
| 91 | * | 
|---|
| 92 | * \param rhs right-hand side, its \a content is NULL after this | 
|---|
| 93 | */ | 
|---|
| 94 | AtomicInstance<T>& operator=(const AtomicInstance<T>& rhs); | 
|---|
| 95 |  | 
|---|
| 96 | private: | 
|---|
| 97 |  | 
|---|
| 98 | //!> internal ptr to the protected instance, not (un)locking when NULL | 
|---|
| 99 | mutable T* content; | 
|---|
| 100 |  | 
|---|
| 101 | //!> a lock for atomic access to the state of the instance | 
|---|
| 102 | static boost::mutex atomicLock; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | #include "AtomicInstance_impl.hpp" | 
|---|
| 106 |  | 
|---|
| 107 | #endif /* ATOMICINSTANCE_HPP_ */ | 
|---|