libcluon  0.0.148
FromMsgPackVisitor.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_FROMMSGPACKVISITOR_HPP
10 #define CLUON_FROMMSGPACKVISITOR_HPP
11 
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 MsgPackKeyValue {
30  private:
31  MsgPackKeyValue &operator=(MsgPackKeyValue &&) = delete;
32 
33  public:
34  MsgPackKeyValue() = default;
35  MsgPackKeyValue(const MsgPackKeyValue &) = default;
36  MsgPackKeyValue(MsgPackKeyValue &&) = default;
37  MsgPackKeyValue &operator=(const MsgPackKeyValue &) = default;
38  ~MsgPackKeyValue() = default;
39 
40  public:
41  std::string m_key{""};
43  linb::any m_value;
44  };
45 
46  private:
47  FromMsgPackVisitor(const FromMsgPackVisitor &) = delete;
49  FromMsgPackVisitor &operator=(FromMsgPackVisitor &&) = delete;
50  FromMsgPackVisitor &operator=(const FromMsgPackVisitor &other) = delete;
51 
57  FromMsgPackVisitor(std::map<std::string, FromMsgPackVisitor::MsgPackKeyValue> &preset) noexcept;
58 
59  public:
60  FromMsgPackVisitor() noexcept;
61  ~FromMsgPackVisitor() = 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, FromMsgPackVisitor::MsgPackKeyValue> v
100  = linb::any_cast<std::map<std::string, FromMsgPackVisitor::MsgPackKeyValue>>(m_keyValues[name].m_value);
101  cluon::FromMsgPackVisitor nestedMsgPackDecoder(v);
102  value.accept(nestedMsgPackDecoder);
103  } catch (const linb::bad_any_cast &) { // LCOV_EXCL_LINE
104  }
105  }
106  }
107 
108  private:
109  MsgPackConstants getFormatFamily(uint8_t T) noexcept;
110  std::map<std::string, FromMsgPackVisitor::MsgPackKeyValue> readKeyValues(std::istream &in) noexcept;
111  uint64_t readUint(std::istream &in) noexcept;
112  int64_t readInt(std::istream &in) noexcept;
113  std::string readString(std::istream &in) noexcept;
114 
115  private:
116  std::map<std::string, FromMsgPackVisitor::MsgPackKeyValue> m_data{};
117  std::map<std::string, FromMsgPackVisitor::MsgPackKeyValue> &m_keyValues;
118 };
119 } // namespace cluon
120 
121 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
Definition: FromMsgPackVisitor.hpp:93
Definition: FromMsgPackVisitor.hpp:25
MsgPackConstants
Definition: MsgPackConstants.hpp:16