strusBase  0.17
hton.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 #ifndef _STRUS_BASE_HTON_HPP_INCLUDED
11 #define _STRUS_BASE_HTON_HPP_INCLUDED
12 #include "strus/base/stdint.h"
13 #include <arpa/inet.h>
14 
15 namespace strus
16 {
17 
18 template <typename Scalar>
19 class ByteOrder
20 {
21  typedef void net_value_type;
22 };
23 
24 template <>
25 class ByteOrder<uint8_t>
26 {
27 public:
28  typedef uint8_t net_value_type;
29 
30  static uint8_t hton( const uint8_t& value) {return value;}
31  static uint8_t ntoh( const uint8_t& value) {return value;}
32 };
33 
34 template <>
35 class ByteOrder<int8_t>
36 {
37 public:
38  typedef int8_t net_value_type;
39 
40  static int8_t hton( const int8_t& value) {return value;}
41  static int8_t ntoh( const int8_t& value) {return value;}
42 };
43 
44 template <>
45 class ByteOrder<uint16_t>
46 {
47 public:
48  typedef uint16_t net_value_type;
49 
50  static uint16_t hton( const uint16_t& value) {return htons(value);}
51  static uint16_t ntoh( const uint16_t& value) {return ntohs(value);}
52 };
53 
54 template <>
55 class ByteOrder<int16_t>
56 {
57 public:
58  typedef int16_t net_value_type;
59 
60  static int16_t hton( const int16_t& value) {return htons(value);}
61  static int16_t ntoh( const int16_t& value) {return ntohs(value);}
62 };
63 
64 template <>
65 class ByteOrder<uint32_t>
66 {
67 public:
68  typedef uint32_t net_value_type;
69 
70  static uint32_t hton( const uint32_t& value) {return htonl(value);}
71  static uint32_t ntoh( const uint32_t& value) {return ntohl(value);}
72 };
73 
74 template <>
75 class ByteOrder<int32_t>
76 {
77 public:
78  typedef int32_t net_value_type;
79 
80  static int32_t hton( const int32_t& value) {return htonl(value);}
81  static int32_t ntoh( const int32_t& value) {return ntohl(value);}
82 };
83 
84 template <>
85 class ByteOrder<uint64_t>
86 {
87 public:
88  typedef uint64_t net_value_type;
89 
90  static uint64_t hton( const uint64_t& value)
91  {
92  union
93  {
94  uint32_t p[2];
95  uint64_t v;
96  } val;
97  val.p[0] = htonl( value >> 32);
98  val.p[1] = htonl( value & 0xffFFffFF);
99  return val.v;
100  }
101  static uint64_t ntoh( const uint64_t& value)
102  {
103  union
104  {
105  uint32_t p[2];
106  uint64_t v;
107  } val;
108  val.v = value;
109  val.p[0] = ntohl( val.p[0]);
110  val.p[1] = ntohl( val.p[1]);
111  uint64_t rt = val.p[0];
112  rt <<= 32;
113  rt |= val.p[1];
114  return rt;
115  }
116 };
117 
118 template <>
119 class ByteOrder<int64_t>
120 {
121 public:
122  typedef int64_t net_value_type;
123 
124  static int64_t hton( const int64_t& value)
125  {
126  return (int64_t)ByteOrder<uint64_t>::hton((uint64_t)value);
127  }
128  static int64_t ntoh( const int64_t& value)
129  {
130  return (int64_t)ByteOrder<uint64_t>::ntoh((uint64_t)value);
131  }
132 };
133 
134 typedef uint32_t float_net_t;
135 typedef uint64_t double_net_t;
136 
137 template <>
138 class ByteOrder<float>
139 {
140 public:
142 
143  static net_value_type hton( const float& value)
144  {
145  union
146  {
147  net_value_type out;
148  float in;
149  } val;
150  val.in = value;
151  return ByteOrder<net_value_type>::hton( val.out);
152  }
153  static float ntoh( const net_value_type& value)
154  {
155  union
156  {
157  float out;
158  net_value_type in;
159  } val;
160  val.in = ByteOrder<net_value_type>::ntoh( value);
161  return val.out;
162  }
163 };
164 
165 template <>
166 class ByteOrder<double>
167 {
168 public:
170 
171  static net_value_type hton( const double& value)
172  {
173  union
174  {
175  net_value_type out;
176  double in;
177  } val;
178  val.in = value;
179  return ByteOrder<net_value_type>::hton( val.out);
180  }
181  static double ntoh( const net_value_type& value)
182  {
183  union
184  {
185  double out;
186  net_value_type in;
187  } val;
188  val.in = ByteOrder<net_value_type>::ntoh( value);
189  return val.out;
190  }
191 };
192 
193 } //namespace
194 #endif
195 
int64_t net_value_type
Definition: hton.hpp:122
static int16_t ntoh(const int16_t &value)
Definition: hton.hpp:61
static int8_t ntoh(const int8_t &value)
Definition: hton.hpp:41
static uint16_t hton(const uint16_t &value)
Definition: hton.hpp:50
static int64_t hton(const int64_t &value)
Definition: hton.hpp:124
uint16_t net_value_type
Definition: hton.hpp:48
static uint32_t hton(const uint32_t &value)
Definition: hton.hpp:70
static net_value_type hton(const float &value)
Definition: hton.hpp:143
int8_t net_value_type
Definition: hton.hpp:38
static int8_t hton(const int8_t &value)
Definition: hton.hpp:40
float_net_t net_value_type
Definition: hton.hpp:141
int32_t net_value_type
Definition: hton.hpp:78
static int64_t ntoh(const int64_t &value)
Definition: hton.hpp:128
static float ntoh(const net_value_type &value)
Definition: hton.hpp:153
uint64_t double_net_t
Definition: hton.hpp:135
static int16_t hton(const int16_t &value)
Definition: hton.hpp:60
uint8_t net_value_type
Definition: hton.hpp:28
static uint32_t ntoh(const uint32_t &value)
Definition: hton.hpp:71
int16_t net_value_type
Definition: hton.hpp:58
uint32_t float_net_t
Definition: hton.hpp:134
uint64_t net_value_type
Definition: hton.hpp:88
Definition: hton.hpp:19
double_net_t net_value_type
Definition: hton.hpp:169
static int32_t hton(const int32_t &value)
Definition: hton.hpp:80
static int32_t ntoh(const int32_t &value)
Definition: hton.hpp:81
static net_value_type hton(const double &value)
Definition: hton.hpp:171
static uint8_t ntoh(const uint8_t &value)
Definition: hton.hpp:31
static uint64_t ntoh(const uint64_t &value)
Definition: hton.hpp:101
static uint16_t ntoh(const uint16_t &value)
Definition: hton.hpp:51
static uint8_t hton(const uint8_t &value)
Definition: hton.hpp:30
static uint64_t hton(const uint64_t &value)
Definition: hton.hpp:90
static double ntoh(const net_value_type &value)
Definition: hton.hpp:181
uint32_t net_value_type
Definition: hton.hpp:68