メインページ
モジュール
ネームスペース
データ構造
ファイル
ファイル一覧
グローバル
mist
config
endian.h
説明を見る。
1
//
2
// Copyright (c) 2003-2011, MIST Project, Nagoya University
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
// 1. Redistributions of source code must retain the above copyright notice,
9
// this list of conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright notice,
12
// this list of conditions and the following disclaimer in the documentation
13
// and/or other materials provided with the distribution.
14
//
15
// 3. Neither the name of the Nagoya University nor the names of its contributors
16
// may be used to endorse or promote products derived from this software
17
// without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
//
28
33
34
#ifndef __INCLUDE_MIST_ENDIAN__
35
#define __INCLUDE_MIST_ENDIAN__
36
37
#ifndef __INCLUDE_MIST_CONF_H__
38
#include "
mist_conf.h
"
39
#endif
40
41
42
// mist名前空間の始まり
43
_MIST_BEGIN
44
45
53
54
55
56
63
template
<
class
T >
64
union
byte_array
65
{
66
public
:
67
typedef
T
value_type
;
68
typedef
size_t
size_type
;
69
70
private
:
71
value_type
value;
72
unsigned
char
byte[
sizeof
(
value_type
) ];
73
74
public
:
76
byte_array
( ) : value( 0 ){ }
77
78
80
byte_array
(
const
value_type
v ) : value( v ){ }
81
82
84
byte_array
(
const
byte_array
&v ) : value( v.value ){ }
85
86
91
byte_array
(
const
unsigned
char
*b )
92
{
93
for
(
size_type
i = 0 ; i <
sizeof
(
value_type
) ; i++ )
94
{
95
byte[ i ] = b[ i ];
96
}
97
}
98
100
size_type
length
( )
const
{
return
(
sizeof
(
value_type
) ); }
101
103
unsigned
char
&
operator[]
(
size_type
index ){
return
( byte[ index ] ); }
104
106
const
unsigned
char
&
operator[]
(
size_type
index )
const
{
return
( byte[ index ] ); }
107
109
const
value_type
get_value
( )
const
{
return
( value ); }
110
112
value_type
set_value
(
const
value_type
&v ) {
return
( value = v ); }
113
115
const
unsigned
char
*
get_bytes
( )
const
{
return
( byte ); }
116
};
117
118
124
inline
bool
_is_little_endian_
( )
125
{
126
return
(
byte_array< unsigned short >
( 1 )[ 0 ] == 1 );
127
}
128
129
137
inline
bool
_is_big_endian_
( )
138
{
139
return
(
byte_array< unsigned short >
( 1 )[ 0 ] == 0 );
140
}
141
142
147
template
<
class
T >
148
inline
void
swap_bytes
(
byte_array< T >
&bytes )
149
{
150
typedef
typename
byte_array< T >::size_type
size_type;
151
byte_array< T >
tmp( bytes );
152
for
( size_type i = 0 ; i <
sizeof
( T ) ; ++i )
153
{
154
bytes[ i ] = tmp[
sizeof
( T ) - i - 1 ];
155
}
156
}
157
158
170
template
<
class
T >
171
inline
byte_array< T >
to_current_endian
(
const
byte_array< T >
&bytes,
bool
from_little_endian )
172
{
173
static
bool
current_endian =
_is_little_endian_
( );
174
if
( current_endian != from_little_endian )
175
{
176
byte_array< T >
tmp( bytes );
177
swap_bytes
( tmp );
178
return
( tmp );
179
}
180
else
181
{
182
return
( bytes );
183
}
184
}
185
186
198
template
<
class
T >
199
inline
byte_array< T >
from_current_endian
(
const
byte_array< T >
&bytes,
bool
to_little_endian )
200
{
201
static
bool
current_endian =
_is_little_endian_
( );
202
if
( current_endian != to_little_endian )
203
{
204
byte_array< T >
tmp( bytes );
205
swap_bytes
( tmp );
206
return
( tmp );
207
}
208
else
209
{
210
return
( bytes );
211
}
212
}
213
214
215
217
// エンディアンに関する情報の取得及び変換グループの終わり
218
219
220
// mist名前空間の終わり
221
_MIST_END
222
223
224
#endif // __INCLUDE_MIST_ENDIAN__
225
Generated on Wed Nov 12 2014 19:44:14 for MIST by
1.8.1.2