summaryrefslogtreecommitdiffstats
path: root/indexlib/bitio.tcc
blob: f1c14c778b572f40b0cdc8c10dcf6fa89ac37352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "boost-compat/static_assert.h"
#include "boost-compat/remove_cv.h"
#ifdef HAVE_BOOST
#include <boost/type_traits/is_same.hpp>
#endif


namespace byte_io {

	template <typename T>
	inline T no_const( const volatile T v ) {
		return v;
	}

	template<typename T>
	inline
	void write( unsigned char* out, const volatile T d ) {
		write( out, no_const( d ) );
	}
	

	template <typename T>
	inline
	T read( const unsigned char* out ) {
		//BOOST_STATIC_ASSERT( !( ::boost::is_same<T,typename  ::boost::remove_cv<T>::type>::value ) );
		return read<typename ::boost::remove_cv<T>::type>( out );
	}

	template<>
	inline
	void write<uint8_t>( unsigned char* out, uint8_t d ) {
		*out = d;
	}

	template<>
	inline
	uint8_t read<uint8_t>( const unsigned char* in ) {
		return *in;
	}

	template<>
	struct byte_length_struct<uint8_t> {
		static const int value = 1;
	};

	template<>
	inline
	void write<uint16_t>( unsigned char* out, uint16_t d ) {
		*out++ = ( ( d >> 0 ) & 0xff );
		*out++ = ( ( d >> 8 ) & 0xff );
	}

	template<>
	inline
	uint16_t read<uint16_t>( const unsigned char* in ) {
		uint16_t res = 0;
		res |= ( ( *in++ & 0xff ) << 0 );
		res |= ( ( *in++ & 0xff ) << 8 );
		return res;
	}

	template<>
	struct byte_length_struct<uint16_t> {
		static const int value = 2;
	};

	template<>
	inline
	void write<uint32_t>( unsigned char* out, uint32_t d ) {
		*out++ = ( ( d >>  0 ) & 0xff );
		*out++ = ( ( d >>  8 ) & 0xff );
		*out++ = ( ( d >> 16 ) & 0xff );
		*out++ = ( ( d >> 24 ) & 0xff );
	}

	template<>
	inline
	uint32_t read<uint32_t>( const unsigned char* in ) {
		uint32_t res = 0;
		res |= ( ( *in++ & 0xff ) <<  0 );
		res |= ( ( *in++ & 0xff ) <<  8 );
		res |= ( ( *in++ & 0xff ) << 16 );
		res |= ( ( *in++ & 0xff ) << 24 );
		return res;
	}
	template<>
	struct byte_length_struct<uint32_t> {
		static const int value = 4;
	};
}