~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/include/Poco/ByteOrder.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ByteOrder.h
 
3
//
 
4
// $Id: //poco/1.2/Foundation/include/Poco/ByteOrder.h#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Core
 
8
// Module:  ByteOrder
 
9
//
 
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
13
// Permission is hereby granted, free of charge, to any person or organization
 
14
// obtaining a copy of the software and accompanying documentation covered by
 
15
// this license (the "Software") to use, reproduce, display, distribute,
 
16
// execute, and transmit the Software, and to prepare derivative works of the
 
17
// Software, and to permit third-parties to whom the Software is furnished to
 
18
// do so, all subject to the following:
 
19
// 
 
20
// The copyright notices in the Software and this entire statement, including
 
21
// the above license grant, this restriction and the following disclaimer,
 
22
// must be included in all copies of the Software, in whole or in part, and
 
23
// all derivative works of the Software, unless such copies or derivative
 
24
// works are solely in the form of machine-executable object code generated by
 
25
// a source language processor.
 
26
// 
 
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
33
// DEALINGS IN THE SOFTWARE.
 
34
//
 
35
 
 
36
 
 
37
#ifndef Foundation_ByteOrder_INCLUDED
 
38
#define Foundation_ByteOrder_INCLUDED
 
39
 
 
40
 
 
41
#include "Poco/Foundation.h"
 
42
#include "Poco/Types.h"
 
43
 
 
44
 
 
45
namespace Poco {
 
46
 
 
47
 
 
48
class Foundation_API ByteOrder
 
49
        /// This class contains a number of static methods
 
50
        /// to convert between big-endian and little-endian
 
51
        /// integers of various sizes.
 
52
{
 
53
public:
 
54
        static Int16 flipBytes(Int16 value);
 
55
        static UInt16 flipBytes(UInt16 value);
 
56
        static Int32 flipBytes(Int32 value);
 
57
        static UInt32 flipBytes(UInt32 value);
 
58
#if defined(POCO_HAVE_INT64)
 
59
        static Int64 flipBytes(Int64 value);
 
60
        static UInt64 flipBytes(UInt64 value);
 
61
#endif
 
62
 
 
63
        static Int16 toBigEndian(Int16 value);
 
64
        static UInt16 toBigEndian (UInt16 value);
 
65
        static Int32 toBigEndian(Int32 value);
 
66
        static UInt32 toBigEndian (UInt32 value);
 
67
#if defined(POCO_HAVE_INT64)
 
68
        static Int64 toBigEndian(Int64 value);
 
69
        static UInt64 toBigEndian (UInt64 value);
 
70
#endif
 
71
 
 
72
        static Int16 fromBigEndian(Int16 value);
 
73
        static UInt16 fromBigEndian (UInt16 value);
 
74
        static Int32 fromBigEndian(Int32 value);
 
75
        static UInt32 fromBigEndian (UInt32 value);
 
76
#if defined(POCO_HAVE_INT64)
 
77
        static Int64 fromBigEndian(Int64 value);
 
78
        static UInt64 fromBigEndian (UInt64 value);
 
79
#endif
 
80
 
 
81
        static Int16 toLittleEndian(Int16 value);
 
82
        static UInt16 toLittleEndian (UInt16 value);
 
83
        static Int32 toLittleEndian(Int32 value);
 
84
        static UInt32 toLittleEndian (UInt32 value);
 
85
#if defined(POCO_HAVE_INT64)
 
86
        static Int64 toLittleEndian(Int64 value);
 
87
        static UInt64 toLittleEndian (UInt64 value);
 
88
#endif
 
89
 
 
90
        static Int16 fromLittleEndian(Int16 value);
 
91
        static UInt16 fromLittleEndian (UInt16 value);
 
92
        static Int32 fromLittleEndian(Int32 value);
 
93
        static UInt32 fromLittleEndian (UInt32 value);
 
94
#if defined(POCO_HAVE_INT64)
 
95
        static Int64 fromLittleEndian(Int64 value);
 
96
        static UInt64 fromLittleEndian (UInt64 value);
 
97
#endif
 
98
 
 
99
        static Int16 toNetwork(Int16 value);
 
100
        static UInt16 toNetwork (UInt16 value);
 
101
        static Int32 toNetwork(Int32 value);
 
102
        static UInt32 toNetwork (UInt32 value);
 
103
#if defined(POCO_HAVE_INT64)
 
104
        static Int64 toNetwork(Int64 value);
 
105
        static UInt64 toNetwork (UInt64 value);
 
106
#endif
 
107
 
 
108
        static Int16 fromNetwork(Int16 value);
 
109
        static UInt16 fromNetwork (UInt16 value);
 
110
        static Int32 fromNetwork(Int32 value);
 
111
        static UInt32 fromNetwork (UInt32 value);
 
112
#if defined(POCO_HAVE_INT64)
 
113
        static Int64 fromNetwork(Int64 value);
 
114
        static UInt64 fromNetwork (UInt64 value);
 
115
#endif
 
116
};
 
117
 
 
118
 
 
119
//
 
120
// inlines
 
121
//
 
122
inline UInt16 ByteOrder::flipBytes(UInt16 value)
 
123
{
 
124
        return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00);
 
125
}
 
