Turi Create  4.0
table_element_printers.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_TABLE_ELEMENT_PRINTERS_H_
7 #define TURI_TABLE_ELEMENT_PRINTERS_H_
8 
9 #include <core/data/flexible_type/flexible_type.hpp>
10 #include <type_traits>
11 #include <atomic>
12 #include <core/parallel/atomic.hpp>
13 
14 namespace turi {
15 
16 struct progress_time;
17 
18 namespace table_internal {
19 
20 /** Printers for each of the primimtive types. Called by the routines below.
21  */
22 
23 void _format_time(std::stringstream& ts, double t);
24 
25 void _print_string(std::ostringstream& ss, size_t width, const std::string& s);
26 void _print_double(std::ostringstream& ss, size_t width, double s);
27 void _print_bool(std::ostringstream& ss, size_t width, bool b);
28 void _print_long(std::ostringstream& ss, size_t width, long v);
29 void _print_time(std::ostringstream& ss, size_t width, double pt);
30 void _print_flexible_type(std::ostringstream& ss, size_t width, const flexible_type& pt);
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 // Now specific classes to direct how each type is printed and how
34 // values are stored in the type string.
35 
36 class table_printer_element_base {
37  public:
38  enum class style_type : uint8_t {
39  kDefault, // Written as number or string
40  kBool, // Written as true or false
41  kProgressTime, // Written as human-readable elapsed time
42  };
43 
44  virtual void print(std::ostringstream& ss, size_t width){};
45  virtual flexible_type get_value(){return FLEX_UNDEFINED;}
46 };
47 
48 template <typename T, class Enable = void>
49 struct table_printer_element : public table_printer_element_base
50 {
51  static constexpr bool valid_type = false;
52  static constexpr style_type style = style_type::kDefault;
53 };
54 
55 /** For printing doubles.
56  */
57 template <typename T>
58 struct table_printer_element
59 <T, typename std::enable_if<std::is_floating_point<T>::value>::type >
60  : public table_printer_element_base {
61 
62  public:
63  static constexpr bool valid_type = true;
64  static constexpr style_type style = style_type::kDefault;
65 
66  table_printer_element(T v)
67  : value(double(v))
68  {}
69 
70  void print(std::ostringstream& ss, size_t width) {
71  _print_double(ss, width, value);
72  }
73 
74  flexible_type get_value() {
75  return value;
76  }
77 
78 private:
79  double value;
80 };
81 
82 
83 
84 /** For printing bools.
85  */
86 template <typename T>
87 struct table_printer_element
88 <T, typename std::enable_if<std::is_same<T, bool>::value>::type >
89  : public table_printer_element_base {
90  public:
91  static constexpr bool valid_type = true;
92  static constexpr style_type style = style_type::kBool;
93 
94  table_printer_element(T v)
95  : value(v)
96  {}
97 
98  void print(std::ostringstream& ss, size_t width) {
99  _print_bool(ss, width, value);
100  }
101 
102  flexible_type get_value() {
103  return value;
104  }
105 
106 private:
107  bool value;
108 };
109 
110 
111 /** For printing signed integers.
112  */
113 template <typename T>
114 struct table_printer_element
115 <T, typename std::enable_if< (std::is_integral<T>::value && (!std::is_same<T, bool>::value))>::type >
116  : public table_printer_element_base {
117 public:
118  static constexpr bool valid_type = true;
119  static constexpr style_type style = style_type::kDefault;
120 
121  table_printer_element(const T& v)
122  : value(v)
123  {}
124 
125  void print(std::ostringstream& ss, size_t width) {
126  _print_long(ss, width, value);
127  }
128 
129  flexible_type get_value() {
130  return value;
131  }
132 
133 private:
134  long value;
135 };
136 
137 /** For printing std atomics.
138  */
139 template <typename T>
140 struct table_printer_element
141 <std::atomic<T>, typename std::enable_if<std::is_integral<T>::value>::type >
142  : public table_printer_element<T> {
143 public:
144  table_printer_element(const std::atomic<T>& v)
145  : table_printer_element<T>(T(v))
146  {}
147 };
148 
149 /** For printing turicreate atomics.
150  */
151 template <typename T>
152 struct table_printer_element
153 <turi::atomic<T>, typename std::enable_if<std::is_integral<T>::value>::type >
154  : public table_printer_element<T> {
155 public:
156  table_printer_element(const std::atomic<T>& v)
157  : table_printer_element<T>(T(v))
158  {}
159 };
160 
161 
162 /** For printing strings.
163  */
164 template <typename T>
165 struct table_printer_element
166 <T, typename std::enable_if<std::is_convertible<T, std::string>::value
167  && !std::is_same<T, flexible_type>::value>::type>
168  : public table_printer_element_base {
169 
170  public:
171  static constexpr bool valid_type = true;
172  static constexpr style_type style = style_type::kDefault;
173 
174  table_printer_element(const T& v)
175  : value(v)
176  {}
177 
178  void print(std::ostringstream& ss, size_t width) {
179  _print_string(ss, width, value);
180  }
181 
182  flexible_type get_value() {
183  return value;
184  }
185 
186 private:
187  std::string value;
188 };
189 
190 /** For printing progress time.
191  */
192 template <typename T>
193 struct table_printer_element
194 <T, typename std::enable_if<std::is_same<T, progress_time>::value>::type>
195  : public table_printer_element_base {
196 
197  public:
198  static constexpr bool valid_type = true;
199  static constexpr style_type style = style_type::kProgressTime;
200 
201  table_printer_element(double v)
202  : value(v)
203  {}
204 
205  void print(std::ostringstream& ss, size_t width) {
206  _print_time(ss, width, value);
207  }
208 
209  flexible_type get_value() {
210  return value;
211  }
212 
213 private:
214  double value;
215 };
216 
217 /** For printing flexible_type.
218  */
219 template <typename T>
220 struct table_printer_element
221 <T, typename std::enable_if<std::is_same<T, flexible_type>::value>::type >
222  : public table_printer_element_base {
223  public:
224  static constexpr bool valid_type = true;
225  static constexpr style_type style = style_type::kDefault;
226 
227  table_printer_element(const T& v)
228  : value(v)
229  {}
230 
231  void print(std::ostringstream& ss, size_t width) {
232  _print_flexible_type(ss, width, value);
233  }
234 
235  flexible_type get_value() {
236  return value;
237  }
238 
239 private:
240  flexible_type value;
241 };
242 
243 
244 }}
245 
246 #endif /* TURI_TABLE_PRINTER_IMPL_H_ */
STL namespace.
static flexible_type FLEX_UNDEFINED