wrapper_object.h 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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),
  32. to_string_(&object::to_string_impl<T>) {
  33. }
  34. } }