Util.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef QLOW_UTIL_H
  2. #define QLOW_UTIL_H
  3. #include <vector>
  4. #include <memory>
  5. #include <sstream>
  6. #include <typeinfo>
  7. namespace qlow
  8. {
  9. template<typename T>
  10. using OwningList = std::vector<std::unique_ptr<T>>;
  11. /// I don't like this, but I lack better ideas at the moment.
  12. /// TODO: find better solution
  13. /*!
  14. * \brief tries to cast a unique_ptr and throws if it fails
  15. */
  16. template<typename T, typename U>
  17. std::unique_ptr<T> unique_dynamic_cast(std::unique_ptr<U>&& p)
  18. {
  19. U* released = p.release();
  20. if (T* casted = dynamic_cast<T*>(released); casted)
  21. return std::unique_ptr<T> (casted);
  22. else {
  23. delete released;
  24. //throw "failed unique dynamic cast";
  25. throw std::string("invalid unique_dynamic_cast from ") + typeid(p.get()).name() + " to " + typeid(T).name();
  26. }
  27. }
  28. namespace util
  29. {
  30. inline std::string toString(const void* a)
  31. {
  32. std::ostringstream o;
  33. o << a;
  34. return o.str();
  35. }
  36. }
  37. }
  38. #endif // QLOW_UTIL_H