libcluon  0.0.148
ToCSVVisitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_TOCSVVISITOR_HPP
10 #define CLUON_TOCSVVISITOR_HPP
11 
12 #include "cluon/cluon.hpp"
13 
14 #include <cstdint>
15 #include <map>
16 #include <sstream>
17 #include <string>
18 
19 namespace cluon {
39  private:
40  ToCSVVisitor(const ToCSVVisitor &) = delete;
41  ToCSVVisitor(ToCSVVisitor &&) = delete;
42  ToCSVVisitor &operator=(const ToCSVVisitor &) = delete;
43  ToCSVVisitor &operator=(ToCSVVisitor &&) = delete;
44 
45  public:
56  ToCSVVisitor(char delimiter = ';', bool withHeader = true, const std::map<uint32_t, bool> &mask = {}) noexcept;
57 
58  protected:
68  ToCSVVisitor(const std::string &prefix, char delimiter, bool withHeader, bool isNested) noexcept;
69 
70  public:
74  std::string csv() const noexcept;
75 
79  void clear() noexcept;
80 
81  public:
82  // The following methods are provided to allow an instance of this class to
83  // be used as visitor for an instance with the method signature void accept<T>(T&);
84 
85  void preVisit(int32_t id, const std::string &shortName, const std::string &longName) noexcept;
86  void postVisit() noexcept;
87 
88  void visit(uint32_t id, std::string &&typeName, std::string &&name, bool &v) noexcept;
89  void visit(uint32_t id, std::string &&typeName, std::string &&name, char &v) noexcept;
90  void visit(uint32_t id, std::string &&typeName, std::string &&name, int8_t &v) noexcept;
91  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint8_t &v) noexcept;
92  void visit(uint32_t id, std::string &&typeName, std::string &&name, int16_t &v) noexcept;
93  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint16_t &v) noexcept;
94  void visit(uint32_t id, std::string &&typeName, std::string &&name, int32_t &v) noexcept;
95  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint32_t &v) noexcept;
96  void visit(uint32_t id, std::string &&typeName, std::string &&name, int64_t &v) noexcept;
97  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint64_t &v) noexcept;
98  void visit(uint32_t id, std::string &&typeName, std::string &&name, float &v) noexcept;
99  void visit(uint32_t id, std::string &&typeName, std::string &&name, double &v) noexcept;
100  void visit(uint32_t id, std::string &&typeName, std::string &&name, std::string &v) noexcept;
101 
102  template <typename T>
103  void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept {
104  (void)id;
105  (void)typeName;
106  if ((0 == m_mask.count(id)) || m_mask[id]) {
107  constexpr bool IS_NESTED{true};
108  ToCSVVisitor csvVisitor(name, m_delimiter, m_withHeader, IS_NESTED);
109  value.accept(csvVisitor);
110 
111  if (m_fillHeader) {
112  m_bufferHeader << csvVisitor.m_bufferHeader.str();
113  }
114  m_bufferValues << csvVisitor.m_bufferValues.str();
115  }
116  }
117 
118  private:
119  std::map<uint32_t, bool> m_mask{};
120  std::string m_prefix{};
121  char m_delimiter{';'};
122  bool m_withHeader{true};
123  bool m_isNested{false};
124  bool m_fillHeader{true};
125  std::stringstream m_bufferHeader{};
126  std::stringstream m_bufferValues{};
127 };
128 
129 } // namespace cluon
130 #endif
void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
Definition: ToCSVVisitor.hpp:103
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: ToCSVVisitor.hpp:38