summaryrefslogtreecommitdiffstats
path: root/indexlib/thing.h
blob: 26afb07b99f3fd4c403197a2087f8120af22e61b (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef LPC_THING_H1103643194_INCLUDE_GUARD_
#define LPC_THING_H1103643194_INCLUDE_GUARD_

/* This file is part of indexlib.
 * Copyright (C) 2005 Luís Pedro Coelho <luis@luispedro.org>
 *
 * Indexlib is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation and available as file
 * GPL_V2 which is distributed along with indexlib.
 * 
 * Indexlib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA
 * 
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of this program with any edition of
 * the Qt library by Trolltech AS, Norway (or with modified versions
 * of Qt that use the same license as Qt), and distribute linked
 * combinations including the two.  You must obey the GNU General
 * Public License in all respects for all of the code used other than
 * Qt.  If you modify this file, you may extend this exception to
 * your version of the file, but you are not obligated to do so.  If
 * you do not wish to do so, delete this exception statement from
 * your version.
 */


#include <inttypes.h>
#include "bitio.h"
#include "pointer.h"

/**
 * \class thing
 *
 * This is perhaps one of the most important classes in the system.
 *
 * Ideally one would like to have something like:
 *
 *     struct_on_disk X {
 *                int32_t a;
 *                int32_t b;
 *     };
 *
 *     X var;
 *
 * And var would be accessed in our exact format. Since I want to control the exact format
 * to be able to use the same index even in different architechtures, it's not really possible.
 *
 * So we would do:
 *
 *     START_THING( X, simple_accessor )
 *		MEMBER( int32_t, a, 0 )
 *		MEMBER( int32_t, b, 4 )
 *     END_THING( X )
 *
 * This base class provides the machinery for this.
 */
template <typename accessor>
struct thing : protected accessor { // this allow the emtpy base optimization
	protected:
		thing( uint32_t idx, const accessor& access = accessor() ):
			accessor( access ),
			idx_( idx )
			{
			}

		unsigned char* base() {
			return reinterpret_cast<unsigned char*>( accessor::rw_base( idx_ ) );
		}
		const unsigned char* base() const {
			return reinterpret_cast<const unsigned char*>( accessor::ronly_base( idx_ ) );
		}
	public:
		~thing() { }
		thing( const thing& other ):
			accessor( static_cast<const accessor&>( other ) ),
			idx_( other.idx_ )
			{
			}

		thing& operator = ( const thing& other ) {
			accessor::operator=( other );
			idx_ = other.idx_;
			return *this;
		}
	protected:
		uint32_t idx_;
};

template <void * ( *get_base )()>
struct simple_accessor {
	public:
		void* rw_base( unsigned idx ) const {
			return reinterpret_cast<unsigned char*>( get_base() ) + idx;
		}
		const void* ronly_base( unsigned idx ) const {
			return reinterpret_cast<const unsigned char*>( get_base() ) + idx;
		}
};


#define START_THING( name, base ) \
	class name  : public base { \
		friend class pointer<name>; \
		protected: \
			name ( const base& b ) \
				:base( b ) \
				{ \
				} \
			\
			name ( uint32_t i ) \
				:base( i ) \
				{ \
				} \
		public:

#define MEMBER( type, name, idx ) \
		type name() const { \
			const unsigned char* data = this->base() + idx; \
			return byte_io::read<type>( data ); \
		} \
		\
		void set_ ## name ( const type & n_ ## name ) { \
			unsigned char* data = this->base() + idx; \
			byte_io::write<type>( data, n_ ## name ); \
		}

#define MY_BASE( idx ) \
		private: \
		unsigned char* my_base() { return base() + idx; } \
		const unsigned char* my_base() const { return base() + idx; } \


#define END_THING( name ) \
	}; \
	\
	typedef ::pointer< name > name ## ptr;

#define DO_POINTER_SPECS( name ) \
	namespace byte_io { \
	template<> \
	inline \
	pointer<name> read< pointer<name> >( const unsigned char* in ) \
	{ \
		return pointer< name >::cast_from_uint32( read<uint32_t>( in ) ); \
	}\
	template<> \
	inline \
	void write< pointer<name> >( unsigned char* out, pointer<name> p ) { \
		write<uint32_t>( out, p.cast_to_uint32() ); \
	} \
	template<> \
	struct byte_lenght_struct< pointer <name> > { \
		static const unsigned value = byte_lenght_struct<uint32_t>::value; \
	}; \
	} // namespace




#endif /* LPC_THING_H1103643194_INCLUDE_GUARD_ */