libcluon  0.0.148
FromJSONVisitor.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_FROMJSONVISITOR_HPP
10 #define CLUON_FROMJSONVISITOR_HPP
11 
12 #include "cluon/JSONConstants.hpp"
13 #include "cluon/any/any.hpp"
14 #include "cluon/cluon.hpp"
15 
16 #include <cstdint>
17 #include <istream>
18 #include <map>
19 #include <string>
20 
21 namespace cluon {
29  class JSONKeyValue {
30  private:
31  JSONKeyValue &operator=(JSONKeyValue &&) = delete;
32 
33  public:
34  JSONKeyValue() = default;
35  JSONKeyValue(const JSONKeyValue &) = default;
36  JSONKeyValue(JSONKeyValue &&) = default;
37  JSONKeyValue &operator=(const JSONKeyValue &) = default;
38  ~JSONKeyValue() = default;
39 
40  public:
41  std::string m_key{""};
43  linb::any m_value;
44  };
45 
46  private:
47  FromJSONVisitor(const FromJSONVisitor &) = delete;
48  FromJSONVisitor(FromJSONVisitor &&) = delete;
49  FromJSONVisitor &operator=(FromJSONVisitor &&) = delete;
50  FromJSONVisitor &operator=(const FromJSONVisitor &other) = delete;
51 
57  FromJSONVisitor(std::map<std::string, FromJSONVisitor::JSONKeyValue> &preset) noexcept;
58 
59  public:
60  FromJSONVisitor() noexcept;
61  ~FromJSONVisitor() = default;
62 
63  public:
69  void decodeFrom(std::istream &in) noexcept;
70 
71  public:
72  // The following methods are provided to allow an instance of this class to
73  // be used as visitor for an instance with the method signature void accept<T>(T&);
74 
75  void preVisit(int32_t id, const std::string &shortName, const std::string &longName) noexcept;
76  void postVisit() noexcept;
77 
78  void visit(uint32_t id, std::string &&typeName, std::string &&name, bool &v) noexcept;
79  void visit(uint32_t id, std::string &&typeName, std::string &&name, char &v) noexcept;
80  void visit(uint32_t id, std::string &&typeName, std::string &&name, int8_t &v) noexcept;
81  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint8_t &v) noexcept;
82  void visit(uint32_t id, std::string &&typeName, std::string &&name, int16_t &v) noexcept;
83  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint16_t &v) noexcept;
84  void visit(uint32_t id, std::string &&typeName, std::string &&name, int32_t &v) noexcept;
85  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint32_t &v) noexcept;
86  void visit(uint32_t id, std::string &&typeName, std::string &&name, int64_t &v) noexcept;
87  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint64_t &v) noexcept;
88  void visit(uint32_t id, std::string &&typeName, std::string &&name, float &v) noexcept;
89  void visit(uint32_t id, std::string &&typeName, std::string &&name, double &v) noexcept;
90  void visit(uint32_t id, std::string &&typeName, std::string &&name, std::string &v) noexcept;
91 
92  template <typename T>
93  void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept {
94  (void)id;
95  (void)typeName;
96 
97  if (0 < m_keyValues.count(name)) {
98  try {
99  std::map<std::string, FromJSONVisitor::JSONKeyValue> v
100  = linb::any_cast<std::map<std::string, FromJSONVisitor::JSONKeyValue>>(m_keyValues[name].m_value);
101  cluon::FromJSONVisitor nestedJSONDecoder(v);
102  value.accept(nestedJSONDecoder);
103  } catch (const linb::bad_any_cast &) { // LCOV_EXCL_LINE
104  }
105  }
106  }
107 
108  public:
115  static std::string decodeBase64(const std::string &input) noexcept;
116 
117  private:
118  std::map<std::string, FromJSONVisitor::JSONKeyValue> readKeyValues(std::string &input) noexcept;
119 
120  private:
121  std::map<std::string, FromJSONVisitor::JSONKeyValue> m_data{};
122  std::map<std::string, FromJSONVisitor::JSONKeyValue> &m_keyValues;
123 };
124 } // namespace cluon
125 
126 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: FromJSONVisitor.hpp:25
JSONConstants
Definition: JSONConstants.hpp:16
void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
Definition: FromJSONVisitor.hpp:93