package_serializer.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2019 FZI Forschungszentrum Informatik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
28 #ifndef UR_CLIENT_LIBRARY_PACKAGE_SERIALIZER_H_INCLUDED
29 #define UR_CLIENT_LIBRARY_PACKAGE_SERIALIZER_H_INCLUDED
30 
31 #include <endian.h>
32 #include <cstring>
33 
34 namespace urcl
35 {
36 namespace comm
37 {
43 {
44 public:
54  template <typename T>
55  static size_t serialize(uint8_t* buffer, T val)
56  {
57  size_t size = sizeof(T);
58  T tmp = encode(val);
59  std::memcpy(buffer, &tmp, size);
60  return size;
61  }
62 
71  static size_t serialize(uint8_t* buffer, double val)
72  {
73  size_t size = sizeof(double);
74  uint64_t inner;
75  std::memcpy(&inner, &val, size);
76  inner = encode(inner);
77  std::memcpy(buffer, &inner, size);
78  return size;
79  }
80 
89  static size_t serialize(uint8_t* buffer, std::string val)
90  {
91  const uint8_t* c_val = reinterpret_cast<const uint8_t*>(val.c_str());
92 
93  for (size_t i = 0; i < val.size(); i++)
94  {
95  buffer[i] = c_val[i];
96  }
97  return val.size();
98  }
99 
100 private:
101  template <typename T>
102  static T encode(T val)
103  {
104  return val;
105  }
106  static uint16_t encode(uint16_t val)
107  {
108  return htobe16(val);
109  }
110  static uint32_t encode(uint32_t val)
111  {
112  return htobe32(val);
113  }
114  static uint64_t encode(uint64_t val)
115  {
116  return htobe64(val);
117  }
118  static int16_t encode(int16_t val)
119  {
120  return htobe16(val);
121  }
122  static int32_t encode(int32_t val)
123  {
124  return htobe32(val);
125  }
126  static int64_t encode(int64_t val)
127  {
128  return htobe64(val);
129  }
130 };
131 
132 } // namespace comm
133 } // namespace urcl
134 #endif // UR_CLIENT_LIBRARY_PACKAGE_SERIALIZER_H_INCLUDED
static size_t serialize(uint8_t *buffer, double val)
A serialization method for double values.
A helper class to serialize packages. Contains methods for serializing all relevant datatypes...
static int64_t encode(int64_t val)
static int16_t encode(int16_t val)
static uint32_t encode(uint32_t val)
static size_t serialize(uint8_t *buffer, std::string val)
A serialization method for strings.
static uint64_t encode(uint64_t val)
static size_t serialize(uint8_t *buffer, T val)
A generalized serialization method for arbitrary datatypes.
static uint16_t encode(uint16_t val)
static int32_t encode(int32_t val)


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26