siplasplas
A library for C++ reflection and introspection
iostream.hpp
1 #ifndef SIPLASPLAS_TYPEERASURE_FEATURES_IOSTREAM_HPP
2 #define SIPLASPLAS_TYPEERASURE_FEATURES_IOSTREAM_HPP
3 
4 #include <siplasplas/utility/staticif.hpp>
5 #include <siplasplas/utility/exception.hpp>
6 #include <siplasplas/typeerasure/concepts/iostream.hpp>
7 
8 namespace cpp
9 {
10 
11 namespace features
12 {
13 
19 {
20 public:
34  template<typename T>
35  static std::ostream& apply(std::ostream& os, const T& value) noexcept(cpp::concepts::OStreamable<T>::no_except)
36  {
37  return cpp::staticIf<cpp::concepts::OStreamable<T>::value>([&](auto identity) -> std::ostream&
38  {
39  return identity(os) << value;
40  }).Else([](auto) -> std::ostream&
41  {
42  throw cpp::exception<std::runtime_error>(
43  "{} is not ostreamable!",
44  ctti::type_id<T>().name()
45  );
46  });
47  }
48 };
49 
55 {
56 public:
70  template<typename T>
71  static std::istream& apply(std::istream& is, T& value) noexcept(cpp::concepts::IStreamable<T>::no_except)
72  {
73  return cpp::staticIf<cpp::concepts::IStreamable<T>::value>([&](auto identity) -> std::istream&
74  {
75  return identity(is) >> value;
76  }).Else([](auto) -> std::istream&
77  {
78  throw cpp::exception<std::runtime_error>(
79  "{} is not istreamable!",
80  ctti::type_id<T>().name()
81  );
82  });
83  }
84 };
85 }
86 
87 }
88 
89 #endif // SIPLASPLAS_TYPEERASURE_FEATURES_IOSTREAM_HPP
Definition: canary_allocator.hpp:7
static std::istream & apply(std::istream &is, T &value) noexcept(cpp::concepts::IStreamable< T >::no_except)
Implements read from input stream feature.
Definition: iostream.hpp:71
Implements write to an standard output stream.
Definition: iostream.hpp:18
Checks whether a vaue of type T can be written to a standard stream.
Definition: iostream.hpp:45
Implements read from an standard input stream.
Definition: iostream.hpp:54
static std::ostream & apply(std::ostream &os, const T &value) noexcept(cpp::concepts::OStreamable< T >::no_except)
Implements write to output stream feature.
Definition: iostream.hpp:35
Checks whether a vaue of type T can be read from a standard stream.
Definition: iostream.hpp:75