wrapper_object.h 865 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // wrapper_object.hpp
  3. // logging
  4. //
  5. // Created by Sam Jaffe on 4/4/19.
  6. //
  7. #pragma once
  8. #include <iostream>
  9. #include <string>
  10. #include "detail/to_string.h"
  11. #include "detail/to_json.h"
  12. namespace logging { namespace detail {
  13. class object {
  14. public:
  15. template <typename T> explicit object(T & object);
  16. private:
  17. friend std::ostream & operator<<(std::ostream & os, object const & obj) {
  18. return os << obj.to_string_(obj.ptr_);
  19. }
  20. template <typename> static std::string to_string_impl(void * ptr);
  21. private:
  22. void * ptr_;
  23. std::string (*to_string_)(void*);
  24. };
  25. template <typename T>
  26. std::string object::to_string_impl(void * ptr) {
  27. return to_string(*static_cast<T*>(ptr));
  28. }
  29. template <typename T>
  30. object::object(T & object)
  31. : ptr_(&object), to_string_(&object::to_string_impl<T>) {
  32. }
  33. } }