wrapper_object.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. Json::Value json() const { return to_json_(ptr_); }
  17. private:
  18. friend std::ostream & operator<<(std::ostream & os, object const & obj) {
  19. return os << obj.to_string_(obj.ptr_);
  20. }
  21. template <typename> static std::string to_string_impl(void * ptr);
  22. template <typename> static Json::Value to_json_impl(void * ptr);
  23. private:
  24. void * ptr_;
  25. std::string (*to_string_)(void*);
  26. Json::Value (*to_json_)(void*);
  27. };
  28. template <typename T>
  29. std::string object::to_string_impl(void * ptr) {
  30. return to_string(*static_cast<T*>(ptr));
  31. }
  32. template <typename T>
  33. Json::Value object::to_json_impl(void * ptr) {
  34. return to_json(*static_cast<T*>(ptr));
  35. }
  36. template <typename T>
  37. object::object(T & object)
  38. : ptr_(&object),
  39. to_string_(&object::to_string_impl<T>),
  40. to_json_(&object::to_json_impl<T>) {
  41. }
  42. } }