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


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58