libcluon  0.0.148
ToJSONVisitor.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_TOJSONVISITOR_HPP
10 #define CLUON_TOJSONVISITOR_HPP
11 
12 #include "cluon/any/any.hpp"
13 #include "cluon/cluon.hpp"
14 
15 #include <cstdint>
16 #include <map>
17 #include <sstream>
18 #include <string>
19 
20 namespace cluon {
35  private:
36  ToJSONVisitor(const ToJSONVisitor &) = delete;
37  ToJSONVisitor(ToJSONVisitor &&) = delete;
38  ToJSONVisitor &operator=(const ToJSONVisitor &) = delete;
39  ToJSONVisitor &operator=(ToJSONVisitor &&) = delete;
40 
41  public:
50  ToJSONVisitor(bool withOuterCurlyBraces = true, const std::map<uint32_t, bool> &mask = {}) noexcept;
51 
55  std::string json() const noexcept;
56 
57  public:
58  // The following methods are provided to allow an instance of this class to
59  // be used as visitor for an instance with the method signature void accept<T>(T&);
60 
61  void preVisit(int32_t id, const std::string &shortName, const std::string &longName) noexcept;
62  void postVisit() noexcept;
63 
64  void visit(uint32_t id, std::string &&typeName, std::string &&name, bool &v) noexcept;
65  void visit(uint32_t id, std::string &&typeName, std::string &&name, char &v) noexcept;
66  void visit(uint32_t id, std::string &&typeName, std::string &&name, int8_t &v) noexcept;
67  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint8_t &v) noexcept;
68  void visit(uint32_t id, std::string &&typeName, std::string &&name, int16_t &v) noexcept;
69  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint16_t &v) noexcept;
70  void visit(uint32_t id, std::string &&typeName, std::string &&name, int32_t &v) noexcept;
71  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint32_t &v) noexcept;
72  void visit(uint32_t id, std::string &&typeName, std::string &&name, int64_t &v) noexcept;
73  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint64_t &v) noexcept;
74  void visit(uint32_t id, std::string &&typeName, std::string &&name, float &v) noexcept;
75  void visit(uint32_t id, std::string &&typeName, std::string &&name, double &v) noexcept;
76  void visit(uint32_t id, std::string &&typeName, std::string &&name, std::string &v) noexcept;
77 
78  template <typename T>
79  void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept {
80  (void)typeName;
81  if ((0 == m_mask.count(id)) || m_mask[id]) {
82  try {
83  ToJSONVisitor jsonVisitor;
84  value.accept(jsonVisitor);
85  m_buffer << '\"' << name << '\"' << ':' << jsonVisitor.json() << ',' << '\n';
86  } catch (const linb::bad_any_cast &) { // LCOV_EXCL_LINE
87  }
88  }
89  }
90 
91  public:
98  static std::string encodeBase64(const std::string &input) noexcept;
99 
100  private:
101  bool m_withOuterCurlyBraces{true};
102  std::map<uint32_t, bool> m_mask;
103  std::stringstream m_buffer{};
104 };
105 
106 } // namespace cluon
107 #endif
Definition: cluon.hpp:65
void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
Definition: ToJSONVisitor.hpp:79
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: ToJSONVisitor.hpp:34
std::string json() const noexcept
Definition: ToJSONVisitor.cpp:20