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);
118  { if (this==&o) return *this; Copy(o); return *this; }
119  int Size() const { return m_nLength; }
120  // Comparison - returns <0, =0, >0 depending on the ordering.
121  int Compare(const MHOctetString &str) const;
122  bool Equal(const MHOctetString &str) const { return Compare(str) == 0; }
123  unsigned char GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_pChars[i]; }
124  const unsigned char *Bytes() const { return m_pChars; } // Read-only pointer to the buffer.
125  void Append(const MHOctetString &str); // Add text to the end of the string.
126 
127  QString Printable() const { return QString::fromLatin1((const char*)m_pChars, m_nLength); }
128 
129  void PrintMe(FILE *fd, int nTabs) const;
130 
131 protected:
132  int m_nLength {0};
133  unsigned char *m_pChars {nullptr};
134 };
135 
136 // A colour is encoded as a string or the index into a palette.
137 // Palettes aren't defined in UK MHEG so the palette index isn't really relevant.
138 class MHColour {
139  public:
140  MHColour() = default;
141  void Initialise(MHParseNode *p, MHEngine *engine);
142  void PrintMe(FILE *fd, int nTabs) const;
143  bool IsSet() const { return m_nColIndex >= 0 || m_ColStr.Size() != 0; }
144  void SetFromString(const char *str, int nLen);
145  void Copy(const MHColour &col);
147  int m_nColIndex {-1};
148 };
149 
150 // An object reference is used to identify and refer to an object.
151 // Internal objects have the m_pGroupId field empty.
153 {
154  public:
155  MHObjectRef() = default;
156  void Initialise(MHParseNode *p, MHEngine *engine);
157  void Copy(const MHObjectRef &objr);
159 
160  MHObjectRef& operator=(const MHObjectRef&) = default;
161 
162  // Sometimes the object reference is optional. This tests if it has been set
163  bool IsSet() const { return (m_nObjectNo != 0 || m_GroupId.Size() != 0); }
164  void PrintMe(FILE *fd, int nTabs) const;
165  bool Equal(const MHObjectRef &objr, MHEngine *engine) const;
166  QString Printable() const;
167 
168  int m_nObjectNo {0};
170 };
171 
172 // A content reference gives the location (e.g. file name) to find the content.
174 {
175  public:
176  MHContentRef() = default;
177 
178  MHContentRef& operator=(const MHContentRef&) = default;
179 
180  void Initialise(MHParseNode *p, MHEngine *engine);
181  void PrintMe(FILE *fd, int nTabs) const { m_ContentRef.PrintMe(fd, nTabs); }
182  void Copy(const MHContentRef &cr) { m_ContentRef.Copy(cr.m_ContentRef); }
183  bool IsSet() const { return m_ContentRef.Size() != 0; }
184  bool Equal(const MHContentRef &cr, MHEngine *engine) const;
186  QString Printable() const { return m_ContentRef.Printable(); }
187 
189 };
190 
191 // "Generic" versions of int, bool etc can be either the value or an indirect reference.
193 {
194  public:
195  MHObjectRef *GetReference(); // Return the indirect reference or fail if it's direct
196  bool m_fIsDirect {false};
197 protected:
199 };
200 
202 {
203  public:
204  MHGenericBoolean() = default;
205  void Initialise(MHParseNode *p, MHEngine *engine);
206  void PrintMe(FILE *fd, int nTabs) const;
207  bool GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
208 protected:
209  bool m_fDirect {false};
210 };
211 
213 {
214  public:
215  MHGenericInteger() = default;
216  void Initialise(MHParseNode *p, MHEngine *engine);
217  void PrintMe(FILE *fd, int nTabs) const;
218  int GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
219 protected:
220  int m_nDirect {-1};
221 };
222 
224 {
225  public:
226  MHGenericOctetString() = default;
227  void Initialise(MHParseNode *p, MHEngine *engine);
228  void PrintMe(FILE *fd, int nTabs) const;
229  void GetValue(MHOctetString &str, MHEngine *engine) const; // Return the value, looking up any indirect ref.
230 protected:
232 };
233 
235 {
236  public:
237  MHGenericObjectRef() = default;
238  void Initialise(MHParseNode *p, MHEngine *engine);
239  void PrintMe(FILE *fd, int nTabs) const;
240  void GetValue(MHObjectRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
241 protected:
243 };
244 
246 {
247  public:
248  MHGenericContentRef() = default;
249  void Initialise(MHParseNode *p, MHEngine *engine);
250  void PrintMe(FILE *fd, int nTabs) const;
251  void GetValue(MHContentRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
252 protected:
254 };
255 
256 // In certain cases (e.g. parameters to Call) we have values which are the union of the base types.
258 {
259  public:
260  MHParameter() = default;
261  void Initialise(MHParseNode *p, MHEngine *engine);
262  void PrintMe(FILE *fd, int nTabs) const;
263  MHObjectRef *GetReference(); // Get an indirect reference.
264 
265  enum ParamTypes {
272  } m_Type { P_Null }; // Null is used when this is optional
273 
279 };
280 
281 // A union type. Returned when a parameter is evaluated.
282 class MHUnion
283 {
284  public:
285  MHUnion() = default;
286  MHUnion(int nVal) : m_Type(U_Int), m_nIntVal(nVal) {}
287  MHUnion(bool fVal) : m_Type(U_Bool), m_fBoolVal(fVal) {}
288  MHUnion(const MHOctetString &strVal) : m_Type(U_String) { m_StrVal.Copy(strVal); }
289  MHUnion(const MHObjectRef &objVal) : m_Type(U_ObjRef) { m_ObjRefVal.Copy(objVal); };
290  MHUnion(const MHContentRef &cnVal) : m_Type(U_ContentRef) { m_ContentRefVal.Copy(cnVal); }
291 
292  MHUnion& operator=(const MHUnion&) = default;
293 
294  void GetValueFrom(const MHParameter &value, MHEngine *engine); // Copies the argument, getting the value of an indirect args.
295  QString Printable() const;
296 
298  void CheckType (enum UnionTypes t) const; // Check a type and fail if it doesn't match.
299  static const char *GetAsString(enum UnionTypes t);
300 
301  int m_nIntVal {0};
302  bool m_fBoolVal {false};
306 };
307 
308 class MHFontBody {
309  // A font body can either be a string or an object reference
310  public:
311  MHFontBody() = default;
312  void Initialise(MHParseNode *p, MHEngine *engine);
313  void PrintMe(FILE *fd, int nTabs) const;
314  bool IsSet() const { return m_DirFont.Size() != 0 || m_IndirFont.IsSet(); }
315  void Copy(const MHFontBody &fb);
316 protected:
319 };
320 
321 // This is used only in DynamicLineArt
322 class MHPointArg {
323  public:
324  MHPointArg() = default;
325  void Initialise(MHParseNode *p, MHEngine *engine);
326  void PrintMe(FILE *fd, int nTabs) const;
328 };
329 
330 #endif
void Initialise(MHParseNode *p, MHEngine *engine)
MHUnion(int nVal)
Definition: BaseClasses.h:286
BASE * m_Values
Definition: BaseClasses.h:72
bool m_fBoolVal
Definition: BaseClasses.h:302
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.h:181
void Initialise(MHParseNode *p, MHEngine *engine)
void Append(const MHOctetString &str)
void CheckType(enum UnionTypes t) const
int m_nIntVal
Definition: BaseClasses.h:301
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef()=default
bool GetValue(MHEngine *engine) const
bool IsSet() const
Definition: BaseClasses.h:163
void PrintMe(FILE *fd, int nTabs) const
void PrintMe(FILE *fd, int nTabs) const
MHOctetString m_Direct
Definition: BaseClasses.h:231
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:185
MHSequence()=default
bool IsSet() const
Definition: BaseClasses.h:143
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:317
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:124
MHOctetString & operator=(const MHOctetString &o)
Definition: BaseClasses.h:117
unsigned char GetAt(int i) const
Definition: BaseClasses.h:123
void PrintMe(FILE *fd, int nTabs) const
void Copy(const MHColour &col)
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef m_ContentRefVal
Definition: BaseClasses.h:305
void GetValueFrom(const MHParameter &value, MHEngine *engine)
static const char * GetAsString(enum UnionTypes t)
MHGenericInteger m_y
Definition: BaseClasses.h:327
int Size() const
Definition: BaseClasses.h:99
MHGenericContentRef m_ContentRefVal
Definition: BaseClasses.h:278
MHGenericBoolean m_BoolVal
Definition: BaseClasses.h:275
bool Equal(const MHContentRef &cr, MHEngine *engine) const
MHGenericInteger m_x
Definition: BaseClasses.h:327
void Copy(const MHContentRef &cr)
Definition: BaseClasses.h:182
void GetValue(MHObjectRef &ref, MHEngine *engine) const
MHObjectRef m_IndirFont
Definition: BaseClasses.h:318
MHUnion & operator=(const MHUnion &)=default
MHGenericOctetString m_StrVal
Definition: BaseClasses.h:276
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:290
void Copy(const MHOctetString &str)
int Size() const
Definition: BaseClasses.h:119
void RemoveAt(int i)
Definition: BaseClasses.h:65
MHGenericBoolean()=default
QString Printable() const
Definition: BaseClasses.h:186
QString Printable() const
int m_VecSize
Definition: BaseClasses.h:71
QString Printable() const
Definition: BaseClasses.h:127
bool Equal(const MHOctetString &str) const
Definition: BaseClasses.h:122
void Initialise(MHParseNode *p, MHEngine *engine)
MHGenericOctetString()=default
MHObjectRef m_ObjRef
Definition: BaseClasses.h:242
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:139
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:133
bool IsSet() const
Definition: BaseClasses.h:314
BASE Top()
Definition: BaseClasses.h:95
bool IsSet() const
Definition: BaseClasses.h:183
int m_nColIndex
Definition: BaseClasses.h:147
MHContentRef m_Direct
Definition: BaseClasses.h:253
void Initialise(MHParseNode *p, MHEngine *engine)
MHOctetString m_StrVal
Definition: BaseClasses.h:303
MHColour()=default
void PrintMe(FILE *fd, int nTabs) const
MHGenericObjectRef m_ObjRefVal
Definition: BaseClasses.h:277
MHObjectRef & operator=(const MHObjectRef &)=default
MHParameter()=default
MHOctetString m_ContentRef
Definition: BaseClasses.h:188
MHUnion(bool fVal)
Definition: BaseClasses.h:287
virtual ~MHOctetString()
void Initialise(MHParseNode *p, MHEngine *engine)
MHOctetString m_GroupId
Definition: BaseClasses.h:169
MHObjectRef m_ObjRefVal
Definition: BaseClasses.h:304
MHGenericInteger m_IntVal
Definition: BaseClasses.h:274
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:198
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:289
bool Empty() const
Definition: BaseClasses.h:86
MHOctetString m_ColStr
Definition: BaseClasses.h:146
MHUnion(const MHOctetString &strVal)
Definition: BaseClasses.h:288
static MHObjectRef Null
Definition: BaseClasses.h:158