MythTV  0.28pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
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(){ m_VecSize = 0; m_Values = 0; }
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 == NULL) 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;
71  BASE *m_Values;
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() {
88  MHASSERT(MHSequence<BASE>::m_VecSize > 0);
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() {
95  MHASSERT(MHSequence<BASE>::m_VecSize > 0);
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  virtual ~MHOctetString();
113 
114  void Copy(const MHOctetString &str);
115  int Size() const { return m_nLength; }
116  // Comparison - returns <0, =0, >0 depending on the ordering.
117  int Compare(const MHOctetString &str) const;
118  bool Equal(const MHOctetString &str) const { return Compare(str) == 0; }
119  unsigned char GetAt(int i) const { MHASSERT(i >= 0 && i < Size()); return m_pChars[i]; }
120  const unsigned char *Bytes() const { return m_pChars; } // Read-only pointer to the buffer.
121  void Append(const MHOctetString &str); // Add text to the end of the string.
122 
123  QString Printable() const { return QString::fromLatin1((const char*)m_pChars, m_nLength); }
124 
125  void PrintMe(FILE *fd, int nTabs) const;
126 
127 protected:
129  unsigned char *m_pChars;
130 };
131 
132 // A colour is encoded as a string or the index into a palette.
133 // Palettes aren't defined in UK MHEG so the palette index isn't really relevant.
134 class MHColour {
135  public:
137  void Initialise(MHParseNode *p, MHEngine *engine);
138  void PrintMe(FILE *fd, int nTabs) const;
139  bool IsSet() const { return m_nColIndex >= 0 || m_ColStr.Size() != 0; }
140  void SetFromString(const char *str, int nLen);
141  void Copy(const MHColour &col);
144 };
145 
146 // An object reference is used to identify and refer to an object.
147 // Internal objects have the m_pGroupId field empty.
149 {
150  public:
152  void Initialise(MHParseNode *p, MHEngine *engine);
153  void Copy(const MHObjectRef &objr);
155 
156  // Sometimes the object reference is optional. This tests if it has been set
157  bool IsSet() const { return (m_nObjectNo != 0 || m_GroupId.Size() != 0); }
158  void PrintMe(FILE *fd, int nTabs) const;
159  bool Equal(const MHObjectRef &objr, MHEngine *engine) const;
160  QString Printable() const;
161 
164 };
165 
166 // A content reference gives the location (e.g. file name) to find the content.
168 {
169  public:
171  void Initialise(MHParseNode *p, MHEngine *engine);
172  void PrintMe(FILE *fd, int nTabs) const { m_ContentRef.PrintMe(fd, nTabs); }
173  void Copy(const MHContentRef &cr) { m_ContentRef.Copy(cr.m_ContentRef); }
174  bool IsSet() const { return m_ContentRef.Size() != 0; }
175  bool Equal(const MHContentRef &cr, MHEngine *engine) const;
177  QString Printable() const { return m_ContentRef.Printable(); }
178 
180 };
181 
182 // "Generic" versions of int, bool etc can be either the value or an indirect reference.
184 {
185  public:
186  MHObjectRef *GetReference(); // Return the indirect reference or fail if it's direct
188 protected:
190 };
191 
193 {
194  public:
196  void Initialise(MHParseNode *p, MHEngine *engine);
197  void PrintMe(FILE *fd, int nTabs) const;
198  bool GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
199 protected:
200  bool m_fDirect;
201 };
202 
204 {
205  public:
207  void Initialise(MHParseNode *p, MHEngine *engine);
208  void PrintMe(FILE *fd, int nTabs) const;
209  int GetValue(MHEngine *engine) const; // Return the value, looking up any indirect ref.
210 protected:
212 };
213 
215 {
216  public:
218  void Initialise(MHParseNode *p, MHEngine *engine);
219  void PrintMe(FILE *fd, int nTabs) const;
220  void GetValue(MHOctetString &str, MHEngine *engine) const; // Return the value, looking up any indirect ref.
221 protected:
223 };
224 
226 {
227  public:
229  void Initialise(MHParseNode *p, MHEngine *engine);
230  void PrintMe(FILE *fd, int nTabs) const;
231  void GetValue(MHObjectRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
232 protected:
234 };
235 
237 {
238  public:
240  void Initialise(MHParseNode *p, MHEngine *engine);
241  void PrintMe(FILE *fd, int nTabs) const;
242  void GetValue(MHContentRef &ref, MHEngine *engine) const; // Return the value, looking up any indirect ref.
243 protected:
245 };
246 
247 // In certain cases (e.g. parameters to Call) we have values which are the union of the base types.
249 {
250  public:
252  void Initialise(MHParseNode *p, MHEngine *engine);
253  void PrintMe(FILE *fd, int nTabs) const;
254  MHObjectRef *GetReference(); // Get an indirect reference.
255 
256  enum ParamTypes { P_Int, P_Bool, P_String, P_ObjRef, P_ContentRef, P_Null } m_Type; // Null is used when this is optional
257 
263 };
264 
265 // A union type. Returned when a parameter is evaluated.
266 class MHUnion
267 {
268  public:
270  MHUnion(int nVal) : m_Type(U_Int), m_nIntVal(nVal), m_fBoolVal(false) {}
271  MHUnion(bool fVal) : m_Type(U_Bool), m_nIntVal(0), m_fBoolVal(fVal) {}
275 
276  void GetValueFrom(const MHParameter &value, MHEngine *engine); // Copies the argument, getting the value of an indirect args.
277  QString Printable() const;
278 
280  void CheckType (enum UnionTypes) const; // Check a type and fail if it doesn't match.
281  static const char *GetAsString(enum UnionTypes t);
282 
288 };
289 
290 class MHFontBody {
291  // A font body can either be a string or an object reference
292  public:
294  void Initialise(MHParseNode *p, MHEngine *engine);
295  void PrintMe(FILE *fd, int nTabs) const;
296  bool IsSet() const { return m_DirFont.Size() != 0 || m_IndirFont.IsSet(); }
297  void Copy(const MHFontBody &fb);
298 protected:
301 };
302 
303 // This is used only in DynamicLineArt
304 class MHPointArg {
305  public:
307  void Initialise(MHParseNode *p, MHEngine *engine);
308  void PrintMe(FILE *fd, int nTabs) const;
310 };
311 
312 #endif
void Initialise(MHParseNode *p, MHEngine *engine)
MHUnion(int nVal)
Definition: BaseClasses.h:270
BASE * m_Values
Definition: BaseClasses.h:71
bool m_fBoolVal
Definition: BaseClasses.h:284
void Initialise(MHParseNode *p, MHEngine *engine)
void Append(const MHOctetString &str)
int m_nIntVal
Definition: BaseClasses.h:283
void Initialise(MHParseNode *p, MHEngine *engine)
QString Printable() const
Definition: BaseClasses.h:123
int Size() const
Definition: BaseClasses.h:115
QString Printable() const
bool IsSet() const
Definition: BaseClasses.h:157
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger y
Definition: BaseClasses.h:309
void PrintMe(FILE *fd, int nTabs) const
MHOctetString m_Direct
Definition: BaseClasses.h:222
BASE GetAt(int i) const
Definition: BaseClasses.h:48
void PrintMe(FILE *fd, int nTabs) const
static MHContentRef Null
Definition: BaseClasses.h:176
const unsigned char * Bytes() const
Definition: BaseClasses.h:120
lzo_voidp ptr
Definition: lzoconf.h:275
BASE Pop()
Definition: BaseClasses.h:87
void Initialise(MHParseNode *p, MHEngine *engine)
unsigned char GetAt(int i) const
Definition: BaseClasses.h:119
QString Printable() const
Definition: BaseClasses.h:177
enum MHParameter::ParamTypes m_Type
MHGenericInteger x
Definition: BaseClasses.h:309
MHOctetString m_DirFont
Definition: BaseClasses.h:299
void InsertAt(BASE b, int n)
Definition: BaseClasses.h:52
void Copy(const MHObjectRef &objr)
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:83
unsigned char t
Definition: ParseText.cpp:339
void Copy(const MHColour &col)
void Initialise(MHParseNode *p, MHEngine *engine)
MHContentRef m_ContentRefVal
Definition: BaseClasses.h:287
void GetValueFrom(const MHParameter &value, MHEngine *engine)
static const char * GetAsString(enum UnionTypes t)
unsigned char b
Definition: ParseText.cpp:339
MHGenericContentRef m_ContentRefVal
Definition: BaseClasses.h:262
MHGenericBoolean m_BoolVal
Definition: BaseClasses.h:259
void Copy(const MHContentRef &cr)
Definition: BaseClasses.h:173
MHObjectRef m_IndirFont
Definition: BaseClasses.h:300
MHGenericOctetString m_StrVal
Definition: BaseClasses.h:260
void Initialise(MHParseNode *p, MHEngine *engine)
MHObjectRef * GetReference()
enum MHUnion::UnionTypes m_Type
void SetFromString(const char *str, int nLen)
void PrintMe(FILE *fd, int nTabs) const
void Copy(const MHFontBody &fb)
MHUnion(const MHContentRef &cnVal)
Definition: BaseClasses.h:274
void Copy(const MHOctetString &str)
int Size() const
Definition: BaseClasses.h:46
void GetValue(MHContentRef &ref, MHEngine *engine) const
void RemoveAt(int i)
Definition: BaseClasses.h:64
bool GetValue(MHEngine *engine) const
void GetValue(MHOctetString &str, MHEngine *engine) const
int m_VecSize
Definition: BaseClasses.h:70
int Compare(const MHOctetString &str) const
void Initialise(MHParseNode *p, MHEngine *engine)
QString Printable() const
MHObjectRef m_ObjRef
Definition: BaseClasses.h:233
void Initialise(MHParseNode *p, MHEngine *engine)
bool Equal(const MHObjectRef &objr, MHEngine *engine) const
void GetValue(MHObjectRef &ref, MHEngine *engine) const
int GetValue(MHEngine *engine) const
bool Equal(const MHOctetString &str) const
Definition: BaseClasses.h:118
int FILE
Definition: mythburn.py:110
void Initialise(MHParseNode *p, MHEngine *engine)
lzo_uintptr_t p
Definition: minilzo.cpp:2207
void PrintMe(FILE *fd, int nTabs) const
unsigned char * m_pChars
Definition: BaseClasses.h:129
bool IsSet() const
Definition: BaseClasses.h:139
BASE Top()
Definition: BaseClasses.h:94
int m_nColIndex
Definition: BaseClasses.h:143
MHContentRef m_Direct
Definition: BaseClasses.h:244
void Initialise(MHParseNode *p, MHEngine *engine)
MHOctetString m_StrVal
Definition: BaseClasses.h:285
void CheckType(enum UnionTypes) const
bool Empty() const
Definition: BaseClasses.h:85
MHGenericObjectRef m_ObjRefVal
Definition: BaseClasses.h:261
BASE operator[](int i) const
Definition: BaseClasses.h:49
MHOctetString m_ContentRef
Definition: BaseClasses.h:179
void PrintMe(FILE *fd, int nTabs) const
MHUnion(bool fVal)
Definition: BaseClasses.h:271
virtual ~MHOctetString()
void Initialise(MHParseNode *p, MHEngine *engine)
bool IsSet() const
Definition: BaseClasses.h:296
void PrintMe(FILE *fd, int nTabs) const
Definition: BaseClasses.h:172
MHOctetString m_GroupId
Definition: BaseClasses.h:163
bool IsSet() const
Definition: BaseClasses.h:174
int Size() const
Definition: BaseClasses.h:98
void PrintMe(FILE *fd, int nTabs) const
MHObjectRef m_ObjRefVal
Definition: BaseClasses.h:286
void PrintMe(FILE *fd, int nTabs) const
MHGenericInteger m_IntVal
Definition: BaseClasses.h:258
void Push(BASE b)
Definition: BaseClasses.h:92
void Append(BASE b)
Definition: BaseClasses.h:62
MHObjectRef m_Indirect
Definition: BaseClasses.h:189
void PrintMe(FILE *fd, int nTabs) const
void PrintMe(FILE *fd, int nTabs) const
bool Equal(const MHContentRef &cr, MHEngine *engine) const
void PrintMe(FILE *fd, int nTabs) const
MHObjectRef * GetReference()
MHUnion(const MHObjectRef &objVal)
Definition: BaseClasses.h:273
MHOctetString m_ColStr
Definition: BaseClasses.h:142
MHUnion(const MHOctetString &strVal)
Definition: BaseClasses.h:272
static MHObjectRef Null
Definition: BaseClasses.h:154