libcluon  0.0.148
ToLCMVisitor.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_TOLCMVISITOR_HPP
10 #define CLUON_TOLCMVISITOR_HPP
11 
12 #include "cluon/cluon.hpp"
13 
14 #include <cstdint>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 namespace cluon {
24  private:
25  ToLCMVisitor(const ToLCMVisitor &) = delete;
26  ToLCMVisitor(ToLCMVisitor &&) = delete;
27  ToLCMVisitor &operator=(const ToLCMVisitor &) = delete;
28  ToLCMVisitor &operator=(ToLCMVisitor &&) = delete;
29 
30  public:
31  ToLCMVisitor() = default;
32  ~ToLCMVisitor() = default;
33 
38  std::string encodedData(bool withHash = true) const noexcept;
39 
40  public:
41  // The following methods are provided to allow an instance of this class to
42  // be used as visitor for an instance with the method signature void accept<T>(T&);
43 
44  void preVisit(int32_t id, const std::string &shortName, const std::string &longName) noexcept;
45  void postVisit() noexcept;
46 
47  void visit(uint32_t id, std::string &&typeName, std::string &&name, bool &v) noexcept;
48  void visit(uint32_t id, std::string &&typeName, std::string &&name, char &v) noexcept;
49  void visit(uint32_t id, std::string &&typeName, std::string &&name, int8_t &v) noexcept;
50  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint8_t &v) noexcept;
51  void visit(uint32_t id, std::string &&typeName, std::string &&name, int16_t &v) noexcept;
52  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint16_t &v) noexcept;
53  void visit(uint32_t id, std::string &&typeName, std::string &&name, int32_t &v) noexcept;
54  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint32_t &v) noexcept;
55  void visit(uint32_t id, std::string &&typeName, std::string &&name, int64_t &v) noexcept;
56  void visit(uint32_t id, std::string &&typeName, std::string &&name, uint64_t &v) noexcept;
57  void visit(uint32_t id, std::string &&typeName, std::string &&name, float &v) noexcept;
58  void visit(uint32_t id, std::string &&typeName, std::string &&name, double &v) noexcept;
59  void visit(uint32_t id, std::string &&typeName, std::string &&name, std::string &v) noexcept;
60 
61  template <typename T>
62  void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept {
63  (void)id;
64  (void)typeName;
65  calculateHash(name);
66  calculateHash(0);
67 
68  // No hash for the type but for name and dimension.
69  cluon::ToLCMVisitor nestedLCMEncoder;
70  value.accept(nestedLCMEncoder);
71 
72  constexpr bool WITH_HASH{false};
73  const std::string s = nestedLCMEncoder.encodedData(WITH_HASH);
74  m_buffer.write(s.c_str(), static_cast<std::streamsize>(s.size()));
75 
76  // Save this complex field's hash for later to compute final hash.
77  m_hashes.push_back(nestedLCMEncoder.hash());
78  }
79 
80  private:
81  int64_t hash() const noexcept;
82  void calculateHash(char c) noexcept;
83  void calculateHash(const std::string &s) noexcept;
84 
85  private:
86  int64_t m_hash{0x12345678};
87  std::stringstream m_buffer{""};
88  std::vector<int64_t> m_hashes{};
89 };
90 } // namespace cluon
91 
92 #endif
Definition: cluon.hpp:65
std::string encodedData(bool withHash=true) const noexcept
Definition: ToLCMVisitor.cpp:21
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: ToLCMVisitor.hpp:23
void visit(uint32_t &id, std::string &&typeName, std::string &&name, T &value) noexcept
Definition: ToLCMVisitor.hpp:62