Virtual Testbed
Ship dynamics simulator for extreme conditions
fields.hh
1 #ifndef VTESTBED_BASE_FIELDS_HH
2 #define VTESTBED_BASE_FIELDS_HH
3 
4 #include <cmath>
5 #include <istream>
6 #include <type_traits>
7 
8 namespace vtb {
9 
10  namespace base {
11 
12  template <class T>
13  struct unsigned_field {
14 
15  static_assert(std::is_unsigned<T>::value, "bad type");
16 
17  typedef T value_type;
18  typedef typename std::make_signed<T>::type signed_value_type;
19 
20  value_type& value;
21 
22  inline friend std::istream&
23  operator>>(std::istream& in, const unsigned_field& rhs) {
24  signed_value_type tmp = 0; in >> tmp;
25  if (tmp < 0) { in.setstate(std::ios::failbit); }
26  else { rhs.value = static_cast<value_type>(tmp); }
27  return in;
28  }
29 
30  };
31 
32  template <class T>
34  make_unsigned_field(T& value) {
35  return unsigned_field<T>{value};
36  }
37 
38  template <class T>
40 
41  static_assert(std::is_floating_point<T>::value, "bad type");
42 
43  typedef T value_type;
44 
45  value_type& value;
46 
47  inline friend std::istream&
48  operator>>(std::istream& in, const floating_point_field& rhs) {
49  value_type tmp = 0; in >> tmp;
50  if (!std::isfinite(tmp)) { in.setstate(std::ios::failbit); }
51  else { rhs.value = tmp; }
52  return in;
53  }
54 
55  };
56 
57  template <class T>
59  make_floating_point_field(T& value) {
60  return floating_point_field<T>{value};
61  }
62 
63  }
64 
65 }
66 
67 #endif // vim:filetype=cpp
Main namespace.
Definition: convert.hh:9