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  BASE *ptr = (BASE*)realloc(m_Values, (m_VecSize+1) * sizeof(BASE));
55  if (ptr == nullptr) 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 {0};
71  BASE *m_Values {nullptr};
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() {
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() {
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.
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  MHOctetString(const MHOctetString& o) { Copy(o); }
113  virtual ~MHOctetString();
114 
115  void Copy(const MHOctetString &str);
116  MHOctetString& operator=(const MHOctetString& o) {Copy(o); return *this; }
117  int Size() const { return m_nLength; }
118  // Comparison - returns <0, =0, >0 depending on the ordering.
119  int Compare(const MHOctetString &str) const;
120  bool Equal(const MHOctetString &str) const { return Compare(str) == 0; }
121  unsigned char GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_pChars[i]; }
122  const unsigned char *Bytes() const { return m_pChars; } // Read-only pointer to the buffer.
123  void Append(const MHOctetString &str); // Add text to the end of the string.
124 
125  QString Printable() const { return QString::fromLatin1((const char*)m_pChars, m_nLength); }
126 
127  void PrintMe(FILE *fd, int nTabs) const;
128 
129 protected:
131  unsigned char *m_pChars {nullptr};
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.
136 class 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 
158  MHObjectRef& operator=(const MHObjectRef&) = default;
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:
175 
176  MHContentRef& operator=(const MHContentRef&) = default;
177 
178  void Initialise(MHParseNode *p, MHEngine *engine);
179  void PrintMe(FILE *fd, int nTabs) const { m_ContentRef.PrintMe(fd, nTabs); }
180  void Copy(const MHContentRef &cr) { m_ContentRef.Copy(cr.m_ContentRef); }
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
195 protected:
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.
206 protected:
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.
217 protected:
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.
228 protected:
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.
239 protected:
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.
250 protected:
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:
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 { P_Int, P_Bool, P_String, P_ObjRef, P_ContentRef, P_Null } m_Type; // Null is used when this is optional
264 
270 };
271 
272 // A union type. Returned when a parameter is evaluated.
273 class MHUnion
274 {
275  public:
276  MHUnion() = default;
277  MHUnion(int nVal) : m_Type(U_Int), m_nIntVal(nVal) {}
278  MHUnion(bool fVal) : m_Type(U_Bool), m_nIntVal(0), m_fBoolVal(fVal) {}
279  MHUnion(const MHOctetString &strVal) : m_Type(U_String) { m_StrVal.Copy(strVal); }
280  MHUnion(const MHObjectRef &objVal) : m_Type(U_ObjRef) { m_ObjRefVal.Copy(objVal); };
281  MHUnion(const MHContentRef &cnVal) : m_Type(U_ContentRef) { m_ContentRefVal.Copy(cnVal); }
282 
283  MHUnion& operator=(const MHUnion&) = default;
284 
285  void GetValueFrom(const MHParameter &value, MHEngine *engine); // Copies the argument, getting the value of an indirect args.
286  QString Printable() const;
287 
289  void CheckType (enum UnionTypes) const; // Check a type and fail if it doesn't match.
290  static const char *GetAsString(enum UnionTypes t);
291 
292  int m_nIntVal {0};
293  bool m_fBoolVal {false};
297 };
298 
299 class MHFontBody {
300  // A font body can either be a string or an object reference
301  public:
303  void Initialise(MHParseNode *p, MHEngine *engine);
304  void PrintMe(FILE *fd, int nTabs) const;
305  bool IsSet() const { return m_DirFont.Size() != 0 || m_IndirFont.IsSet(); }
306  void Copy(const MHFontBody &fb);
307 protected:
310 };
311 
312 // This is used only in DynamicLineArt
313 class MHPointArg {
314  public:
316  void Initialise(MHParseNode *p, MHEngine *engine);
317  void PrintMe(FILE *fd, int nTabs) const;
319 };
320 
321 #endif
void Initialise(MHParseNode *p, MHEngine *engine)
MHUnion(int nVal)
Definition: BaseClasses.h:277
BASE * m_Values
Definition: BaseClasses.h:71
bool m_fBoolVal
Definition: BaseClasses.h:293
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.h:179
void Initialise(MHParseNode *p, MHEngine *engine)
void Append(const MHOctetString &str)
int m_nIntVal
Definition: BaseClasses.h:292
void Initialise(MHParseNode *p, MHEngine *engine)
bool GetValue(MHEngine *engine) const
bool IsSet() const
Definition: BaseClasses.h:161
void PrintMe(FILE *fd, int nTabs) const
void PrintMe(FILE *fd, int nTabs) const
MHOctetString m_Direct
Definition: BaseClasses.h:229
MHGenericInteger()=default
MHContentRef & operator=(const MHContentRef &)=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:183
MHSequence()=default
bool IsSet() const
Definition: BaseClasses.h:141
BASE Pop()
Definition: BaseClasses.h:87
void Initialise(MHParseNode *p, MHEngine *engine)
QString Printable() const
enum MHParameter::ParamTypes m_Type
int GetValue(MHEngine *engine) const
MHOctetString m_DirFont
Definition: BaseClasses.h:308
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:122
MHOctetString & operator=(const MHOctetString &o)
Definition: BaseClasses.h:116
unsigned char GetAt(int i) const
Definition: BaseClasses.h:121
void PrintMe(FILE *fd, int nTabs) const
void Copy(const MHColour &col)
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef m_ContentRefVal
Definition: BaseClasses.h:296
void GetValueFrom(const MHParameter &value, MHEngine *engine)
static const char * GetAsString(enum UnionTypes t)
MHGenericInteger m_y
Definition: BaseClasses.h:318
int Size() const
Definition: BaseClasses.h:98
unsigned char b
Definition: ParseText.cpp:329
MHGenericContentRef m_ContentRefVal
Definition: BaseClasses.h:269
MHGenericBoolean m_BoolVal
Definition: BaseClasses.h:266
bool Equal(const MHContentRef &cr, MHEngine *engine) const
MHGenericInteger m_x
Definition: BaseClasses.h:318
void Copy(const MHContentRef &cr)
Definition: BaseClasses.h:180
void GetValue(MHObjectRef &ref, MHEngine *engine) const
MHObjectRef m_IndirFont
Definition: BaseClasses.h:309
MHUnion & operator=(const MHUnion &)=default
MHGenericOctetString m_StrVal
Definition: BaseClasses.h:267
void Initialise(MHParseNode *p, MHEngine *engine)
void CheckType(enum UnionTypes) const
MHObjectRef * GetReference()
void SetFromString(const char *str, int nLen)
void Copy(const MHFontBody &fb)
MHUnion(const MHContentRef &cnVal)
Definition: BaseClasses.h:281
void Copy(const MHOctetString &str)
int Size() const
Definition: BaseClasses.h:117
void RemoveAt(int i)
Definition: BaseClasses.h:64
MHGenericBoolean()=default
QString Printable() const
Definition: BaseClasses.h:184
QString Printable() const
unsigned char t
Definition: ParseText.cpp:329
int m_VecSize
Definition: BaseClasses.h:70
QString Printable() const
Definition: BaseClasses.h:125
bool Equal(const MHOctetString &str) const
Definition: BaseClasses.h:120
void Initialise(MHParseNode *p, MHEngine *engine)
MHObjectRef m_ObjRef
Definition: BaseClasses.h:240
void PrintMe(FILE *fd, int nTabs) const
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:112
int FILE
Definition: mythburn.py:110
void Initialise(MHParseNode *p, MHEngine *engine)
void PrintMe(FILE *fd, int nTabs) const
BASE operator[](int i) const
Definition: BaseClasses.h:49
unsigned char * m_pChars
Definition: BaseClasses.h:131
bool IsSet() const
Definition: BaseClasses.h:305
BASE Top()
Definition: BaseClasses.h:94
bool IsSet() const
Definition: BaseClasses.h:181
int m_nColIndex
Definition: BaseClasses.h:145
MHContentRef m_Direct
Definition: BaseClasses.h:251
void Initialise(MHParseNode *p, MHEngine *engine)
MHOctetString m_StrVal
Definition: BaseClasses.h:294
MHColour()=default
void PrintMe(FILE *fd, int nTabs) const
MHGenericObjectRef m_ObjRefVal
Definition: BaseClasses.h:268
MHObjectRef & operator=(const MHObjectRef &)=default
MHOctetString m_ContentRef
Definition: BaseClasses.h:186
MHUnion(bool fVal)
Definition: BaseClasses.h:278
virtual ~MHOctetString()
void Initialise(MHParseNode *p, MHEngine *engine)
MHOctetString m_GroupId
Definition: BaseClasses.h:167
MHObjectRef m_ObjRefVal
Definition: BaseClasses.h:295
MHGenericInteger m_IntVal
Definition: BaseClasses.h:265
void Push(BASE b)
Definition: BaseClasses.h:92
void Append(BASE b)
Definition: BaseClasses.h:62
int Compare(const MHOctetString &str) const
MHObjectRef m_Indirect
Definition: BaseClasses.h:196
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:280
bool Empty() const
Definition: BaseClasses.h:85
MHOctetString m_ColStr
Definition: BaseClasses.h:144
MHUnion(const MHOctetString &strVal)
Definition: BaseClasses.h:279
static MHObjectRef Null
Definition: BaseClasses.h:156