]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/SGAtomic.hxx
restore some part of the code to prevent an untwanted segmentationf fault.
[simgear.git] / simgear / structure / SGAtomic.hxx
index 1ff9a44d05213f08c4cfd3ab7337c99e86d6ee7a..a7977fc31408483332cb004c026a59bd6cbb2d3c 100644 (file)
@@ -113,4 +113,33 @@ private:
   unsigned mValue;
 };
 
+namespace simgear
+{
+// Typesafe wrapper around SGSwappable
+template <typename T>
+class Swappable : private SGAtomic
+{
+public:
+    Swappable(const T& value) : SGAtomic(static_cast<unsigned>(value))
+    {
+    }
+    T operator() () const
+    {
+        return static_cast<T>(SGAtomic::operator unsigned ());
+    }
+    Swappable& operator=(const Swappable& rhs)
+    {
+        for (unsigned oldval = unsigned(*this);
+             !compareAndExchange(oldval, unsigned(rhs));
+             oldval = unsigned(*this))
+            ;
+        return *this;
+    }
+    bool compareAndSwap(const T& oldVal, const T& newVal)
+    {
+        return SGAtomic::compareAndExchange(static_cast<unsigned>(oldVal),
+                                           static_cast<unsigned>(newVal));
+    }
+};
+}
 #endif