SSP21-CPP
EndianHelpers.h
1 /*
2  * Copyright (c) 2018, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef SER4CPP_ENDIANHELPERS_H
26 #define SER4CPP_ENDIANHELPERS_H
27 
28 #include "ser4cpp/serialization/SerializationTemplates.h"
29 #include "ser4cpp/util/Uncopyable.h"
30 
31 namespace ser4cpp
32 {
33 
34 template <class Int16Type, class UInt16Type, class Int32Type, class UInt32Type, class Int64Type, class UInt64Type>
35 class EndianHelpers : private StaticOnly
36 {
37 
38 public:
39  template <class T, typename... Args>
40  static bool read(rseq_t& input, T& value, Args& ... args)
41  {
42  return read_one(input, value) && read(input, args...);
43  }
44 
45  template <class T, typename... Args>
46  static bool write(wseq_t& dest, const T& value, const Args& ... args)
47  {
48  return write_one(dest, value) && write(dest, args...);
49  }
50 
51 private:
52  static inline bool read(rseq_t& input)
53  {
54  return true;
55  }
56 
57  static inline bool read_one(rseq_t& input, uint8_t& out)
58  {
59  return UInt8::read_from(input, out);
60  }
61 
62  static inline bool read_one(rseq_t& input, int16_t& out)
63  {
64  return Int16Type::read_from(input, out);
65  }
66 
67  static inline bool read_one(rseq_t& input, uint16_t& out)
68  {
69  return UInt16Type::read_from(input, out);
70  }
71 
72  static inline bool read_one(rseq_t& input, int32_t& out)
73  {
74  return Int32Type::read_from(input, out);
75  }
76 
77  static inline bool read_one(rseq_t& input, uint32_t& out)
78  {
79  return UInt32Type::read_from(input, out);
80  }
81 
82  static inline bool read_one(rseq_t& input, int64_t& out)
83  {
84  return Int64Type::read_from(input, out);
85  }
86 
87  static inline bool read_one(rseq_t& input, uint64_t& out)
88  {
89  return UInt64Type::read_from(input, out);
90  }
91 
92  static inline bool write(wseq_t& dest)
93  {
94  return true;
95  }
96 
97  static inline bool write_one(wseq_t& dest, const uint8_t& value)
98  {
99  return UInt8::write_to(dest, value);
100  }
101 
102  static inline bool write_one(wseq_t& dest, const int16_t& value)
103  {
104  return Int16Type::write_to(dest, value);
105  }
106 
107  static inline bool write_one(wseq_t& dest, const uint16_t& value)
108  {
109  return UInt16Type::write_to(dest, value);
110  }
111 
112  static inline bool write_one(wseq_t& dest, const int32_t& value)
113  {
114  return Int32Type::write_to(dest, value);
115  }
116 
117  static inline bool write_one(wseq_t& dest, const uint32_t& value)
118  {
119  return UInt32Type::write_to(dest, value);
120  }
121 
122  static inline bool write_one(wseq_t& dest, const int64_t& value)
123  {
124  return Int64Type::write_to(dest, value);
125  }
126 
127  static inline bool write_one(wseq_t& dest, const uint64_t& value)
128  {
129  return UInt64Type::write_to(dest, value);
130  }
131 };
132 
133 }
134 
135 #endif
ser4cpp header-only library namespace
Definition: Buffer.h:33