traits.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // traits.hpp
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 6/24/17.
  6. //
  7. #pragma once
  8. namespace stream { namespace detail {
  9. template <typename T>
  10. struct ref_or_val {
  11. ref_or_val operator=(T && val) { value = std::move(val); return *this; }
  12. operator T const &() const { return value; }
  13. T value;
  14. };
  15. template <typename T>
  16. struct ref_or_val<T&> {
  17. ref_or_val operator=(T & val) { value = &val; return *this; }
  18. operator T &() const { return *value; }
  19. T * value;
  20. };
  21. } }
  22. namespace stream { namespace detail {
  23. template <typename F> struct map_member_object;
  24. template <typename T, typename R>
  25. struct map_member_object<R T::*> {
  26. using type = R const &;
  27. type operator()(T const & val) const { return val.*mem; }
  28. R T::*mem;
  29. };
  30. template <typename F> struct map_member_function;
  31. template <typename T, typename R>
  32. struct map_member_function<R (T::*)() const> {
  33. using type = R;
  34. type operator()(T const & val) const { return (val.*mem)(); }
  35. R (T::* mem)() const;
  36. };
  37. }}
  38. namespace stream { namespace detail {
  39. template <typename T, typename = void>
  40. struct is_dereferencable : public std::false_type {};
  41. template <>
  42. struct is_dereferencable<void*> : public std::false_type {};
  43. template <typename T>
  44. struct is_dereferencable<T, typename std::enable_if<!std::is_void<decltype(*std::declval<T>())>::value>::type> : public std::true_type {};
  45. }}