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#if !defined(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() { free(reinterpret_cast<void*>(m_values)); }
46 // Get the current size.
47 int Size() const { return m_vecSize; }
48 // Get an element at a particular index.
49 BASE GetAt(int i) const { MHASSERT(i >= 0 && i < m_vecSize); 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 <= m_vecSize);
55 // NOLINTNEXTLINE(bugprone-sizeof-expression)
56 BASE *ptr = (BASE*)realloc(reinterpret_cast<void*>(m_values), (m_vecSize+1) * sizeof(BASE));
57 if (ptr == nullptr) throw "Out of Memory";
58 m_values = ptr;
59 for (int i = m_vecSize; i > n; i--) m_values[i] = m_values[i-1];
60 m_values[n] = b;
61 m_vecSize++;
62 }
63 // Add an element to the end of the sequence.
64 void Append(BASE b) { InsertAt(b, m_vecSize); }
65 // Remove an element and shift all the others down.
66 void RemoveAt(int i) {
67 MHASSERT(i >= 0 && i < m_vecSize);
68 for (int j = i+1; j < m_vecSize; j++) m_values[j-1] = m_values[j];
69 m_vecSize--;
70 }
71 protected:
72 int m_vecSize {0};
73 BASE *m_values {nullptr};
74};
75
76// As above, but it deletes the pointers when the sequence is destroyed.
77template <class BASE> class MHOwnPtrSequence: public MHSequence<BASE*> {
78 public:
79 ~MHOwnPtrSequence() { for(int i = 0; i < MHSequence<BASE*>::m_vecSize; i++) delete(MHSequence<BASE*>::GetAt(i)); }
80};
81
82
83// Simple stack.
84template <class BASE> class MHStack: protected MHSequence<BASE> {
85 public:
86 // Test for empty
87 bool Empty() const { return Size() == 0; }
88 // Pop an item from the stack.
89 BASE Pop() {
92 }
93 // Push an element on the stack.
94 void Push(BASE b) { this->Append(b); }
95 // Return the top of the stack.
96 BASE Top() {
99 }
100 int Size() const { return MHSequence<BASE>::Size(); }
101};
102
103class MHParseNode;
104
105// An MHEG octet string is a sequence of bytes which can include nulls. MHEG, or at least UK MHEG, indexes
106// strings from 1. We use 0 indexing in calls to these functions.
108{
109 public:
110 MHOctetString() = default;
111 MHOctetString(const char *str, int nLen = -1); // From character string
112 MHOctetString(const unsigned char *str, int nLen); // From byte vector
113 MHOctetString(const MHOctetString &str, int nOffset=0, int nLen=-1); // Substring
115 virtual ~MHOctetString();
116
117 void Copy(const MHOctetString &str);
119 { if (this==&o) return *this; Copy(o); return *this; }
120 int Size() const { return m_nLength; }
121 // Comparison - returns <0, =0, >0 depending on the ordering.
122 int Compare(const MHOctetString &str) const;
123 bool Equal(const MHOctetString &str) const { return Compare(str) == 0; }
124 unsigned char GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_pChars[i]; }
125 const unsigned char *Bytes() const { return m_pChars; } // Read-only pointer to the buffer.
126 void Append(const MHOctetString &str); // Add text to the end of the string.
127
128 QString Printable() const { return QString::fromLatin1((const char*)m_pChars, m_nLength); }
129
130 void PrintMe(FILE *fd, int nTabs) const;
131
132protected:
133 int m_nLength {0};
134 unsigned char *m_pChars {nullptr};
135};
136
137// A colour is encoded as a string or the index into a palette.
138// Palettes aren't defined in UK MHEG so the palette index isn't really relevant.
139class MHColour {
140 public:
141 MHColour() = default;
142 void Initialise(MHParseNode *p, MHEngine *engine);
143 void PrintMe(FILE *fd, int nTabs) const;
144 bool IsSet() const { return m_nColIndex >= 0 || m_colStr.Size() != 0; }
145 void SetFromString(const char *str, int nLen);
146 void Copy(const MHColour &col);
148 int m_nColIndex {-1};
149};
150
151// An object reference is used to identify and refer to an object.
152// Internal objects have the m_pGroupId field empty.
154{
155 public:
156 MHObjectRef() = default;
157 void Initialise(MHParseNode *p, MHEngine *engine);
158 void Copy(const MHObjectRef &objr);
160
162
163 // Sometimes the object reference is optional. This tests if it has been set
164 bool IsSet() const { return (m_nObjectNo != 0 || m_groupId.Size() != 0); }
165 void PrintMe(FILE *fd, int nTabs) const;
166 bool Equal(const MHObjectRef &objr, MHEngine *engine) const;
167 QString Printable() const;
168
169 int m_nObjectNo {0};
171};
172
173// A content reference gives the location (e.g. file name) to find the content.
175{
176 public:
177 MHContentRef() = default;
178
180
181 void Initialise(MHParseNode *p, MHEngine *engine);
182 void PrintMe(FILE *fd, int nTabs) const { m_contentRef.PrintMe(fd, nTabs); }
184 bool IsSet() const { return m_contentRef.Size() != 0; }
185 bool Equal(const MHContentRef &cr, MHEngine *engine) const;
187 QString Printable() const { return m_contentRef.Printable(); }
188
190};
191
192// "Generic" versions of int, bool etc can be either the value or an indirect reference.
194{
195 public:
196 MHObjectRef *GetReference(); // Return the indirect reference or fail if it's direct
197 bool m_fIsDirect {false};
198protected:
200};
201
203{
204 public:
205 MHGenericBoolean() = default;
206 void Initialise(MHParseNode *p, MHEngine *engine);
207 void PrintMe(FILE *fd, int nTabs) const;
208 bool GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
209protected:
210 bool m_fDirect {false};
211};
212
214{
215 public:
216 MHGenericInteger() = default;
217 void Initialise(MHParseNode *p, MHEngine *engine);
218 void PrintMe(FILE *fd, int nTabs) const;
219 int GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
220protected:
221 int m_nDirect {-1};
222};
223
225{
226 public:
228 void Initialise(MHParseNode *p, MHEngine *engine);
229 void PrintMe(FILE *fd, int nTabs) const;
230 void GetValue(MHOctetString &str, MHEngine *engine) const; // Return the value, looking up any indirect ref.
231protected:
233};
234
236{
237 public:
239 void Initialise(MHParseNode *p, MHEngine *engine);
240 void PrintMe(FILE *fd, int nTabs) const;
241 void GetValue(MHObjectRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
242protected:
244};
245
247{
248 public:
250 void Initialise(MHParseNode *p, MHEngine *engine);
251 void PrintMe(FILE *fd, int nTabs) const;
252 void GetValue(MHContentRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
253protected:
255};
256
257// In certain cases (e.g. parameters to Call) we have values which are the union of the base types.
259{
260 public:
261 MHParameter() = default;
262 void Initialise(MHParseNode *p, MHEngine *engine);
263 void PrintMe(FILE *fd, int nTabs) const;
264 MHObjectRef *GetReference(); // Get an indirect reference.
265
266 enum ParamTypes : std::uint8_t {
272 P_Null
273 } m_Type { P_Null }; // Null is used when this is optional
274
280};
281
282// A union type. Returned when a parameter is evaluated.
284{
285 public:
286 MHUnion() = default;
287 MHUnion(int nVal) : m_Type(U_Int), m_nIntVal(nVal) {}
288 MHUnion(bool fVal) : m_Type(U_Bool), m_fBoolVal(fVal) {}
289 MHUnion(const MHOctetString &strVal) : m_Type(U_String) { m_strVal.Copy(strVal); }
290 MHUnion(const MHObjectRef &objVal) : m_Type(U_ObjRef) { m_objRefVal.Copy(objVal); };
291 MHUnion(const MHContentRef &cnVal) : m_Type(U_ContentRef) { m_contentRefVal.Copy(cnVal); }
292
293 MHUnion& operator=(const MHUnion&) = default;
294
295 void GetValueFrom(const MHParameter &value, MHEngine *engine); // Copies the argument, getting the value of an indirect args.
296 QString Printable() const;
297
298 enum UnionTypes : std::uint8_t { U_Int, U_Bool, U_String, U_ObjRef, U_ContentRef, U_None } m_Type {U_None};
299 void CheckType (enum UnionTypes t) const; // Check a type and fail if it doesn't match.
300 static const char *GetAsString(enum UnionTypes t);
301
302 int m_nIntVal {0};
303 bool m_fBoolVal {false};
307};
308
310 // A font body can either be a string or an object reference
311 public:
312 MHFontBody() = default;
313 void Initialise(MHParseNode *p, MHEngine *engine);
314 void PrintMe(FILE *fd, int nTabs) const;
315 bool IsSet() const { return m_dirFont.Size() != 0 || m_indirFont.IsSet(); }
316 void Copy(const MHFontBody &fb);
317protected:
320};
321
322// This is used only in DynamicLineArt
324 public:
325 MHPointArg() = default;
326 void Initialise(MHParseNode *p, MHEngine *engine);
327 void PrintMe(FILE *fd, int nTabs) const;
329};
330
331#endif
std::vector< int > MHPointVec
Definition: BaseClasses.h:31
#define MHASSERT(f)
Definition: Logging.h:30
bool IsSet() const
Definition: BaseClasses.h:144
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
MHOctetString m_colStr
Definition: BaseClasses.h:147
MHColour()=default
void SetFromString(const char *str, int nLen)
void Copy(const MHColour &col)
int m_nColIndex
Definition: BaseClasses.h:148
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef()=default
bool IsSet() const
Definition: BaseClasses.h:184
static MHContentRef Null
Definition: BaseClasses.h:186
MHOctetString m_contentRef
Definition: BaseClasses.h:189
void Copy(const MHContentRef &cr)
Definition: BaseClasses.h:183
MHContentRef & operator=(const MHContentRef &)=default
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.h:182
bool Equal(const MHContentRef &cr, MHEngine *engine) const
QString Printable() const
Definition: BaseClasses.h:187
MHObjectRef m_indirFont
Definition: BaseClasses.h:319
void Copy(const MHFontBody &fb)
void PrintMe(FILE *fd, int nTabs) const
bool IsSet() const
Definition: BaseClasses.h:315
MHFontBody()=default
MHOctetString m_dirFont
Definition: BaseClasses.h:318
void Initialise(MHParseNode *p, MHEngine *engine)
MHObjectRef m_indirect
Definition: BaseClasses.h:199
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:254
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:243
void GetValue(MHObjectRef &ref, MHEngine *engine) const
MHOctetString m_direct
Definition: BaseClasses.h:232
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:170
MHObjectRef()=default
bool Equal(const MHObjectRef &objr, MHEngine *engine) const
bool IsSet() const
Definition: BaseClasses.h:164
QString Printable() const
MHObjectRef & operator=(const MHObjectRef &)=default
void PrintMe(FILE *fd, int nTabs) const
static MHObjectRef Null
Definition: BaseClasses.h:159
void Initialise(MHParseNode *p, MHEngine *engine)
void Copy(const MHObjectRef &objr)
virtual ~MHOctetString()
void Copy(const MHOctetString &str)
const unsigned char * Bytes() const
Definition: BaseClasses.h:125
MHOctetString(const MHOctetString &o)
Definition: BaseClasses.h:114
unsigned char * m_pChars
Definition: BaseClasses.h:134
MHOctetString & operator=(const MHOctetString &o)
Definition: BaseClasses.h:118
int Size() const
Definition: BaseClasses.h:120
unsigned char GetAt(int i) const
Definition: BaseClasses.h:124
void Append(const MHOctetString &str)
void PrintMe(FILE *fd, int nTabs) const
QString Printable() const
Definition: BaseClasses.h:128
MHOctetString()=default
int Compare(const MHOctetString &str) const
bool Equal(const MHOctetString &str) const
Definition: BaseClasses.h:123
MHGenericContentRef m_contentRefVal
Definition: BaseClasses.h:279
MHGenericOctetString m_strVal
Definition: BaseClasses.h:277
MHGenericBoolean m_boolVal
Definition: BaseClasses.h:276
MHGenericObjectRef m_objRefVal
Definition: BaseClasses.h:278
MHGenericInteger m_intVal
Definition: BaseClasses.h:275
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:328
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger m_y
Definition: BaseClasses.h:328
void Initialise(MHParseNode *p, MHEngine *engine)
MHPointArg()=default
BASE * m_values
Definition: BaseClasses.h:73
void RemoveAt(int i)
Definition: BaseClasses.h:66
int Size() const
Definition: BaseClasses.h:47
void Append(BASE b)
Definition: BaseClasses.h:64
void InsertAt(BASE b, int n)
Definition: BaseClasses.h:53
BASE operator[](int i) const
Definition: BaseClasses.h:50
int m_vecSize
Definition: BaseClasses.h:72
MHSequence()=default
BASE GetAt(int i) const
Definition: BaseClasses.h:49
void Push(BASE b)
Definition: BaseClasses.h:94
int Size() const
Definition: BaseClasses.h:100
BASE Pop()
Definition: BaseClasses.h:89
BASE Top()
Definition: BaseClasses.h:96
bool Empty() const
Definition: BaseClasses.h:87
QString Printable() const
MHUnion(bool fVal)
Definition: BaseClasses.h:288
MHUnion(int nVal)
Definition: BaseClasses.h:287
MHUnion(const MHOctetString &strVal)
Definition: BaseClasses.h:289
bool m_fBoolVal
Definition: BaseClasses.h:303
int m_nIntVal
Definition: BaseClasses.h:302
MHObjectRef m_objRefVal
Definition: BaseClasses.h:305
MHContentRef m_contentRefVal
Definition: BaseClasses.h:306
void GetValueFrom(const MHParameter &value, MHEngine *engine)
MHUnion(const MHObjectRef &objVal)
Definition: BaseClasses.h:290
MHUnion(const MHContentRef &cnVal)
Definition: BaseClasses.h:291
MHUnion & operator=(const MHUnion &)=default
MHUnion()=default
void CheckType(enum UnionTypes t) const
@ U_ContentRef
Definition: BaseClasses.h:298
enum MHUnion::UnionTypes U_None
static const char * GetAsString(enum UnionTypes t)
MHOctetString m_strVal
Definition: BaseClasses.h:304
int FILE
Definition: mythburn.py:138