xref: /xnu-11417.140.69/libkern/libkern/c++/OSEndianTypes.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_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 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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  *
28  * HISTORY
29  *   gvdl    20050620    Created
30  */
31 
32 // xx-review: are these even used anywhere? Grep turns up squat.
33 
34 
35 /*!
36  * @header OSEndianTypes
37  *
38  * @abstract
39  * C++ inline types for byte-swapping.
40  *
41  * @discussion
42  * The OSEndianTypes consist of a number of types that are used
43  * very similarly to the traditional MacOS C scalar integers types,
44  * for example, <code>UInt32</code> and <code>SInt32</code>.
45  * @copyright 2005 Apple Computer, Inc. All rights reserved.
46  * @updated 2005-07-25
47  */
48 
49 // Header doc magic trick for simple documentation
50 #if 0
51 /*!
52  * @typedef BigUInt16
53  * @abstract A Big-endian unsigned integer scalar size 16 - UInt16
54  */
55 typedef class BigUInt16 BigUInt16;
56 
57 /*!
58  * @typedef BigSInt16
59  * @abstract A Big-endian signed integer scalar size 16 - SInt16
60  */
61 typedef class BigSInt16 BigSInt16;
62 
63 /*!
64  * @typedef BigUInt32
65  * @abstract A Big-endian unsigned integer scalar size 32 - UInt32
66  */
67 typedef class BigUInt32 BigUInt32;
68 
69 /*!
70  * @typedef BigSInt32
71  * @abstract A Big-endian signed integer scalar size 32 - SInt32
72  */
73 typedef class BigSInt32 BigSInt32;
74 
75 /*!
76  * @typedef BigUInt64
77  * @abstract A Big-endian unsigned integer scalar size 64 - UInt64
78  */
79 typedef class BigUInt64 BigUInt64;
80 
81 /*!
82  * @typedef BigSInt64
83  * @abstract A Big-endian signed integer scalar size 64 - SInt64
84  */
85 typedef class BigSInt64 BigSInt64;
86 
87 /*!
88  * @typedef LittleUInt16
89  * @abstract A Little-endian unsigned integer scalar size 16 - UInt16
90  */
91 typedef class LittleUInt16 LittleUInt16;
92 
93 /*!
94  * @typedef LittleSInt16
95  * @abstract A Little-endian signed integer scalar size 16 - SInt16
96  */
97 typedef class LittleSInt16 LittleSInt16;
98 
99 /*!
100  * @typedef LittleUInt32
101  * @abstract A Little-endian unsigned integer scalar size 32 - UInt32
102  */
103 typedef class LittleUInt32 LittleUInt32;
104 
105 /*!
106  * @typedef LittleSInt32
107  * @abstract A Little-endian signed integer scalar size 32 - SInt32
108  */
109 typedef class LittleSInt32 LittleSInt32;
110 
111 /*!
112  * @typedef LittleUInt64
113  * @abstract A Little-endian unsigned integer scalar size 64 - UInt64
114  */
115 typedef class LittleUInt64 LittleUInt64;
116 
117 /*!
118  * @typedef LittleSInt64
119  * @abstract A Little-endian signed integer scalar size 64 - SInt64
120  */
121 typedef class LittleSInt64 LittleSInt64;
122 
123 #endif /* 0 - headerdoc trick */
124 
125 #ifndef _OS_OSENDIANHELPER_H
126 #define _OS_OSENDIANHELPER_H
127 
128 #if __cplusplus
129 
130 #include <libkern/OSTypes.h>
131 #include <libkern/OSByteOrder.h>
132 
133 // Probably should really be using templates, this is one of the few cases
134 // where they do make sense.  But as the kernel is not allowed to export
135 // template based C++ APIs we have to use sophisticated macros instead
136 #define __OSEndianSignIntSizeDEF(argname, argend, argtype, argsize) {  \
137 public:                                                                \
138     typedef argtype ## argsize        Value;                           \
139                                                                        \
140 private:                                                               \
141     typedef UInt ## argsize        UValue;                             \
142     UValue mValue;                                                     \
143                                                                        \
144     void writeValue(Value v) {                                         \
145     if (__builtin_constant_p(v))                                       \
146 	mValue = OSSwapHostTo ## argend ## ConstInt ## argsize(v);     \
147     else                                                               \
148 	OSWrite ## argend ## Int ## argsize(&mValue, 0, (UValue) v);   \
149     };                                                                 \
150                                                                        \
151     Value readValue() const {                                          \
152     return (Value) OSRead ## argend ## Int ## argsize(&mValue, 0);     \
153     };                                                                 \
154                                                                        \
155 public:                                                                \
156     argname() { };                                                     \
157                                                                        \
158     argname (Value v) { writeValue(v); };                              \
159     argname  &operator = (Value v) { writeValue(v); return *this; }    \
160                                                                        \
161     Value get() const { return readValue(); };                         \
162     operator Value () const { return readValue(); };                   \
163 }
164 
165 class BigUInt16    __OSEndianSignIntSizeDEF(BigUInt16, Big, UInt, 16);
166 class BigSInt16    __OSEndianSignIntSizeDEF(BigSInt16, Big, SInt, 16);
167 class BigUInt32    __OSEndianSignIntSizeDEF(BigUInt32, Big, UInt, 32);
168 class BigSInt32    __OSEndianSignIntSizeDEF(BigSInt32, Big, SInt, 32);
169 class BigUInt64    __OSEndianSignIntSizeDEF(BigUInt64, Big, UInt, 64);
170 class BigSInt64    __OSEndianSignIntSizeDEF(BigSInt64, Big, SInt, 64);
171 class LittleUInt16 __OSEndianSignIntSizeDEF(LittleUInt16, Little, UInt, 16);
172 class LittleSInt16 __OSEndianSignIntSizeDEF(LittleSInt16, Little, SInt, 16);
173 class LittleUInt32 __OSEndianSignIntSizeDEF(LittleUInt32, Little, UInt, 32);
174 class LittleSInt32 __OSEndianSignIntSizeDEF(LittleSInt32, Little, SInt, 32);
175 class LittleUInt64 __OSEndianSignIntSizeDEF(LittleUInt64, Little, UInt, 64);
176 class LittleSInt64 __OSEndianSignIntSizeDEF(LittleSInt64, Little, SInt, 64);
177 
178 #undef __OSEndianSignIntSizeDEF
179 
180 #endif /* __cplusplus
181         */
182 
183 #endif /* ! _OS_OSENDIANHELPER_H
184         */
185