MythTV master
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 Or, point your browser to http://www.gnu.org/copyleft/gpl.html
19
20*/
21
22#ifndef BASECLASSES_H
23#define BASECLASSES_H
24
25#include <cstdlib> // malloc etc.
26
27#include <array>
28#include <QString>
29#include <vector>
30
31using MHPointVec = std::vector<int>; // Duplicated in freemheg.h
32
33#include "Logging.h" // For MHASSERT
34
35class MHEngine;
36
37// These templates should really be obtained from a library. They are defined here to
38// allow easy porting to a variety of libraries.
39
40// Simple sequence class.
41template <class BASE> class MHSequence {
42 public:
43 MHSequence() = default;
44 // The destructor frees the vector but not the elements.
45 ~MHSequence() { m_values.clear(); }
46 // Get the current size.
47 int Size() const { return m_values.size(); }
48 // Get an element at a particular index.
49 BASE GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_values[i]; }
50 BASE operator[](int i) const { return GetAt(i); }
51 // Add a new element at position n and move the existing element at that position
52 // and anything above it up one place.
53 void InsertAt(BASE b, int n) {
54 MHASSERT(n >= 0 && n <= Size());
55 m_values.insert(m_values.begin() + n, b);
56 }
57 // Add an element to the end of the sequence.
58 void Append(BASE b) { m_values.push_back(b); }
59 // Remove an element and shift all the others down.
60 void RemoveAt(int i) {
61 MHASSERT(i >= 0 && i < Size());
62 m_values.erase(m_values.begin() + i);
63 }
64 protected:
65 std::vector<BASE> m_values;
66};
67
68// As above, but it deletes the pointers when the sequence is destroyed.
69template <class BASE> class MHOwnPtrSequence: public MHSequence<BASE*> {
70 public:
72 {
73 for(auto *b : std::as_const(MHSequence<BASE*>::m_values))
74 delete b;
75 }
76};
77
78
79// Simple stack.
80template <class BASE> class MHStack: protected MHSequence<BASE> {
81 public:
82 // Test for empty
83 bool Empty() const { return Size() == 0; }
84 // Pop an item from the stack.
85 BASE Pop() {
86 MHASSERT(Size() > 0);
87 BASE tmp = MHSequence<BASE>::m_values.back();
89 return tmp;
90 }
91 // Push an element on the stack.
92 void Push(BASE b) { MHSequence<BASE>::m_values.push_back(b); }
93 // Return the top of the stack.
94 BASE Top() {
95 MHASSERT(Size() > 0);
96 return MHSequence<BASE>::m_values.back();
97 }
98 int Size() const { return MHSequence<BASE>::Size(); }
99};
100
101class 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.
106{
107 public:
108 MHOctetString() = default;
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
113 virtual ~MHOctetString() = default;
114
115 void Copy(const MHOctetString &str);
117 { if (this==&o) return *this; Copy(o); return *this; }
118 int Size() const { return m_pChars.size(); }
119 // Comparison - returns <0, =0, >0 depending on the ordering.
120 int Compare(const MHOctetString &str) const;
121 bool Equal(const MHOctetString &str) const { return Compare(str) == 0; }
122 unsigned char GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_pChars[i]; }
123 const unsigned char *Bytes() const { return m_pChars.data(); } // Read-only pointer to the buffer.
124 void Append(const MHOctetString &str); // Add text to the end of the string.
125
126 QString Printable() const { return QString::fromLatin1((const char*)m_pChars.data(), m_pChars.size()); }
127
128 void PrintMe(FILE *fd, int nTabs) const;
129
130protected:
131 std::vector<uint8_t> m_pChars;
132};
133
134// A colour is encoded as a string or the index into a palette.
135// Palettes aren't defined in UK MHEG so the palette index isn't really relevant.
136class MHColour {
137 public:
138 MHColour() = default;
139 void Initialise(MHParseNode *p, MHEngine *engine);
140 void PrintMe(FILE *fd, int nTabs) const;
141 bool IsSet() const { return m_nColIndex >= 0 || m_colStr.Size() != 0; }
142 void SetFromString(const char *str, int nLen);
143 void Copy(const MHColour &col);
145 int m_nColIndex {-1};
146};
147
148// An object reference is used to identify and refer to an object.
149// Internal objects have the m_pGroupId field empty.
151{
152 public:
153 MHObjectRef() = default;
154 void Initialise(MHParseNode *p, MHEngine *engine);
155 void Copy(const MHObjectRef &objr);
157
159
160 // Sometimes the object reference is optional. This tests if it has been set
161 bool IsSet() const { return (m_nObjectNo != 0 || m_groupId.Size() != 0); }
162 void PrintMe(FILE *fd, int nTabs) const;
163 bool Equal(const MHObjectRef &objr, MHEngine *engine) const;
164 QString Printable() const;
165
166 int m_nObjectNo {0};
168};
169
170// A content reference gives the location (e.g. file name) to find the content.
172{
173 public:
174 MHContentRef() = default;
175
177
178 void Initialise(MHParseNode *p, MHEngine *engine);
179 void PrintMe(FILE *fd, int nTabs) const { m_contentRef.PrintMe(fd, nTabs); }
181 bool IsSet() const { return m_contentRef.Size() != 0; }
182 bool Equal(const MHContentRef &cr, MHEngine *engine) const;
184 QString Printable() const { return m_contentRef.Printable(); }
185
187};
188
189// "Generic" versions of int, bool etc can be either the value or an indirect reference.
191{
192 public:
193 MHObjectRef *GetReference(); // Return the indirect reference or fail if it's direct
194 bool m_fIsDirect {false};
195protected:
197};
198
200{
201 public:
202 MHGenericBoolean() = default;
203 void Initialise(MHParseNode *p, MHEngine *engine);
204 void PrintMe(FILE *fd, int nTabs) const;
205 bool GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
206protected:
207 bool m_fDirect {false};
208};
209
211{
212 public:
213 MHGenericInteger() = default;
214 void Initialise(MHParseNode *p, MHEngine *engine);
215 void PrintMe(FILE *fd, int nTabs) const;
216 int GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
217protected:
218 int m_nDirect {-1};
219};
220
222{
223 public:
225 void Initialise(MHParseNode *p, MHEngine *engine);
226 void PrintMe(FILE *fd, int nTabs) const;
227 void GetValue(MHOctetString &str, MHEngine *engine) const; // Return the value, looking up any indirect ref.
228protected:
230};
231
233{
234 public:
236 void Initialise(MHParseNode *p, MHEngine *engine);
237 void PrintMe(FILE *fd, int nTabs) const;
238 void GetValue(MHObjectRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
239protected:
241};
242
244{
245 public:
247 void Initialise(MHParseNode *p, MHEngine *engine);
248 void PrintMe(FILE *fd, int nTabs) const;
249 void GetValue(MHContentRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
250protected:
252};
253
254// In certain cases (e.g. parameters to Call) we have values which are the union of the base types.
256{
257 public:
258 MHParameter() = default;
259 void Initialise(MHParseNode *p, MHEngine *engine);
260 void PrintMe(FILE *fd, int nTabs) const;
261 MHObjectRef *GetReference(); // Get an indirect reference.
262
263 enum ParamTypes : std::uint8_t {
269 P_Null
270 } m_Type { P_Null }; // Null is used when this is optional
271
277};
278
279// A union type. Returned when a parameter is evaluated.
281{
282 public:
283 MHUnion() = default;
284 MHUnion(int nVal) : m_Type(U_Int), m_nIntVal(nVal) {}
285 MHUnion(bool fVal) : m_Type(U_Bool), m_fBoolVal(fVal) {}
286 MHUnion(const MHOctetString &strVal) : m_Type(U_String) { m_strVal.Copy(strVal); }
287 MHUnion(const MHObjectRef &objVal) : m_Type(U_ObjRef) { m_objRefVal.Copy(objVal); };
288 MHUnion(const MHContentRef &cnVal) : m_Type(U_ContentRef) { m_contentRefVal.Copy(cnVal); }
289
290 MHUnion& operator=(const MHUnion&) = default;
291
292 void GetValueFrom(const MHParameter &value, MHEngine *engine); // Copies the argument, getting the value of an indirect args.
293 QString Printable() const;
294
295 enum UnionTypes : std::uint8_t { U_Int, U_Bool, U_String, U_ObjRef, U_ContentRef, U_None } m_Type {U_None};
296 void CheckType (enum UnionTypes t) const; // Check a type and fail if it doesn't match.
297 static const char *GetAsString(enum UnionTypes t);
298
299 int m_nIntVal {0};
300 bool m_fBoolVal {false};
304};
305
307 // A font body can either be a string or an object reference
308 public:
309 MHFontBody() = default;
310 void Initialise(MHParseNode *p, MHEngine *engine);
311 void PrintMe(FILE *fd, int nTabs) const;
312 bool IsSet() const { return m_dirFont.Size() != 0 || m_indirFont.IsSet(); }
313 void Copy(const MHFontBody &fb);
314protected:
317};
318
319// This is used only in DynamicLineArt
321 public:
322 MHPointArg() = default;
323 void Initialise(MHParseNode *p, MHEngine *engine);
324 void PrintMe(FILE *fd, int nTabs) const;
326};
327
328#endif
std::vector< int > MHPointVec
Definition: BaseClasses.h:31
#define MHASSERT(f)
Definition: Logging.h:30
bool IsSet() const
Definition: BaseClasses.h:141
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
MHOctetString m_colStr
Definition: BaseClasses.h:144
MHColour()=default
void SetFromString(const char *str, int nLen)
void Copy(const MHColour &col)
int m_nColIndex
Definition: BaseClasses.h:145
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef()=default
bool IsSet() const
Definition: BaseClasses.h:181
static MHContentRef Null
Definition: BaseClasses.h:183
MHOctetString m_contentRef
Definition: BaseClasses.h:186
void Copy(const MHContentRef &cr)
Definition: BaseClasses.h:180
MHContentRef & operator=(const MHContentRef &)=default
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.h:179
bool Equal(const MHContentRef &cr, MHEngine *engine) const
QString Printable() const
Definition: BaseClasses.h:184
MHObjectRef m_indirFont
Definition: BaseClasses.h:316
void Copy(const MHFontBody &fb)
void PrintMe(FILE *fd, int nTabs) const
bool IsSet() const
Definition: BaseClasses.h:312
MHFontBody()=default
MHOctetString m_dirFont
Definition: BaseClasses.h:315
void Initialise(MHParseNode *p, MHEngine *engine)
MHObjectRef m_indirect
Definition: BaseClasses.h:196
MHObjectRef * GetReference()
void PrintMe(FILE *fd, int nTabs) const
MHGenericBoolean()=default
void Initialise(MHParseNode *p, MHEngine *engine)
bool GetValue(MHEngine *engine) const
MHGenericContentRef()=default
void GetValue(MHContentRef &ref, MHEngine *engine) const
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
MHContentRef m_direct
Definition: BaseClasses.h:251
void Initialise(MHParseNode *p, MHEngine *engine)
int GetValue(MHEngine *engine) const
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger()=default
MHGenericObjectRef()=default
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
MHObjectRef m_objRef
Definition: BaseClasses.h:240
void GetValue(MHObjectRef &ref, MHEngine *engine) const
MHOctetString m_direct
Definition: BaseClasses.h:229
void PrintMe(FILE *fd, int nTabs) const
void Initialise(MHParseNode *p, MHEngine *engine)
void GetValue(MHOctetString &str, MHEngine *engine) const
MHGenericOctetString()=default
MHOctetString m_groupId
Definition: BaseClasses.h:167
MHObjectRef()=default
bool Equal(const MHObjectRef &objr, MHEngine *engine) const
bool IsSet() const
Definition: BaseClasses.h:161
QString Printable() const
MHObjectRef & operator=(const MHObjectRef &)=default
void PrintMe(FILE *fd, int nTabs) const
static MHObjectRef Null
Definition: BaseClasses.h:156
void Initialise(MHParseNode *p, MHEngine *engine)
void Copy(const MHObjectRef &objr)
void Copy(const MHOctetString &str)
Definition: BaseClasses.cpp:85
const unsigned char * Bytes() const
Definition: BaseClasses.h:123
MHOctetString(const MHOctetString &o)
Definition: BaseClasses.h:112
MHOctetString & operator=(const MHOctetString &o)
Definition: BaseClasses.h:116
int Size() const
Definition: BaseClasses.h:118
virtual ~MHOctetString()=default
unsigned char GetAt(int i) const
Definition: BaseClasses.h:122
void Append(const MHOctetString &str)
std::vector< uint8_t > m_pChars
Definition: BaseClasses.h:131
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.cpp:97
QString Printable() const
Definition: BaseClasses.h:126
MHOctetString()=default
int Compare(const MHOctetString &str) const
bool Equal(const MHOctetString &str) const
Definition: BaseClasses.h:121
MHGenericContentRef m_contentRefVal
Definition: BaseClasses.h:276
MHGenericOctetString m_strVal
Definition: BaseClasses.h:274
MHGenericBoolean m_boolVal
Definition: BaseClasses.h:273
MHGenericObjectRef m_objRefVal
Definition: BaseClasses.h:275
MHGenericInteger m_intVal
Definition: BaseClasses.h:272
MHObjectRef * GetReference()
MHParameter()=default
enum MHParameter::ParamTypes P_Null
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger m_x
Definition: BaseClasses.h:325
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger m_y
Definition: BaseClasses.h:325
void Initialise(MHParseNode *p, MHEngine *engine)
MHPointArg()=default
void RemoveAt(int i)
Definition: BaseClasses.h:60
int Size() const
Definition: BaseClasses.h:47
std::vector< BASE > m_values
Definition: BaseClasses.h:65
void Append(BASE b)
Definition: BaseClasses.h:58
void InsertAt(BASE b, int n)
Definition: BaseClasses.h:53
BASE operator[](int i) const
Definition: BaseClasses.h:50
MHSequence()=default
BASE GetAt(int i) const
Definition: BaseClasses.h:49
void Push(BASE b)
Definition: BaseClasses.h:92
int Size() const
Definition: BaseClasses.h:98
BASE Pop()
Definition: BaseClasses.h:85
BASE Top()
Definition: BaseClasses.h:94
bool Empty() const
Definition: BaseClasses.h:83
QString Printable() const
MHUnion(bool fVal)
Definition: BaseClasses.h:285
MHUnion(int nVal)
Definition: BaseClasses.h:284
MHUnion(const MHOctetString &strVal)
Definition: BaseClasses.h:286
bool m_fBoolVal
Definition: BaseClasses.h:300
int m_nIntVal
Definition: BaseClasses.h:299
MHObjectRef m_objRefVal
Definition: BaseClasses.h:302
MHContentRef m_contentRefVal
Definition: BaseClasses.h:303
void GetValueFrom(const MHParameter &value, MHEngine *engine)
MHUnion(const MHObjectRef &objVal)
Definition: BaseClasses.h:287
MHUnion(const MHContentRef &cnVal)
Definition: BaseClasses.h:288
MHUnion & operator=(const MHUnion &)=default
MHUnion()=default
void CheckType(enum UnionTypes t) const
@ U_ContentRef
Definition: BaseClasses.h:295
enum MHUnion::UnionTypes U_None
static const char * GetAsString(enum UnionTypes t)
MHOctetString m_strVal
Definition: BaseClasses.h:301
int FILE
Definition: mythburn.py:137