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