126
 
 
127
 
 
128
inline Int16 ByteOrder::flipBytes(Int16 value)
 
129
{
 
130
        return Int16(flipBytes(UInt16(value)));
 
131
}
 
132
 
 
133
 
 
134
inline UInt32 ByteOrder::flipBytes(UInt32 value)
 
135
{
 
136
        return ((value >> 24) & 0x000000FF) | ((value >> 8) & 0x0000FF00)
 
137
             | ((value << 8) & 0x00FF0000) | ((value << 24) & 0xFF000000);
 
138
}
 
139
 
 
140
 
 
141
inline Int32 ByteOrder::flipBytes(Int32 value)
 
142
{
 
143
        return Int32(flipBytes(UInt32(value)));
 
144
}
 
145
 
 
146
 
 
147
#if defined(POCO_HAVE_INT64)
 
148
inline UInt64 ByteOrder::flipBytes(UInt64 value)
 
149
{
 
150
        UInt32 hi = UInt32(value >> 32);
 
151
        UInt32 lo = UInt32(value & 0xFFFFFFFF);
 
152
        return UInt64(flipBytes(hi)) | (UInt64(flipBytes(lo)) << 32);
 
153
}
 
154
 
 
155
 
 
156
inline Int64 ByteOrder::flipBytes(Int64 value)
 
157
{
 
158
        return Int64(flipBytes(UInt64(value)));
 
159
}
 
160
#endif // POCO_HAVE_INT64
 
161
 
 
162
 
 
163
//
 
164
// some macro trickery to automate the method implementation
 
165
//
 
166
#define POCO_IMPLEMENT_BYTEORDER_NOOP_(op, type) \
 
167
        inline type ByteOrder::op(type value)           \
 
168
        {                                                                                       \
 
169
                return value;                                                   \
 
170
        }
 
171
#define POCO_IMPLEMENT_BYTEORDER_FLIP_(op, type) \
 
172
        inline type ByteOrder::op(type value)           \
 
173
        {                                                                                       \
 
174
                return flipBytes(value);                                \
 
175
        }
 
176
 
 
177
 
 
178
#if defined(POCO_HAVE_INT64)
 
179
        #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
 
180
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16)       \
 
181
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16)      \
 
182
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32)       \
 
183
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32)      \
 
184
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int64)       \
 
185
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt64)
 
186
        #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
 
187
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16)       \
 
188
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16)      \
 
189
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32)       \
 
190
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32)      \
 
191
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int64)       \
 
192
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt64)
 
193
#else
 
194
        #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
 
195
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16)       \
 
196
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16)      \
 
197
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32)       \
 
198
                POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32)
 
199
        #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
 
200
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16)       \
 
201
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16)      \
 
202
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32)       \
 
203
                POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32)
 
204
#endif
 
205
 
 
206
 
 
207
#if defined(POCO_ARCH_BIG_ENDIAN)
 
208
        #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_NOOP
 
209
        #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_FLIP
 
210
#else
 
211
        #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_FLIP
 
212
        #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_NOOP
 
213
#endif
 
214
 
 
215
 
 
216
POCO_IMPLEMENT_BYTEORDER_BIG(toBigEndian)
 
217
POCO_IMPLEMENT_BYTEORDER_BIG(fromBigEndian)
 
218
POCO_IMPLEMENT_BYTEORDER_BIG(toNetwork)
 
219
POCO_IMPLEMENT_BYTEORDER_BIG(fromNetwork)
 
220
POCO_IMPLEMENT_BYTEORDER_LIT(toLittleEndian)
 
221
POCO_IMPLEMENT_BYTEORDER_LIT(fromLittleEndian)
 
222
 
 
223
 
 
224
} // namespace Poco
 
225
 
 
226
 
 
227
#endif // Foundation_ByteOrder_INCLUDED