2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * gvdl 20050620 Created
28 @abstract C++ inline types for byte swapping
30 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.
31 @copyright 2005 Apple Computer, Inc. All rights reserved.
35 // Header doc magic trick for simple documentation
37 /*! @typedef BigUInt16
38 @abstract A Big-endian unsigned integer scalar size 16 - UInt16 */
39 typedef class BigUInt16 BigUInt16
;
43 /*! @typedef BigSInt16
44 @abstract A Big-endian signed integer scalar size 16 - SInt16 */
45 typedef class BigSInt16 BigSInt16
;
47 /*! @typedef BigUInt32
48 @abstract A Big-endian unsigned integer scalar size 32 - UInt32 */
49 typedef class BigUInt32 BigUInt32
;
51 /*! @typedef BigSInt32
52 @abstract A Big-endian signed integer scalar size 32 - SInt32 */
53 typedef class BigSInt32 BigSInt32
;
55 /*! @typedef BigUInt64
56 @abstract A Big-endian unsigned integer scalar size 64 - UInt64 */
57 typedef class BigUInt64 BigUInt64
;
59 /*! @typedef BigSInt64
60 @abstract A Big-endian signed integer scalar size 64 - SInt64 */
61 typedef class BigSInt64 BigSInt64
;
63 /*! @typedef LittleUInt16
64 @abstract A Little-endian unsigned integer scalar size 16 - UInt16 */
65 typedef class LittleUInt16 LittleUInt16
;
67 /*! @typedef LittleSInt16
68 @abstract A Little-endian signed integer scalar size 16 - SInt16 */
69 typedef class LittleSInt16 LittleSInt16
;
71 /*! @typedef LittleUInt32
72 @abstract A Little-endian unsigned integer scalar size 32 - UInt32 */
73 typedef class LittleUInt32 LittleUInt32
;
75 /*! @typedef LittleSInt32
76 @abstract A Little-endian signed integer scalar size 32 - SInt32 */
77 typedef class LittleSInt32 LittleSInt32
;
79 /*! @typedef LittleUInt64
80 @abstract A Little-endian unsigned integer scalar size 64 - UInt64 */
81 typedef class LittleUInt64 LittleUInt64
;
83 /*! @typedef LittleSInt64
84 @abstract A Little-endian signed integer scalar size 64 - SInt64 */
85 typedef class LittleSInt64 LittleSInt64
;
90 #ifndef _OS_OSENDIANHELPER_H
91 #define _OS_OSENDIANHELPER_H
95 #include <libkern/OSTypes.h>
96 #include <libkern/OSByteOrder.h>
98 // Probably should really be using templates, this is one of the few cases
99 // where they do make sense. But as the kernel is not allowed to export
100 // template based C++ APIs we have to use sophisticated macros instead
101 #define __OSEndianSignIntSizeDEF(argname, argend, argtype, argsize) { \
103 typedef argtype ## argsize Value; \
106 typedef UInt ## argsize UValue; \
109 void writeValue(Value v) { \
110 if (__builtin_constant_p(v)) \
111 mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v); \
113 OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);\
116 Value readValue() const { \
117 return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0); \
123 argname (Value v) { writeValue(v); }; \
124 argname &operator = (Value v) { writeValue(v); return *this; } \
126 Value get() const { return readValue(); }; \
127 operator Value () const { return readValue(); }; \
130 class BigUInt16
__OSEndianSignIntSizeDEF(BigUInt16
, Big
, UInt
, 16);
131 class BigSInt16
__OSEndianSignIntSizeDEF(BigSInt16
, Big
, SInt
, 16);
132 class BigUInt32
__OSEndianSignIntSizeDEF(BigUInt32
, Big
, UInt
, 32);
133 class BigSInt32
__OSEndianSignIntSizeDEF(BigSInt32
, Big
, SInt
, 32);
134 class BigUInt64
__OSEndianSignIntSizeDEF(BigUInt64
, Big
, UInt
, 64);
135 class BigSInt64
__OSEndianSignIntSizeDEF(BigSInt64
, Big
, SInt
, 64);
136 class LittleUInt16
__OSEndianSignIntSizeDEF(LittleUInt16
, Little
, UInt
, 16);
137 class LittleSInt16
__OSEndianSignIntSizeDEF(LittleSInt16
, Little
, SInt
, 16);
138 class LittleUInt32
__OSEndianSignIntSizeDEF(LittleUInt32
, Little
, UInt
, 32);
139 class LittleSInt32
__OSEndianSignIntSizeDEF(LittleSInt32
, Little
, SInt
, 32);
140 class LittleUInt64
__OSEndianSignIntSizeDEF(LittleUInt64
, Little
, UInt
, 64);
141 class LittleSInt64
__OSEndianSignIntSizeDEF(LittleSInt64
, Little
, SInt
, 64);
143 #undef __OSEndianSignIntSizeDEF
145 #endif /* __cplusplus */
147 #endif /* ! _OS_OSENDIANHELPER_H */