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