]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSEndianTypes.h
c427d57430265337604a21ede60d6d6c96fca19d
[apple/xnu.git] / libkern / libkern / c++ / OSEndianTypes.h
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 *
22 * HISTORY
23 * gvdl 20050620 Created
24 */
25
26 /*!
27 @header OSEndianTypes
28 @abstract C++ inline types for byte swapping
29 @discussion
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.
32 @updated 2005-07-25
33 */
34
35 // Header doc magic trick for simple documentation
36 #if 0
37 /*! @typedef BigUInt16
38 @abstract A Big-endian unsigned integer scalar size 16 - UInt16 */
39 typedef class BigUInt16 BigUInt16;
40 #endif
41
42 #if 0
43 /*! @typedef BigSInt16
44 @abstract A Big-endian signed integer scalar size 16 - SInt16 */
45 typedef class BigSInt16 BigSInt16;
46
47 /*! @typedef BigUInt32
48 @abstract A Big-endian unsigned integer scalar size 32 - UInt32 */
49 typedef class BigUInt32 BigUInt32;
50
51 /*! @typedef BigSInt32
52 @abstract A Big-endian signed integer scalar size 32 - SInt32 */
53 typedef class BigSInt32 BigSInt32;
54
55 /*! @typedef BigUInt64
56 @abstract A Big-endian unsigned integer scalar size 64 - UInt64 */
57 typedef class BigUInt64 BigUInt64;
58
59 /*! @typedef BigSInt64
60 @abstract A Big-endian signed integer scalar size 64 - SInt64 */
61 typedef class BigSInt64 BigSInt64;
62
63 /*! @typedef LittleUInt16
64 @abstract A Little-endian unsigned integer scalar size 16 - UInt16 */
65 typedef class LittleUInt16 LittleUInt16;
66
67 /*! @typedef LittleSInt16
68 @abstract A Little-endian signed integer scalar size 16 - SInt16 */
69 typedef class LittleSInt16 LittleSInt16;
70
71 /*! @typedef LittleUInt32
72 @abstract A Little-endian unsigned integer scalar size 32 - UInt32 */
73 typedef class LittleUInt32 LittleUInt32;
74
75 /*! @typedef LittleSInt32
76 @abstract A Little-endian signed integer scalar size 32 - SInt32 */
77 typedef class LittleSInt32 LittleSInt32;
78
79 /*! @typedef LittleUInt64
80 @abstract A Little-endian unsigned integer scalar size 64 - UInt64 */
81 typedef class LittleUInt64 LittleUInt64;
82
83 /*! @typedef LittleSInt64
84 @abstract A Little-endian signed integer scalar size 64 - SInt64 */
85 typedef class LittleSInt64 LittleSInt64;
86
87 */
88 #endif
89
90 #ifndef _OS_OSENDIANHELPER_H
91 #define _OS_OSENDIANHELPER_H
92
93 #if __cplusplus
94
95 #include <libkern/OSTypes.h>
96 #include <libkern/OSByteOrder.h>
97
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) { \
102 public: \
103 typedef argtype ## argsize Value; \
104 \
105 private: \
106 typedef UInt ## argsize UValue; \
107 UValue mValue; \
108 \
109 void writeValue(Value v) { \
110 if (__builtin_constant_p(v)) \
111 mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v); \
112 else \
113 OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);\
114 }; \
115 \
116 Value readValue() const { \
117 return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0); \
118 }; \
119 \
120 public: \
121 argname() { }; \
122 \
123 argname (Value v) { writeValue(v); }; \
124 argname &operator = (Value v) { writeValue(v); return *this; } \
125 \
126 Value get() const { return readValue(); }; \
127 operator Value () const { return readValue(); }; \
128 }
129
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);
142
143 #undef __OSEndianSignIntSizeDEF
144
145 #endif /* __cplusplus */
146
147 #endif /* ! _OS_OSENDIANHELPER_H */
148
149