2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * gvdl 20050620 Created
34 @abstract C++ inline types for byte swapping
36 The OSEndianTypes consist of a number of types that are used very similarly to the traditional MacOS C scalar integers types, eg. UInt32 and SInt32.
37 @copyright 2005 Apple Computer, Inc. All rights reserved.
41 // Header doc magic trick for simple documentation
43 /*! @typedef BigUInt16
44 @abstract A Big-endian unsigned integer scalar size 16 - UInt16 */
45 typedef class BigUInt16 BigUInt16
;
49 /*! @typedef BigSInt16
50 @abstract A Big-endian signed integer scalar size 16 - SInt16 */
51 typedef class BigSInt16 BigSInt16
;
53 /*! @typedef BigUInt32
54 @abstract A Big-endian unsigned integer scalar size 32 - UInt32 */
55 typedef class BigUInt32 BigUInt32
;
57 /*! @typedef BigSInt32
58 @abstract A Big-endian signed integer scalar size 32 - SInt32 */
59 typedef class BigSInt32 BigSInt32
;
61 /*! @typedef BigUInt64
62 @abstract A Big-endian unsigned integer scalar size 64 - UInt64 */
63 typedef class BigUInt64 BigUInt64
;
65 /*! @typedef BigSInt64
66 @abstract A Big-endian signed integer scalar size 64 - SInt64 */
67 typedef class BigSInt64 BigSInt64
;
69 /*! @typedef LittleUInt16
70 @abstract A Little-endian unsigned integer scalar size 16 - UInt16 */
71 typedef class LittleUInt16 LittleUInt16
;
73 /*! @typedef LittleSInt16
74 @abstract A Little-endian signed integer scalar size 16 - SInt16 */
75 typedef class LittleSInt16 LittleSInt16
;
77 /*! @typedef LittleUInt32
78 @abstract A Little-endian unsigned integer scalar size 32 - UInt32 */
79 typedef class LittleUInt32 LittleUInt32
;
81 /*! @typedef LittleSInt32
82 @abstract A Little-endian signed integer scalar size 32 - SInt32 */
83 typedef class LittleSInt32 LittleSInt32
;
85 /*! @typedef LittleUInt64
86 @abstract A Little-endian unsigned integer scalar size 64 - UInt64 */
87 typedef class LittleUInt64 LittleUInt64
;
89 /*! @typedef LittleSInt64
90 @abstract A Little-endian signed integer scalar size 64 - SInt64 */
91 typedef class LittleSInt64 LittleSInt64
;
96 #ifndef _OS_OSENDIANHELPER_H
97 #define _OS_OSENDIANHELPER_H
101 #include <libkern/OSTypes.h>
102 #include <libkern/OSByteOrder.h>
104 // Probably should really be using templates, this is one of the few cases
105 // where they do make sense. But as the kernel is not allowed to export
106 // template based C++ APIs we have to use sophisticated macros instead
107 #define __OSEndianSignIntSizeDEF(argname, argend, argtype, argsize) { \
109 typedef argtype ## argsize Value; \
112 typedef UInt ## argsize UValue; \
115 void writeValue(Value v) { \
116 if (__builtin_constant_p(v)) \
117 mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v); \
119 OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);\
122 Value readValue() const { \
123 return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0); \
129 argname (Value v) { writeValue(v); }; \
130 argname &operator = (Value v) { writeValue(v); return *this; } \
132 Value get() const { return readValue(); }; \
133 operator Value () const { return readValue(); }; \
136 class BigUInt16
__OSEndianSignIntSizeDEF(BigUInt16
, Big
, UInt
, 16);
137 class BigSInt16
__OSEndianSignIntSizeDEF(BigSInt16
, Big
, SInt
, 16);
138 class BigUInt32
__OSEndianSignIntSizeDEF(BigUInt32
, Big
, UInt
, 32);
139 class BigSInt32
__OSEndianSignIntSizeDEF(BigSInt32
, Big
, SInt
, 32);
140 class BigUInt64
__OSEndianSignIntSizeDEF(BigUInt64
, Big
, UInt
, 64);
141 class BigSInt64
__OSEndianSignIntSizeDEF(BigSInt64
, Big
, SInt
, 64);
142 class LittleUInt16
__OSEndianSignIntSizeDEF(LittleUInt16
, Little
, UInt
, 16);
143 class LittleSInt16
__OSEndianSignIntSizeDEF(LittleSInt16
, Little
, SInt
, 16);
144 class LittleUInt32
__OSEndianSignIntSizeDEF(LittleUInt32
, Little
, UInt
, 32);
145 class LittleSInt32
__OSEndianSignIntSizeDEF(LittleSInt32
, Little
, SInt
, 32);
146 class LittleUInt64
__OSEndianSignIntSizeDEF(LittleUInt64
, Little
, UInt
, 64);
147 class LittleSInt64
__OSEndianSignIntSizeDEF(LittleSInt64
, Little
, SInt
, 64);
149 #undef __OSEndianSignIntSizeDEF
151 #endif /* __cplusplus */
153 #endif /* ! _OS_OSENDIANHELPER_H */