2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * gvdl 20050620 Created
36 @abstract C++ inline types for byte swapping
38 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.
39 @copyright 2005 Apple Computer, Inc. All rights reserved.
43 // Header doc magic trick for simple documentation
45 /*! @typedef BigUInt16
46 @abstract A Big-endian unsigned integer scalar size 16 - UInt16 */
47 typedef class BigUInt16 BigUInt16
;
51 /*! @typedef BigSInt16
52 @abstract A Big-endian signed integer scalar size 16 - SInt16 */
53 typedef class BigSInt16 BigSInt16
;
55 /*! @typedef BigUInt32
56 @abstract A Big-endian unsigned integer scalar size 32 - UInt32 */
57 typedef class BigUInt32 BigUInt32
;
59 /*! @typedef BigSInt32
60 @abstract A Big-endian signed integer scalar size 32 - SInt32 */
61 typedef class BigSInt32 BigSInt32
;
63 /*! @typedef BigUInt64
64 @abstract A Big-endian unsigned integer scalar size 64 - UInt64 */
65 typedef class BigUInt64 BigUInt64
;
67 /*! @typedef BigSInt64
68 @abstract A Big-endian signed integer scalar size 64 - SInt64 */
69 typedef class BigSInt64 BigSInt64
;
71 /*! @typedef LittleUInt16
72 @abstract A Little-endian unsigned integer scalar size 16 - UInt16 */
73 typedef class LittleUInt16 LittleUInt16
;
75 /*! @typedef LittleSInt16
76 @abstract A Little-endian signed integer scalar size 16 - SInt16 */
77 typedef class LittleSInt16 LittleSInt16
;
79 /*! @typedef LittleUInt32
80 @abstract A Little-endian unsigned integer scalar size 32 - UInt32 */
81 typedef class LittleUInt32 LittleUInt32
;
83 /*! @typedef LittleSInt32
84 @abstract A Little-endian signed integer scalar size 32 - SInt32 */
85 typedef class LittleSInt32 LittleSInt32
;
87 /*! @typedef LittleUInt64
88 @abstract A Little-endian unsigned integer scalar size 64 - UInt64 */
89 typedef class LittleUInt64 LittleUInt64
;
91 /*! @typedef LittleSInt64
92 @abstract A Little-endian signed integer scalar size 64 - SInt64 */
93 typedef class LittleSInt64 LittleSInt64
;
98 #ifndef _OS_OSENDIANHELPER_H
99 #define _OS_OSENDIANHELPER_H
103 #include <libkern/OSTypes.h>
104 #include <libkern/OSByteOrder.h>
106 // Probably should really be using templates, this is one of the few cases
107 // where they do make sense. But as the kernel is not allowed to export
108 // template based C++ APIs we have to use sophisticated macros instead
109 #define __OSEndianSignIntSizeDEF(argname, argend, argtype, argsize) { \
111 typedef argtype ## argsize Value; \
114 typedef UInt ## argsize UValue; \
117 void writeValue(Value v) { \
118 if (__builtin_constant_p(v)) \
119 mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v); \
121 OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);\
124 Value readValue() const { \
125 return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0); \
131 argname (Value v) { writeValue(v); }; \
132 argname &operator = (Value v) { writeValue(v); return *this; } \
134 Value get() const { return readValue(); }; \
135 operator Value () const { return readValue(); }; \
138 class BigUInt16
__OSEndianSignIntSizeDEF(BigUInt16
, Big
, UInt
, 16);
139 class BigSInt16
__OSEndianSignIntSizeDEF(BigSInt16
, Big
, SInt
, 16);
140 class BigUInt32
__OSEndianSignIntSizeDEF(BigUInt32
, Big
, UInt
, 32);
141 class BigSInt32
__OSEndianSignIntSizeDEF(BigSInt32
, Big
, SInt
, 32);
142 class BigUInt64
__OSEndianSignIntSizeDEF(BigUInt64
, Big
, UInt
, 64);
143 class BigSInt64
__OSEndianSignIntSizeDEF(BigSInt64
, Big
, SInt
, 64);
144 class LittleUInt16
__OSEndianSignIntSizeDEF(LittleUInt16
, Little
, UInt
, 16);
145 class LittleSInt16
__OSEndianSignIntSizeDEF(LittleSInt16
, Little
, SInt
, 16);
146 class LittleUInt32
__OSEndianSignIntSizeDEF(LittleUInt32
, Little
, UInt
, 32);
147 class LittleSInt32
__OSEndianSignIntSizeDEF(LittleSInt32
, Little
, SInt
, 32);
148 class LittleUInt64
__OSEndianSignIntSizeDEF(LittleUInt64
, Little
, UInt
, 64);
149 class LittleSInt64
__OSEndianSignIntSizeDEF(LittleSInt64
, Little
, SInt
, 64);
151 #undef __OSEndianSignIntSizeDEF
153 #endif /* __cplusplus */
155 #endif /* ! _OS_OSENDIANHELPER_H */