MythTV
0.27pre
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Groups
Pages
libs
libmythfreemheg
BaseClasses.h
Go to the documentation of this file.
1
/* BaseClasses.h
2
3
Copyright (C) David C. J. Matthews 2004 dm at prolingua.co.uk
4
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License
7
as published by the Free Software Foundation; either version 2
8
of the License, or (at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
19
20
*/
21
22
#if !defined(BASECLASSES_H)
23
#define BASECLASSES_H
24
25
#include "config.h"
26
#if HAVE_MALLOC_H
27
#include <malloc.h>
28
#endif
29
30
#include <QString>
31
32
#include "
Logging.h
"
// For MHASSERT
33
34
class
MHEngine
;
35
36
// These templates should really be obtained from a library. They are defined here to
37
// allow easy porting to a variety of libraries.
38
39
// Simple sequence class.
40
template
<
class
BASE>
class
MHSequence
{
41
public
:
42
MHSequence
(){
m_VecSize
= 0;
m_Values
= 0; }
43
// The destructor frees the vector but not the elements.
44
~MHSequence
() { free(
m_Values
); }
45
// Get the current size.
46
int
Size
()
const
{
return
m_VecSize
; }
47
// Get an element at a particular index.
48
BASE
GetAt
(
int
i)
const
{ MHASSERT(i >= 0 && i <
m_VecSize
);
return
m_Values
[i]; }
49
BASE
operator[]
(
int
i)
const
{
return
GetAt
(i); }
50
// Add a new element at position n and move the existing element at that position
51
// and anything above it up one place.
52
void
InsertAt
(BASE
b
,
int
n
) {
53
MHASSERT(n >= 0 && n <=
m_VecSize
);
54
BASE *
ptr
= (BASE*)realloc(
m_Values
, (
m_VecSize
+1) *
sizeof
(BASE));
55
if
(ptr == NULL)
throw
"Out of Memory"
;
56
m_Values
=
ptr
;
57
for
(
int
i =
m_VecSize
; i >
n
; i--)
m_Values
[i] =
m_Values
[i-1];
58
m_Values
[
n
] =
b
;
59
m_VecSize
++;
60
}
61
// Add an element to the end of the sequence.
62
void
Append
(BASE
b
) {
InsertAt
(b,
m_VecSize
); }
63
// Remove an element and shift all the others down.
64
void
RemoveAt
(
int
i) {
65
MHASSERT(i >= 0 && i <
m_VecSize
);
66
for
(
int
j = i+1; j <
m_VecSize
; j++)
m_Values
[j-1] =
m_Values
[j];
67
m_VecSize--;
68
}
69
protected
:
70
int
m_VecSize
;
71
BASE *
m_Values
;
72
};
73
74
// As above, but it deletes the pointers when the sequence is destroyed.
75
template
<
class
BASE>
class
MHOwnPtrSequence
:
public
MHSequence
<BASE*> {
76
public
:
77
~MHOwnPtrSequence
() {
for
(
int
i = 0; i < MHSequence<BASE*>::m_VecSize; i++)
delete
(
MHSequence<BASE*>::GetAt
(i)); }
78
};
79
80
81
// Simple stack.
82
template
<
class
BASE>
class
MHStack
:
protected
MHSequence
<BASE> {
83
public
:
84
// Test for empty
85
bool
Empty
()
const
{
return
Size
() == 0; }
86
// Pop an item from the stack.
87
BASE
Pop
() {
88
MHASSERT(
MHSequence<BASE>::m_VecSize
> 0);
89
return
MHSequence<BASE>::m_Values
[--
MHSequence<BASE>::m_VecSize
];
90
}
91
// Push an element on the stack.
92
void
Push
(BASE
b
) { this->
Append
(b); }
93
// Return the top of the stack.
94
BASE
Top
() {
95
MHASSERT(
MHSequence<BASE>::m_VecSize
> 0);
96
return
MHSequence<BASE>::m_Values
[
MHSequence<BASE>::m_VecSize
-1];
97
}
98
int
Size
()
const
{
return
MHSequence<BASE>::Size
(); }
99
};
100
101
class
MHParseNode
;
102
103
// An MHEG octet string is a sequence of bytes which can include nulls. MHEG, or at least UK MHEG, indexes
104
// strings from 1. We use 0 indexing in calls to these functions.
105
class
MHOctetString
106
{
107
public
:
108
MHOctetString
();
109
MHOctetString
(
const
char
*str,
int
nLen = -1);
// From character string
110
MHOctetString
(
const
unsigned
char
*str,
int
nLen);
// From byte vector
111
MHOctetString
(
const
MHOctetString
&str,
int
nOffset=0,
int
nLen=-1);
// Substring
112
virtual
~MHOctetString
();
113
114
void
Copy
(
const
MHOctetString
&str);
115
int
Size
()
const
{
return
m_nLength
; }
116
// Comparison - returns <0, =0, >0 depending on the ordering.
117
int
Compare
(
const
MHOctetString
&str)
const
;
118
bool
Equal
(
const
MHOctetString
&str)
const
{
return
Compare
(str) == 0; }
119
unsigned
char
GetAt
(
int
i)
const
{ MHASSERT(i >= 0 && i <
Size
());
return
m_pChars
[i]; }
120
const
unsigned
char
*
Bytes
()
const
{
return
m_pChars
; }
// Read-only pointer to the buffer.
121
void
Append
(
const
MHOctetString
&str);
// Add text to the end of the string.
122
123
QString
Printable
()
const
{
return
QString::fromLatin1((
const
char
*)
m_pChars
,
m_nLength
); }
124
125
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
126
127
protected
:
128
int
m_nLength
;
129
unsigned
char
*
m_pChars
;
130
};
131
132
// A colour is encoded as a string or the index into a palette.
133
// Palettes aren't defined in UK MHEG so the palette index isn't really relevant.
134
class
MHColour
{
135
public
:
136
MHColour
():
m_nColIndex
(-1) {}
137
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
138
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
139
bool
IsSet
()
const
{
return
m_nColIndex
>= 0 ||
m_ColStr
.
Size
() != 0; }
140
void
SetFromString
(
const
char
*str,
int
nLen);
141
void
Copy
(
const
MHColour
&col);
142
MHOctetString
m_ColStr
;
143
int
m_nColIndex
;
144
};
145
146
// An object reference is used to identify and refer to an object.
147
// Internal objects have the m_pGroupId field empty.
148
class
MHObjectRef
149
{
150
public
:
151
MHObjectRef
() {
m_nObjectNo
= 0; }
152
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
153
void
Copy
(
const
MHObjectRef
&objr);
154
static
MHObjectRef
Null
;
155
156
// Sometimes the object reference is optional. This tests if it has been set
157
bool
IsSet
()
const
{
return
(
m_nObjectNo
!= 0 ||
m_GroupId
.
Size
() != 0); }
158
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
159
bool
Equal
(
const
MHObjectRef
&objr,
MHEngine
*engine)
const
;
160
QString
Printable
()
const
;
161
162
int
m_nObjectNo
;
163
MHOctetString
m_GroupId
;
164
};
165
166
// A content reference gives the location (e.g. file name) to find the content.
167
class
MHContentRef
168
{
169
public
:
170
MHContentRef
() {}
171
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
172
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
{
m_ContentRef
.
PrintMe
(fd, nTabs); }
173
void
Copy
(
const
MHContentRef
&cr) {
m_ContentRef
.
Copy
(cr.
m_ContentRef
); }
174
bool
IsSet
()
const
{
return
m_ContentRef
.
Size
() != 0; }
175
bool
Equal
(
const
MHContentRef
&cr,
MHEngine
*engine)
const
;
176
static
MHContentRef
Null
;
177
QString
Printable
()
const
{
return
m_ContentRef
.
Printable
(); }
178
179
MHOctetString
m_ContentRef
;
180
};
181
182
// "Generic" versions of int, bool etc can be either the value or an indirect reference.
183
class
MHGenericBase
184
{
185
public
:
186
MHObjectRef
*
GetReference
();
// Return the indirect reference or fail if it's direct
187
bool
m_fIsDirect
;
188
protected
:
189
MHObjectRef
m_Indirect
;
190
};
191
192
class
MHGenericBoolean
:
public
MHGenericBase
193
{
194
public
:
195
MHGenericBoolean
() :
m_fDirect
(
false
) {}
196
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
197
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
198
bool
GetValue
(
MHEngine
*engine)
const
;
// Return the value, looking up any indirect ref.
199
protected
:
200
bool
m_fDirect
;
201
};
202
203
class
MHGenericInteger
:
public
MHGenericBase
204
{
205
public
:
206
MHGenericInteger
() :
m_nDirect
(-1) {}
207
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
208
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
209
int
GetValue
(
MHEngine
*engine)
const
;
// Return the value, looking up any indirect ref.
210
protected
:
211
int
m_nDirect
;
212
};
213
214
class
MHGenericOctetString
:
public
MHGenericBase
215
{
216
public
:
217
MHGenericOctetString
() {}
218
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
219
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
220
void
GetValue
(
MHOctetString
&str,
MHEngine
*engine)
const
;
// Return the value, looking up any indirect ref.
221
protected
:
222
MHOctetString
m_Direct
;
223
};
224
225
class
MHGenericObjectRef
:
public
MHGenericBase
226
{
227
public
:
228
MHGenericObjectRef
() {}
229
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
230
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
231
void
GetValue
(
MHObjectRef
&ref,
MHEngine
*engine)
const
;
// Return the value, looking up any indirect ref.
232
protected
:
233
MHObjectRef
m_ObjRef
;
234
};
235
236
class
MHGenericContentRef
:
public
MHGenericBase
237
{
238
public
:
239
MHGenericContentRef
() {}
240
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
241
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
242
void
GetValue
(
MHContentRef
&ref,
MHEngine
*engine)
const
;
// Return the value, looking up any indirect ref.
243
protected
:
244
MHContentRef
m_Direct
;
245
};
246
247
// In certain cases (e.g. parameters to Call) we have values which are the union of the base types.
248
class
MHParameter
249
{
250
public
:
251
MHParameter
():
m_Type
(
P_Null
) {}
252
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
253
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
254
MHObjectRef
*
GetReference
();
// Get an indirect reference.
255
256
enum
ParamTypes
{
P_Int
,
P_Bool
,
P_String
,
P_ObjRef
,
P_ContentRef
,
P_Null
}
m_Type
;
// Null is used when this is optional
257
258
MHGenericInteger
m_IntVal
;
259
MHGenericBoolean
m_BoolVal
;
260
MHGenericOctetString
m_StrVal
;
261
MHGenericObjectRef
m_ObjRefVal
;
262
MHGenericContentRef
m_ContentRefVal
;
263
};
264
265
// A union type. Returned when a parameter is evaluated.
266
class
MHUnion
267
{
268
public
:
269
MHUnion
() :
m_Type
(
U_None
),
m_nIntVal
(0),
m_fBoolVal
(
false
) {}
270
MHUnion
(
int
nVal) :
m_Type
(
U_Int
),
m_nIntVal
(nVal),
m_fBoolVal
(
false
) {}
271
MHUnion
(
bool
fVal) :
m_Type
(
U_Bool
),
m_nIntVal
(0),
m_fBoolVal
(fVal) {}
272
MHUnion
(
const
MHOctetString
&strVal) :
m_Type
(
U_String
),
m_nIntVal
(0),
m_fBoolVal
(
false
) {
m_StrVal
.
Copy
(strVal); }
273
MHUnion
(
const
MHObjectRef
&objVal) :
m_Type
(
U_ObjRef
),
m_nIntVal
(0),
m_fBoolVal
(
false
) {
m_ObjRefVal
.
Copy
(objVal); };
274
MHUnion
(
const
MHContentRef
&cnVal) :
m_Type
(
U_ContentRef
),
m_nIntVal
(0),
m_fBoolVal
(
false
) {
m_ContentRefVal
.
Copy
(cnVal); }
275
276
void
GetValueFrom
(
const
MHParameter
&value,
MHEngine
*engine);
// Copies the argument, getting the value of an indirect args.
277
278
enum
UnionTypes
{
U_Int
,
U_Bool
,
U_String
,
U_ObjRef
,
U_ContentRef
,
U_None
}
m_Type
;
279
void
CheckType
(
enum
UnionTypes
)
const
;
// Check a type and fail if it doesn't match.
280
static
const
char
*
GetAsString
(
enum
UnionTypes
t
);
281
282
int
m_nIntVal
;
283
bool
m_fBoolVal
;
284
MHOctetString
m_StrVal
;
285
MHObjectRef
m_ObjRefVal
;
286
MHContentRef
m_ContentRefVal
;
287
};
288
289
class
MHFontBody
{
290
// A font body can either be a string or an object reference
291
public
:
292
MHFontBody
() {}
293
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
294
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
295
bool
IsSet
()
const
{
return
m_DirFont
.
Size
() != 0 ||
m_IndirFont
.
IsSet
(); }
296
void
Copy
(
const
MHFontBody
&fb);
297
protected
:
298
MHOctetString
m_DirFont
;
299
MHObjectRef
m_IndirFont
;
300
};
301
302
// This is used only in DynamicLineArt
303
class
MHPointArg
{
304
public
:
305
MHPointArg
() {}
306
void
Initialise
(
MHParseNode
*
p
,
MHEngine
*engine);
307
void
PrintMe
(
FILE
*fd,
int
nTabs)
const
;
308
MHGenericInteger
x
,
y
;
309
};
310
311
#endif
Generated on Tue Jun 18 2013 13:00:04 for MythTV by
1.8.1.2