Selaa lähdekoodia

re-added Util.h

Nicolas Winkler 6 vuotta sitten
vanhempi
commit
a1cb95c93a
1 muutettua tiedostoa jossa 58 lisäystä ja 0 poistoa
  1. 58 0
      src/util/Util.h

+ 58 - 0
src/util/Util.h

@@ -0,0 +1,58 @@
+#ifndef QLOW_UTIL_H
+#define QLOW_UTIL_H
+
+#include <vector>
+#include <memory>
+#include <sstream>
+#include <typeinfo>
+
+
+namespace qlow
+{
+    
+    template<typename T>
+    using OwningList = std::vector<std::unique_ptr<T>>;
+    
+    
+    
+    /// I don't like this, but I lack better ideas at the moment.
+    /// TODO: find better solution
+    /*!
+    * \brief tries to cast a unique_ptr and throws if it fails
+    */
+    template<typename T, typename U>
+    std::unique_ptr<T> unique_dynamic_cast(std::unique_ptr<U>&& p)
+    {
+        U* released = p.release();
+        if (T* casted = dynamic_cast<T*>(released); casted)
+            return std::unique_ptr<T> (casted);
+        else {
+            delete released;
+            //throw "failed unique dynamic cast";
+            throw std::string("invalid unique_dynamic_cast from ") + typeid(p.get()).name() + " to " + typeid(T).name();
+        }
+    }
+
+
+    
+    namespace util
+    {
+        inline std::string toString(const void* a)
+        {
+            std::ostringstream o;
+            o << a;
+            return o.str();
+        }
+    }
+}
+
+
+
+
+
+
+
+
+
+#endif // QLOW_UTIL_H
+