density
C++11 library for paged memory management, function queues, heterogeneous queues and lifo memory management
io_runtimetype_features.h
1 
2 // Copyright Giuseppe Campana (giu.campana@gmail.com) 2016-2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #pragma once
9 #include <density/dynamic_reference.h>
10 #include <density/runtime_type.h>
11 #include <istream>
12 #include <ostream>
13 
14 namespace density
15 {
17  class f_istream
18  {
19  public:
21  template <typename TARGET_TYPE> constexpr static f_istream make() noexcept
22  {
23  return f_istream{&invoke<TARGET_TYPE>};
24  }
25 
30  void operator()(std::istream & i_istream, void * i_dest) const
31  {
32  DENSITY_ASSUME(i_dest != nullptr);
33  (*m_function)(i_istream, i_dest);
34  }
35 
36  private:
37  using Function = void (*)(std::istream & i_istream, void * i_dest);
38  Function const m_function;
39  constexpr f_istream(Function i_function) noexcept : m_function(i_function) {}
40  template <typename TARGET_TYPE> static void invoke(std::istream & i_istream, void * i_dest)
41  {
42  const auto derived = static_cast<TARGET_TYPE *>(i_dest);
43  i_istream >> *derived;
44  }
45  };
46 
48  class f_ostream
49  {
50  public:
52  template <typename TARGET_TYPE> constexpr static f_ostream make() noexcept
53  {
54  return f_ostream{&invoke<TARGET_TYPE>};
55  }
56 
61  void operator()(std::ostream & i_ostream, const void * i_dest) const
62  {
63  DENSITY_ASSUME(i_dest != nullptr);
64  (*m_function)(i_ostream, i_dest);
65  }
66 
67  private:
68  using Function = void (*)(std::ostream & i_ostream, const void * i_dest);
69  Function const m_function;
70  constexpr f_ostream(Function i_function) noexcept : m_function(i_function) {}
71  template <typename TARGET_TYPE>
72  static void invoke(std::ostream & i_ostream, const void * i_dest)
73  {
74  const auto derived = static_cast<const TARGET_TYPE *>(i_dest);
75  i_ostream << *derived;
76  }
77  };
78 
88  template <
89  typename... FEATURES,
90  typename =
91  typename std::enable_if<has_features<feature_list<FEATURES...>, f_istream>::value>::type>
92  inline std::istream & operator>>(
93  std::istream & i_source_stream, const dynamic_reference<runtime_type<FEATURES...>> & i_ptr)
94  {
95  i_ptr.type().template get_feature<f_istream>()(i_source_stream, i_ptr.address());
96  return i_source_stream;
97  }
98 
107  template <
108  typename... FEATURES,
109  typename =
110  typename std::enable_if<has_features<feature_list<FEATURES...>, f_ostream>::value>::type>
111  inline std::ostream & operator<<(
112  std::ostream & i_dest_stream, const dynamic_reference<runtime_type<FEATURES...>> & i_ref)
113  {
114  i_ref.type().template get_feature<f_ostream>()(i_dest_stream, i_ref.address());
115  return i_dest_stream;
116  }
117 
118 } // namespace density
Definition: io_runtimetype_features.h:17
std::ostream & operator<<(std::ostream &i_dest_stream, const dynamic_reference< runtime_type< FEATURES... >> &i_ref)
Definition: io_runtimetype_features.h:111
Definition: conc_function_queue.h:11
Definition: runtime_type.h:665
std::istream & operator>>(std::istream &i_source_stream, const dynamic_reference< runtime_type< FEATURES... >> &i_ptr)
Definition: io_runtimetype_features.h:92
void operator()(std::istream &i_istream, void *i_dest) const
Definition: io_runtimetype_features.h:30
void operator()(std::ostream &i_ostream, const void *i_dest) const
Definition: io_runtimetype_features.h:61
static constexpr f_istream make() noexcept
Definition: io_runtimetype_features.h:21
Definition: io_runtimetype_features.h:48
Definition: runtime_type.h:46
static constexpr f_ostream make() noexcept
Definition: io_runtimetype_features.h:52
Definition: dynamic_reference.h:107
Definition: runtime_type.h:509
#define DENSITY_ASSUME(bool_expr,...)
Definition: density_config.h:46