ByteOrder.h
Go to the documentation of this file.
00001 //
00002 // ByteOrder.h
00003 //
00004 // $Id: //poco/1.3/Foundation/include/Poco/ByteOrder.h#1 $
00005 //
00006 // Library: Foundation
00007 // Package: Core
00008 // Module:  ByteOrder
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #ifndef Foundation_ByteOrder_INCLUDED
00038 #define Foundation_ByteOrder_INCLUDED
00039 
00040 
00041 #include "Poco/Foundation.h"
00042 #include "Poco/Types.h"
00043 
00044 
00045 namespace Poco {
00046 
00047 
00048 class Foundation_API ByteOrder
00052 {
00053 public:
00054         static Int16 flipBytes(Int16 value);
00055         static UInt16 flipBytes(UInt16 value);
00056         static Int32 flipBytes(Int32 value);
00057         static UInt32 flipBytes(UInt32 value);
00058 #if defined(POCO_HAVE_INT64)
00059         static Int64 flipBytes(Int64 value);
00060         static UInt64 flipBytes(UInt64 value);
00061 #endif
00062 
00063         static Int16 toBigEndian(Int16 value);
00064         static UInt16 toBigEndian (UInt16 value);
00065         static Int32 toBigEndian(Int32 value);
00066         static UInt32 toBigEndian (UInt32 value);
00067 #if defined(POCO_HAVE_INT64)
00068         static Int64 toBigEndian(Int64 value);
00069         static UInt64 toBigEndian (UInt64 value);
00070 #endif
00071 
00072         static Int16 fromBigEndian(Int16 value);
00073         static UInt16 fromBigEndian (UInt16 value);
00074         static Int32 fromBigEndian(Int32 value);
00075         static UInt32 fromBigEndian (UInt32 value);
00076 #if defined(POCO_HAVE_INT64)
00077         static Int64 fromBigEndian(Int64 value);
00078         static UInt64 fromBigEndian (UInt64 value);
00079 #endif
00080 
00081         static Int16 toLittleEndian(Int16 value);
00082         static UInt16 toLittleEndian (UInt16 value);
00083         static Int32 toLittleEndian(Int32 value);
00084         static UInt32 toLittleEndian (UInt32 value);
00085 #if defined(POCO_HAVE_INT64)
00086         static Int64 toLittleEndian(Int64 value);
00087         static UInt64 toLittleEndian (UInt64 value);
00088 #endif
00089 
00090         static Int16 fromLittleEndian(Int16 value);
00091         static UInt16 fromLittleEndian (UInt16 value);
00092         static Int32 fromLittleEndian(Int32 value);
00093         static UInt32 fromLittleEndian (UInt32 value);
00094 #if defined(POCO_HAVE_INT64)
00095         static Int64 fromLittleEndian(Int64 value);
00096         static UInt64 fromLittleEndian (UInt64 value);
00097 #endif
00098 
00099         static Int16 toNetwork(Int16 value);
00100         static UInt16 toNetwork (UInt16 value);
00101         static Int32 toNetwork(Int32 value);
00102         static UInt32 toNetwork (UInt32 value);
00103 #if defined(POCO_HAVE_INT64)
00104         static Int64 toNetwork(Int64 value);
00105         static UInt64 toNetwork (UInt64 value);
00106 #endif
00107 
00108         static Int16 fromNetwork(Int16 value);
00109         static UInt16 fromNetwork (UInt16 value);
00110         static Int32 fromNetwork(Int32 value);
00111         static UInt32 fromNetwork (UInt32 value);
00112 #if defined(POCO_HAVE_INT64)
00113         static Int64 fromNetwork(Int64 value);
00114         static UInt64 fromNetwork (UInt64 value);
00115 #endif
00116 };
00117 
00118 
00119 //
00120 // inlines
00121 //
00122 inline UInt16 ByteOrder::flipBytes(UInt16 value)
00123 {
00124         return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00);
00125 }
00126 
00127 
00128 inline Int16 ByteOrder::flipBytes(Int16 value)
00129 {
00130         return Int16(flipBytes(UInt16(value)));
00131 }
00132 
00133 
00134 inline UInt32 ByteOrder::flipBytes(UInt32 value)
00135 {
00136         return ((value >> 24) & 0x000000FF) | ((value >> 8) & 0x0000FF00)
00137              | ((value << 8) & 0x00FF0000) | ((value << 24) & 0xFF000000);
00138 }
00139 
00140 
00141 inline Int32 ByteOrder::flipBytes(Int32 value)
00142 {
00143         return Int32(flipBytes(UInt32(value)));
00144 }
00145 
00146 
00147 #if defined(POCO_HAVE_INT64)
00148 inline UInt64 ByteOrder::flipBytes(UInt64 value)
00149 {
00150         UInt32 hi = UInt32(value >> 32);
00151         UInt32 lo = UInt32(value & 0xFFFFFFFF);
00152         return UInt64(flipBytes(hi)) | (UInt64(flipBytes(lo)) << 32);
00153 }
00154 
00155 
00156 inline Int64 ByteOrder::flipBytes(Int64 value)
00157 {
00158         return Int64(flipBytes(UInt64(value)));
00159 }
00160 #endif // POCO_HAVE_INT64
00161 
00162 
00163 //
00164 // some macro trickery to automate the method implementation
00165 //
00166 #define POCO_IMPLEMENT_BYTEORDER_NOOP_(op, type) \
00167         inline type ByteOrder::op(type value)           \
00168         {                                                                                       \
00169                 return value;                                                   \
00170         }
00171 #define POCO_IMPLEMENT_BYTEORDER_FLIP_(op, type) \
00172         inline type ByteOrder::op(type value)           \
00173         {                                                                                       \
00174                 return flipBytes(value);                                \
00175         }
00176 
00177 
00178 #if defined(POCO_HAVE_INT64)
00179         #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
00180                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16)       \
00181                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16)      \
00182                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32)       \
00183                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32)      \
00184                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int64)       \
00185                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt64)
00186         #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
00187                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16)       \
00188                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16)      \
00189                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32)       \
00190                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32)      \
00191                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int64)       \
00192                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt64)
00193 #else
00194         #define POCO_IMPLEMENT_BYTEORDER_NOOP(op) \
00195                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int16)       \
00196                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt16)      \
00197                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, Int32)       \
00198                 POCO_IMPLEMENT_BYTEORDER_NOOP_(op, UInt32)
00199         #define POCO_IMPLEMENT_BYTEORDER_FLIP(op) \
00200                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int16)       \
00201                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt16)      \
00202                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, Int32)       \
00203                 POCO_IMPLEMENT_BYTEORDER_FLIP_(op, UInt32)
00204 #endif
00205 
00206 
00207 #if defined(POCO_ARCH_BIG_ENDIAN)
00208         #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_NOOP
00209         #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_FLIP
00210 #else
00211         #define POCO_IMPLEMENT_BYTEORDER_BIG POCO_IMPLEMENT_BYTEORDER_FLIP
00212         #define POCO_IMPLEMENT_BYTEORDER_LIT POCO_IMPLEMENT_BYTEORDER_NOOP
00213 #endif
00214 
00215 
00216 POCO_IMPLEMENT_BYTEORDER_BIG(toBigEndian)
00217 POCO_IMPLEMENT_BYTEORDER_BIG(fromBigEndian)
00218 POCO_IMPLEMENT_BYTEORDER_BIG(toNetwork)
00219 POCO_IMPLEMENT_BYTEORDER_BIG(fromNetwork)
00220 POCO_IMPLEMENT_BYTEORDER_LIT(toLittleEndian)
00221 POCO_IMPLEMENT_BYTEORDER_LIT(fromLittleEndian)
00222 
00223 
00224 } // namespace Poco
00225 
00226 
00227 #endif // Foundation_ByteOrder_INCLUDED


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19