iceoryx_hoofs  2.0.2
serialization.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_HOOFS_CXX_SERIALIZATION_HPP
18 #define IOX_HOOFS_CXX_SERIALIZATION_HPP
19 
20 #include "iceoryx_hoofs/cxx/convert.hpp"
21 
22 #include <cstdlib>
23 #include <iostream>
24 #include <sstream>
25 
26 namespace iox
27 {
28 namespace cxx
29 {
66 {
67  public:
70  explicit Serialization(const std::string& value) noexcept;
71 
74  std::string toString() const noexcept;
75 
78  operator std::string() const noexcept;
79 
85  template <typename... Targs>
86  static Serialization create(const Targs&... args) noexcept;
87 
96  template <typename T, typename... Targs>
97  bool extract(T& t, Targs&... args) const noexcept;
98 
106  template <typename T>
107  bool getNth(const unsigned int index, T& t) const noexcept;
108 
110  enum class Error
111  {
112  DESERIALIZATION_FAILED,
113  };
114 
115  private:
116  std::string m_value;
117  static constexpr char separator = ':';
118 
119  private:
120  static std::string serializer() noexcept;
121 
122  static bool removeFirstEntry(std::string& firstEntry, std::string& remainder) noexcept;
123 
124  template <typename T>
125  static typename std::enable_if<std::is_convertible<T, Serialization>::value, std::string>::type
126  getString(const T& t) noexcept;
127  template <typename T>
128  static typename std::enable_if<!std::is_convertible<T, Serialization>::value, std::string>::type
129  getString(const T& t) noexcept;
130  template <typename T, typename... Targs>
131  static std::string serializer(const T& t, const Targs&... args) noexcept;
132 
133  static bool deserialize(const std::string& serializedString) noexcept;
134 
135  template <typename T, typename... Targs>
136  static bool deserialize(const std::string& serializedString, T& t, Targs&... args) noexcept;
137 };
138 
139 } // namespace cxx
140 } // namespace iox
141 
142 #include "iceoryx_hoofs/internal/cxx/serialization.inl"
143 
144 #endif // IOX_HOOFS_CXX_SERIALIZATION_HPP
Simple serializer which serials every given type into the following format: (The type needs to be con...
Definition: serialization.hpp:66
bool getNth(const unsigned int index, T &t) const noexcept
Extracts the value at index and writes it into t. If the conversion failed it returns false It also r...
Serialization(const std::string &value) noexcept
Creates a serialization object from a given raw serialization.
bool extract(T &t, Targs &... args) const noexcept
Extracts the values from the serialization and writes them into the the given args,...
std::string toString() const noexcept
string conversion operator, returns the raw serialized string
Error
This is an error which can be used for cxx::expected on a custom deserialization when extract fails.
Definition: serialization.hpp:111
static Serialization create(const Targs &... args) noexcept
Create Serialization if every arguments is convertable to string via cxx::convert::toString,...
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29