OSVR-Core
CSVCellGroup.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_CSVCellGroup_h_GUID_F36004E7_71A4_43AD_7353_B67BCBD8045A
26 #define INCLUDED_CSVCellGroup_h_GUID_F36004E7_71A4_43AD_7353_B67BCBD8045A
27 
28 // Internal Includes
29 #include <osvr/Util/CSV.h>
30 #include <osvr/Util/QuaternionC.h>
31 #include <osvr/Util/TimeValue.h>
32 #include <osvr/Util/Vec3C.h>
33 
34 // Library/third-party includes
35 // - none
36 
37 // Standard includes
38 // - none
39 
40 namespace osvr {
41 namespace util {
42  struct DefaultGroupTag;
50  template <typename RowProxyType, typename GroupTag = DefaultGroupTag>
52  public:
53  CellGroupProxy(RowProxyType &row, const char *groupPrefix)
54  : row_(row), prefix_(groupPrefix){};
55  CellGroupProxy(RowProxyType &row, std::string const &groupPrefix)
56  : row_(row), prefix_(groupPrefix){};
57 
58  RowProxyType getRow() { return std::forward<RowProxyType>(row_); }
59 
60  std::string const &getPrefix() const { return prefix_; }
61 
62  private:
63  RowProxyType &row_;
64  const std::string prefix_;
65  };
66 
71  template <typename RowProxyType, typename GroupTag, typename... CellArgs>
73  operator<<(CellGroupProxy<RowProxyType, GroupTag> &group,
74  detail::Cell<CellArgs...> &&currentCell) {
75  // cell.addHeaderPrefix(group.getPrefix());
76  group.getRow().add(cell(group.getPrefix() + currentCell.getHeader(),
77  currentCell.getData()));
78  return group;
79  }
80 
81  namespace detail {
82  template <typename PrefixType> struct PrefixTypeToArgType {
83  using type = PrefixType;
84  };
85  template <> struct PrefixTypeToArgType<std::string> {
86  using type = std::string const &;
87  };
90  template <typename T, typename PrefixType = const char *,
91  typename Tag = DefaultGroupTag>
92  class CellGroup {
93  public:
94  using prefix_type = PrefixType;
95  using prefix_arg_type =
96  typename PrefixTypeToArgType<PrefixType>::type;
97  CellGroup(prefix_arg_type prefix, T const &data)
98  : prefix_(prefix), data_(data) {}
99 
100  prefix_arg_type getPrefix() const { return prefix_; }
101  T const &getData() const { return data_; }
102 
103  private:
104  PrefixType prefix_;
105  T const &data_;
106  };
107 #if 0
108  template <typename T, typename Tag>
109  class CellGroup<T, std::string, Tag> {
110  public:
111  using prefix_type = std::string;
112  CellGroup(prefix_type const &prefix, T const &data)
113  : prefix_(prefix), data_(data) {}
114 
115  prefix_type const &getPrefix() const { return prefix_; }
116  T const &getData() const { return data_; }
117 
118  private:
119  prefix_type prefix_;
120  T const &data_;
121  };
122 #endif
123 
128  template <typename Derived, typename T, typename PrefixType,
129  typename Tag>
130  inline CSVRowProxy<Derived> &
131  operator<<(CSVRowProxy<Derived> &row,
133  using RowProxyType = CSVRowProxy<Derived> &;
134  auto proxy =
135  CellGroupProxy<RowProxyType, Tag>(row, group.getPrefix());
136  proxy << group.getData();
137  return row;
138  }
139 
144  template <typename Derived, typename T, typename PrefixType,
145  typename Tag>
146  inline CSVRowProxy<Derived> &&
147  operator<<(CSVRowProxy<Derived> &&row,
149  using RowProxyType = CSVRowProxy<Derived> &&;
151  std::forward<RowProxyType>(row), group.getPrefix()};
152  proxy << group.getData();
153  return std::move(row);
154  }
155 
156  } // namespace detail
157 
159  template <typename T>
160  inline detail::CellGroup<T> cellGroup(const char *groupHeader,
161  T const &data) {
162  return detail::CellGroup<T>{groupHeader, data};
163  }
164 
166  template <typename T>
168  cellGroup(std::string const &groupHeader, T const &data) {
169  return detail::CellGroup<T, std::string>{groupHeader, data};
170  }
173  template <typename Tag, typename T>
175  cellGroup(const char *groupHeader, T const &data,
176  Tag * /*dummy*/ = static_cast<Tag *>(nullptr)) {
177  return detail::CellGroup<T, const char *, Tag>{groupHeader, data};
178  }
179 
182  template <typename Tag, typename T>
184  cellGroup(std::string const &groupHeader, T const &data,
185  Tag * /*dummy*/ = static_cast<Tag *>(nullptr)) {
186  return detail::CellGroup<T, std::string, Tag>{groupHeader, data};
187  }
190  template <typename T> inline detail::CellGroup<T> cellGroup(T const &data) {
191  return detail::CellGroup<T>{"", data};
192  }
193 
196  template <typename Tag, typename T>
198  cellGroup(T const &data, Tag * /*dummy*/ = static_cast<Tag *>(nullptr)) {
200  }
201 
202  template <typename T>
203  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
204  time::TimeValue const &tv) {
205  group << cell("seconds", tv.seconds)
206  << cell("microseconds", tv.microseconds);
207  }
208  struct AbbreviatedTimeMemberFieldsTag;
209  template <typename T>
210  inline void
211  operator<<(CellGroupProxy<T, AbbreviatedTimeMemberFieldsTag> &group,
212  time::TimeValue const &tv) {
213  group << cell("sec", tv.seconds) << cell("usec", tv.microseconds);
214  }
215 
216  struct DecimalTimeFieldTag;
217  template <typename T>
218  inline void operator<<(CellGroupProxy<T, DecimalTimeFieldTag> &group,
219  time::TimeValue const &tv) {
220 
221  group << cell("seconds", time::toDecimalString(tv));
222  }
223 
224  template <typename T>
225  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
226  OSVR_Vec3 const &v) {
227  group << cell("x", v.data[0]) << cell("y", v.data[1])
228  << cell("z", v.data[2]);
229  }
230 
231  template <typename T>
232  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
233  OSVR_Quaternion const &q) {
234  group << cell("qw", osvrQuatGetW(&q)) << cell("qx", osvrQuatGetX(&q))
235  << cell("qy", osvrQuatGetY(&q)) << cell("qz", osvrQuatGetZ(&q));
236  }
237 
238 #ifdef EIGEN_WORLD_VERSION
239  template <typename T, typename Scalar>
240  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
241  Eigen::Matrix<Scalar, 2, 1> const &v) {
242  group << cell("x", v.x()) << cell("y", v.y());
243  }
244 
245  template <typename T>
246  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
247  Eigen::Vector3d const &v) {
248  group << cell("x", v.x()) << cell("y", v.y()) << cell("z", v.z());
249  }
250 
251  template <typename T>
252  inline void operator<<(CellGroupProxy<T, DefaultGroupTag> &group,
253  Eigen::Quaterniond const &q) {
254  group << cell("qw", q.w()) << cell("qx", q.x()) << cell("qy", q.y())
255  << cell("qz", q.z());
256  }
257 #endif
258 
259 } // namespace util
260 } // namespace osvr
261 #endif // INCLUDED_CSVCellGroup_h_GUID_F36004E7_71A4_43AD_7353_B67BCBD8045A
Temporary object created by the cellGroup call, captures the string (literal) prefix, the data reference/type, and the group tag.
Definition: CSVCellGroup.h:92
Definition: RunLoopManager.h:42
A structure defining a 3D vector, often a position/translation.
Definition: Vec3C.h:48
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: CSVCellGroup.h:82
Definition: TypeSafeIdHash.h:44
Returned by calls to .row() on a CSV object (you&#39;ll never need instantiate manually), and only really interacted with using the operator<< and cell() calls.
Definition: CSV.h:112
A structure defining a quaternion, often a unit quaternion representing 3D rotation.
Definition: QuaternionC.h:49
detail::CellGroup< T > cellGroup(const char *groupHeader, T const &data)
Helper function to create a cell group with a group header prefix.
Definition: CSVCellGroup.h:160
Utility class used in conjunction with osvr::util::CSV, to store a single table cell&#39;s column header ...
Definition: CSV.h:62
Definition: newuoa.h:1888
Header.
Header providing a C++ wrapper around TimeValueC.h.
A class that exists to make it easier to output composite data structures to CSV. ...
Definition: CSVCellGroup.h:51
Header.
Definition: Quaternion.h:47
std::string toDecimalString(TimeValue tv)
Converts to a precise decimal string.
Definition: TimeValue.h:67
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
detail::Cell< T > cell(const char *header, T const &data)
Helper free function to make a CSV cell.
Definition: CSV.h:401