Not exactly an Atomic implementation - If you use shared resources, this helps you protecting the access with a Mutex.

Work in progress...

Files at this revision

API Documentation at this revision

Comitter:
MatteoT
Date:
Fri Jul 19 06:13:48 2013 +0000
Parent:
2:16cab88c1203
Commit message:
method templates inside header file

Changed in this revision

SharedObject.cpp Show diff for this revision Revisions of this file
SharedObject.h Show annotated file Show diff for this revision Revisions of this file
diff -r 16cab88c1203 -r 1069ef627cff SharedObject.cpp
--- a/SharedObject.cpp	Fri Jul 19 05:34:48 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#include "SharedObject.h"
-
-template<class T>
-SharedObject<T>::SharedObject (const T& value)
-{
-    _readwrite_mutex.lock();
-    _value = value;
-    _readwrite_mutex.unlock();
-}
-
-template<class T>
-SharedObject<T>::SharedObject ()
-{
-    _readwrite_mutex.unlock();
-}
-
-template<class T>
-void SharedObject<T>::get (T& value_destination) const
-{
-    _readwrite_mutex.lock();
-    value_destination = _value;
-    _readwrite_mutex.unlock();
-}
-template<class T>
-SharedObject<T>::operator T () const
-{
-    T tmp_value;
-    get(tmp_value);
-    return tmp_value;
-}
-
-template<class T>
-void SharedObject<T>::set (const T& new_value)
-{
-    _readwrite_mutex.lock();
-    _value = new_value;
-    _readwrite_mutex.unlock();
-}
-template<class T>
-void SharedObject<T>::operator= (const T& new_value)
-{
-    set(new_value);
-}
\ No newline at end of file
diff -r 16cab88c1203 -r 1069ef627cff SharedObject.h
--- a/SharedObject.h	Fri Jul 19 05:34:48 2013 +0000
+++ b/SharedObject.h	Fri Jul 19 06:13:48 2013 +0000
@@ -23,23 +23,49 @@
     /** Resource constructor.
      *  @param value sets the initial value of the resource.
      */
-    SharedObject (const T& value);
+    SharedObject (const T& value)
+    {
+        _readwrite_mutex.lock();
+        _value = value;
+        _readwrite_mutex.unlock();
+    }
 
     /** Resource constructor without initial value.
      */
-    SharedObject ();
+    SharedObject ()
+    {
+        _readwrite_mutex.unlock();
+    }
 
     /** Sets the specified value_destination with the value of the shared resource.
      */
-    void get (T& value_destination) const;
+    void get (T& value_destination) const
+    {
+        _readwrite_mutex.lock();
+        value_destination = _value;
+        _readwrite_mutex.unlock();
+    }
     ///Returns the value of the shared resource (may be slower than get).
-    operator T () const;
+    operator T () const
+    {
+        T tmp_value;
+        get(tmp_value);
+        return tmp_value;
+    }
 
     /** Sets the value of the shared resource with the specified new_value.
      */
-    void set (const T& new_value);
+    void set (const T& new_value)
+    {
+        _readwrite_mutex.lock();
+        _value = new_value;
+        _readwrite_mutex.unlock();
+    }
     ///Alias of set.
-    void operator= (const T& new_value);
+    void operator= (const T& new_value)
+    {
+        set(new_value);
+    }
 };