csv_printing.h
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_dynamics_api package.
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Christian Emmerich
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef RC_DYNAMICS_API_CSV_PRINTING_H_H
37 #define RC_DYNAMICS_API_CSV_PRINTING_H_H
38 
39 #include <string>
40 #include <sstream>
41 #include <iostream>
42 #include <vector>
43 #include <memory>
44 
46 
47 #include "roboception/msgs/frame.pb.h"
48 #include "roboception/msgs/dynamics.pb.h"
49 #include "roboception/msgs/imu.pb.h"
50 
51 #ifdef WIN32
52 #ifdef GetMessage
53 #undef GetMessage
54 #endif
55 #endif
56 
57 namespace csv
58 {
62 struct Header
63 {
64  std::vector<std::string> fields_;
65  std::string prefix_;
66 
67  static Header prefixed(const std::string& p, const ::google::protobuf::Message& m)
68  {
69  Header header;
70  header.prefix_ = p;
71  return header << m;
72  }
73 
74  Header& operator<<(const std::string& field)
75  {
76  fields_.insert(std::end(fields_), prefix_ + field);
77  return *this;
78  }
79 
80  Header& operator<<(const Header& other)
81  {
82  for (auto&& f : other.fields_)
83  fields_.insert(std::end(fields_), prefix_ + f);
84  return *this;
85  }
86 
87  Header& operator<<(const ::google::protobuf::Message& m)
88  {
89  using namespace ::google::protobuf;
90 
91  auto descr = m.GetDescriptor();
92  auto refl = m.GetReflection();
93  for (int i = 0; i < descr->field_count(); ++i)
94  {
95  auto field = descr->field(i);
96 
97  if (field->is_repeated())
98  {
99  int size = refl->FieldSize(m, field);
100  if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
101  {
102  for (int k = 0; k < size; k++)
103  {
104  *this << prefixed(field->name() + '_' + std::to_string(k), refl->GetMessage(m, field));
105  }
106  }
107  else
108  {
109  for (int k = 0; k < size; k++)
110  {
111  *this << field->name() + "_" + std::to_string(k);
112  }
113  }
114  }
115  else if (!field->is_optional() || refl->HasField(m, field))
116  {
117  if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
118  {
119  *this << prefixed(field->name() + '_', refl->GetMessage(m, field));
120  }
121  else
122  {
123  *this << field->name();
124  }
125  }
126  }
127 
128  return *this;
129  }
130 };
131 
135 struct Line
136 {
137  std::vector<std::string> entries_;
138 
139  Line& operator<<(const std::string& t)
140  {
141  this->entries_.insert(std::end(this->entries_), t);
142  return *this;
143  }
144 
145  Line& operator<<(const ::google::protobuf::Message& m)
146  {
147  using namespace ::google::protobuf;
148 
149  auto descr = m.GetDescriptor();
150  auto refl = m.GetReflection();
151  for (int i = 0; i < descr->field_count(); ++i)
152  {
153  auto field = descr->field(i);
154 
155  if (field->is_repeated())
156  {
157  int size = refl->FieldSize(m, field);
158  switch (field->cpp_type())
159  {
160  case FieldDescriptor::CPPTYPE_MESSAGE:
161  for (int k = 0; k < size; ++k)
162  {
163  *this << refl->GetRepeatedMessage(m, field, k);
164  }
165  break;
166  case FieldDescriptor::CPPTYPE_BOOL:
167  for (int k = 0; k < size; ++k)
168  {
169  *this << std::to_string(refl->GetRepeatedBool(m, field, k));
170  }
171  break;
172  case FieldDescriptor::CPPTYPE_ENUM:
173  for (int k = 0; k < size; ++k)
174  {
175  *this << refl->GetRepeatedEnum(m, field, k)->name();
176  }
177  break;
178  case FieldDescriptor::CPPTYPE_FLOAT:
179  for (int k = 0; k < size; ++k)
180  {
181  *this << std::to_string(refl->GetRepeatedFloat(m, field, k));
182  }
183  break;
184  case FieldDescriptor::CPPTYPE_DOUBLE:
185  for (int k = 0; k < size; ++k)
186  {
187  *this << std::to_string(refl->GetRepeatedDouble(m, field, k));
188  }
189  break;
190  case FieldDescriptor::CPPTYPE_UINT32:
191  for (int k = 0; k < size; ++k)
192  {
193  *this << std::to_string(refl->GetRepeatedUInt32(m, field, k));
194  }
195  break;
196  case FieldDescriptor::CPPTYPE_UINT64:
197  for (int k = 0; k < size; ++k)
198  {
199  *this << std::to_string(refl->GetRepeatedUInt64(m, field, k));
200  }
201  break;
202  case FieldDescriptor::CPPTYPE_INT32:
203  for (int k = 0; k < size; ++k)
204  {
205  *this << std::to_string(refl->GetRepeatedInt32(m, field, k));
206  }
207  break;
208  case FieldDescriptor::CPPTYPE_INT64:
209  for (int k = 0; k < size; ++k)
210  {
211  *this << std::to_string(refl->GetRepeatedInt64(m, field, k));
212  }
213  break;
214  case FieldDescriptor::CPPTYPE_STRING:
215  for (int k = 0; k < size; ++k)
216  {
217  *this << refl->GetRepeatedString(m, field, k);
218  }
219  break;
220  }
221  }
222  else if (!field->is_optional() || refl->HasField(m, field))
223  {
224  switch (field->cpp_type())
225  {
226  case FieldDescriptor::CPPTYPE_MESSAGE:
227  *this << refl->GetMessage(m, field);
228  break;
229  case FieldDescriptor::CPPTYPE_BOOL:
230  *this << std::to_string(refl->GetBool(m, field));
231  break;
232  case FieldDescriptor::CPPTYPE_ENUM:
233  *this << refl->GetEnum(m, field)->name();
234  break;
235  case FieldDescriptor::CPPTYPE_FLOAT:
236  *this << std::to_string(refl->GetFloat(m, field));
237  break;
238  case FieldDescriptor::CPPTYPE_DOUBLE:
239  *this << std::to_string(refl->GetDouble(m, field));
240  break;
241  case FieldDescriptor::CPPTYPE_UINT32:
242  *this << std::to_string(refl->GetUInt32(m, field));
243  break;
244  case FieldDescriptor::CPPTYPE_UINT64:
245  *this << std::to_string(refl->GetUInt64(m, field));
246  break;
247  case FieldDescriptor::CPPTYPE_INT32:
248  *this << std::to_string(refl->GetInt32(m, field));
249  break;
250  case FieldDescriptor::CPPTYPE_INT64:
251  *this << std::to_string(refl->GetInt64(m, field));
252  break;
253  case FieldDescriptor::CPPTYPE_STRING:
254  *this << refl->GetString(m, field);
255  break;
256  }
257  }
258  }
259 
260  return *this;
261  }
262 };
263 }
264 
265 std::ostream& operator<<(std::ostream& s, const csv::Header& header)
266 {
267  bool first = true;
268  for (auto&& hf : header.fields_)
269  {
270  if (first)
271  s << hf;
272  else
273  s << "," << hf;
274  first = false;
275  }
276  return s;
277 }
278 
279 std::ostream& operator<<(std::ostream& s, const csv::Line& csv)
280 {
281  bool first = true;
282  for (auto&& e : csv.entries_)
283  {
284  if (first)
285  {
286  s << e;
287  }
288  else
289  {
290  s << "," << e;
291  }
292  first = false;
293  }
294 
295  return s;
296 }
297 
298 #endif // RC_DYNAMICS_API_CSV_PRINTING_H_H
Line & operator<<(const ::google::protobuf::Message &m)
Definition: csv_printing.h:145
std::string prefix_
Definition: csv_printing.h:65
Header & operator<<(const ::google::protobuf::Message &m)
Definition: csv_printing.h:87
Header & operator<<(const std::string &field)
Definition: csv_printing.h:74
struct and methods to "organice" printing of csv-Headers
Definition: csv_printing.h:62
Header & operator<<(const Header &other)
Definition: csv_printing.h:80
std::vector< std::string > entries_
Definition: csv_printing.h:137
Line & operator<<(const std::string &t)
Definition: csv_printing.h:139
std::vector< std::string > fields_
Definition: csv_printing.h:64
struct and methods to "organice" printing of csv-Lines
Definition: csv_printing.h:135
static Header prefixed(const std::string &p, const ::google::protobuf::Message &m)
Definition: csv_printing.h:67


rc_dynamics_api
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Endres
autogenerated on Thu May 9 2019 02:51:36