Util.h 982 B

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