]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSEndianTypes.h
c776332f6ec8ff4fe5990f36553c8c0c8326743c
[apple/xnu.git] / libkern / libkern / c++ / OSEndianTypes.h
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 *
30 * HISTORY
31 * gvdl 20050620 Created
32 */
33
34 /*!
35 @header OSEndianTypes
36 @abstract C++ inline types for byte swapping
37 @discussion
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.
40 @updated 2005-07-25
41 */
42
43 // Header doc magic trick for simple documentation
44 #if 0
45 /*! @typedef BigUInt16
46 @abstract A Big-endian unsigned integer scalar size 16 - UInt16 */
47 typedef class BigUInt16 BigUInt16;
48 #endif
49
50 #if 0
51 /*! @typedef BigSInt16
52 @abstract A Big-endian signed integer scalar size 16 - SInt16 */
53 typedef class BigSInt16 BigSInt16;
54
55 /*! @typedef BigUInt32
56 @abstract A Big-endian unsigned integer scalar size 32 - UInt32 */
57 typedef class BigUInt32 BigUInt32;
58
59 /*! @typedef BigSInt32
60 @abstract A Big-endian signed integer scalar size 32 - SInt32 */
61 typedef class BigSInt32 BigSInt32;
62
63 /*! @typedef BigUInt64
64 @abstract A Big-endian unsigned integer scalar size 64 - UInt64 */
65 typedef class BigUInt64 BigUInt64;
66
67 /*! @typedef BigSInt64
68 @abstract A Big-endian signed integer scalar size 64 - SInt64 */
69 typedef class BigSInt64 BigSInt64;
70
71 /*! @typedef LittleUInt16
72 @abstract A Little-endian unsigned integer scalar size 16 - UInt16 */
73 typedef class LittleUInt16 LittleUInt16;
74
75 /*! @typedef LittleSInt16
76 @abstract A Little-endian signed integer scalar size 16 - SInt16 */
77 typedef class LittleSInt16 LittleSInt16;
78
79 /*! @typedef LittleUInt32
80 @abstract A Little-endian unsigned integer scalar size 32 - UInt32 */
81 typedef class LittleUInt32 LittleUInt32;
82
83 /*! @typedef LittleSInt32
84 @abstract A Little-endian signed integer scalar size 32 - SInt32 */
85 typedef class LittleSInt32 LittleSInt32;
86
87 /*! @typedef LittleUInt64
88 @abstract A Little-endian unsigned integer scalar size 64 - UInt64 */
89 typedef class LittleUInt64 LittleUInt64;
90
91 /*! @typedef LittleSInt64
92 @abstract A Little-endian signed integer scalar size 64 - SInt64 */
93 typedef class LittleSInt64 LittleSInt64;
94
95 */
96 #endif
97
98 #ifndef _OS_OSENDIANHELPER_H
99 #define _OS_OSENDIANHELPER_H
100
101 #if __cplusplus
102
103 #include <libkern/OSTypes.h>
104 #include <libkern/OSByteOrder.h>
105
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) { \
110 public: \
111 typedef argtype ## argsize Value; \
112 \
113 private: \
114 typedef UInt ## argsize UValue; \
115 UValue mValue; \
116 \
117 void writeValue(Value v) { \
118 if (__builtin_constant_p(v)) \
119 mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v); \
120 else \
121 OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);\
122 }; \
123 \
124 Value readValue() const { \
125 return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0); \
126 }; \
127 \
128 public: \
129 argname() { }; \
130 \
131 argname (Value v) { writeValue(v); }; \
132 argname &operator = (Value v) { writeValue(v); return *this; } \
133 \
134 Value get() const { return readValue(); }; \
135 operator Value () const { return readValue(); }; \
136 }
137
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);
150
151 #undef __OSEndianSignIntSizeDEF
152
153 #endif /* __cplusplus */
154
155 #endif /* ! _OS_OSENDIANHELPER_H */
156
157