Classes | Public Types | Public Member Functions | Private Attributes | List of all members
TemplateVariant Class Reference

Variant type which can hold one value of a fixed set of types. More...

#include <template.h>

Classes

class  Delegate
 Helper class to create a delegate that can store a function/method call. More...
 

Public Types

enum  Type {
  None, Bool, Integer, String,
  Struct, List, Function
}
 

Public Member Functions

Type type () const
 
QCString typeAsString () const
 
bool isValid () const
 
 TemplateVariant ()
 
 TemplateVariant (bool b)
 
 TemplateVariant (int v)
 
 TemplateVariant (const char *s, bool raw=FALSE)
 
 TemplateVariant (const QCString &s, bool raw=FALSE)
 
 TemplateVariant (TemplateStructIntf *s)
 
 TemplateVariant (TemplateListIntf *l)
 
 TemplateVariant (const Delegate &delegate)
 
 ~TemplateVariant ()
 
 TemplateVariant (const TemplateVariant &v)
 
TemplateVariantoperator= (const TemplateVariant &v)
 
bool operator== (TemplateVariant &other)
 
QCString toString () const
 
bool toBool () const
 
int toInt () const
 
TemplateListIntftoList () const
 
TemplateStructIntftoStruct () const
 
TemplateVariant call (const QValueList< TemplateVariant > &args)
 
void setRaw (bool b)
 
bool raw () const
 

Private Attributes

Type m_type
 
QCString m_strVal
 
union {
   int   m_intVal
 
   bool   m_boolVal
 
   TemplateStructIntf *   m_strukt
 
   TemplateListIntf *   m_list
 
}; 
 
Delegate m_delegate
 
bool m_raw
 

Detailed Description

Variant type which can hold one value of a fixed set of types.

Definition at line 90 of file template.h.

Member Enumeration Documentation

Types of data that can be stored in a TemplateVariant

Enumerator
None 
Bool 
Integer 
String 
Struct 
List 
Function 

Definition at line 139 of file template.h.

Constructor & Destructor Documentation

TemplateVariant::TemplateVariant ( )
inline

Constructs an invalid variant.

Definition at line 164 of file template.h.

164 : m_type(None), m_strukt(0), m_raw(FALSE) {}
const bool FALSE
Definition: qglobal.h:370
TemplateStructIntf * m_strukt
Definition: template.h:296
TemplateVariant::TemplateVariant ( bool  b)
inlineexplicit

Constructs a new variant with a boolean value b.

Definition at line 167 of file template.h.

167 : m_type(Bool), m_boolVal(b), m_raw(FALSE) {}
const bool FALSE
Definition: qglobal.h:370
static bool * b
Definition: config.cpp:1043
TemplateVariant::TemplateVariant ( int  v)
inline

Constructs a new variant with a integer value v.

Definition at line 170 of file template.h.

170 : m_type(Integer), m_intVal(v), m_raw(FALSE) {}
const bool FALSE
Definition: qglobal.h:370
TemplateVariant::TemplateVariant ( const char *  s,
bool  raw = FALSE 
)
inline

Constructs a new variant with a string value s.

Definition at line 173 of file template.h.

173 : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
Raw data description.
QCString m_strVal
Definition: template.h:291
TemplateStructIntf * m_strukt
Definition: template.h:296
static QCString * s
Definition: config.cpp:1042
TemplateVariant::TemplateVariant ( const QCString s,
bool  raw = FALSE 
)
inline

Constructs a new variant with a string value s.

Definition at line 176 of file template.h.

176 : m_type(String), m_strVal(s), m_strukt(0), m_raw(raw) {}
Raw data description.
QCString m_strVal
Definition: template.h:291
TemplateStructIntf * m_strukt
Definition: template.h:296
TemplateVariant::TemplateVariant ( TemplateStructIntf s)

Constructs a new variant with a struct value s.

Note
. The variant will hold a reference to the object.

Definition at line 146 of file template.cpp.

147  : m_type(Struct), m_strukt(s), m_raw(FALSE)
148 {
149  m_strukt->addRef();
150 }
const bool FALSE
Definition: qglobal.h:370
TemplateStructIntf * m_strukt
Definition: template.h:296
virtual int addRef()=0
TemplateVariant::TemplateVariant ( TemplateListIntf l)

Constructs a new variant with a list value l.

Note
. The variant will hold a reference to the object.

Definition at line 152 of file template.cpp.

153  : m_type(List), m_list(l), m_raw(FALSE)
154 {
155  m_list->addRef();
156 }
TemplateListIntf * m_list
Definition: template.h:297
const bool FALSE
Definition: qglobal.h:370
virtual int addRef()=0
TemplateVariant::TemplateVariant ( const Delegate delegate)
inline

Constructs a new variant which represents a method call

Parameters
[in]delegateDelegate object to invoke when calling call() on this variant.
Note
Use TemplateVariant::Delegate::fromMethod() and TemplateVariant::Delegate::fromFunction() to create Delegate objects.

Definition at line 195 of file template.h.

195 : m_type(Function), m_strukt(0), m_delegate(delegate), m_raw(FALSE) {}
const bool FALSE
Definition: qglobal.h:370
Delegate m_delegate
Definition: template.h:299
TemplateStructIntf * m_strukt
Definition: template.h:296
TemplateVariant::~TemplateVariant ( )

Destroys the Variant object

Definition at line 158 of file template.cpp.

159 {
160  if (m_type==Struct) m_strukt->release();
161  else if (m_type==List) m_list->release();
162 }
TemplateListIntf * m_list
Definition: template.h:297
virtual int release()=0
TemplateStructIntf * m_strukt
Definition: template.h:296
virtual int release()=0
TemplateVariant::TemplateVariant ( const TemplateVariant v)

Constructs a copy of the variant, v, passed as the argument to this constructor.

Definition at line 164 of file template.cpp.

165  : m_type(v.m_type), m_strukt(0), m_raw(FALSE)
166 {
167  m_raw = v.m_raw;
168  switch (m_type)
169  {
170  case None: break;
171  case Bool: m_boolVal = v.m_boolVal; break;
172  case Integer: m_intVal = v.m_intVal; break;
173  case String: m_strVal = v.m_strVal; break;
174  case Struct: m_strukt = v.m_strukt; m_strukt->addRef(); break;
175  case List: m_list = v.m_list; m_list->addRef(); break;
176  case Function: m_delegate= v.m_delegate;break;
177  }
178 }
TemplateListIntf * m_list
Definition: template.h:297
const bool FALSE
Definition: qglobal.h:370
Delegate m_delegate
Definition: template.h:299
QCString m_strVal
Definition: template.h:291
TemplateStructIntf * m_strukt
Definition: template.h:296
virtual int addRef()=0
virtual int addRef()=0

Member Function Documentation

TemplateVariant TemplateVariant::call ( const QValueList< TemplateVariant > &  args)
inline

Return the result of apply this function with args. Returns an empty string if the variant type is not a function.

Definition at line 272 of file template.h.

273  {
274  if (m_type==Function) return m_delegate(args);
275  return TemplateVariant();
276  }
Delegate m_delegate
Definition: template.h:299
bool TemplateVariant::isValid ( ) const
inline

Returns TRUE if the variant holds a valid value, or FALSE otherwise

Definition at line 161 of file template.h.

161 { return m_type!=None; }
TemplateVariant & TemplateVariant::operator= ( const TemplateVariant v)

Assigns the value of the variant v to this variant.

Definition at line 180 of file template.cpp.

181 {
182  // assignment can change the type of the variable, so we have to be
183  // careful with reference counted content.
184  TemplateStructIntf *tmpStruct = m_type==Struct ? m_strukt : 0;
185  TemplateListIntf *tmpList = m_type==List ? m_list : 0;
186  Type tmpType = m_type;
187 
188  m_type = v.m_type;
189  m_raw = v.m_raw;
190  switch (m_type)
191  {
192  case None: break;
193  case Bool: m_boolVal = v.m_boolVal; break;
194  case Integer: m_intVal = v.m_intVal; break;
195  case String: m_strVal = v.m_strVal; break;
196  case Struct: m_strukt = v.m_strukt; m_strukt->addRef(); break;
197  case List: m_list = v.m_list; m_list->addRef(); break;
198  case Function: m_delegate= v.m_delegate;break;
199  }
200 
201  // release overwritten reference counted values
202  if (tmpType==Struct && tmpStruct) tmpStruct->release();
203  else if (tmpType==List && tmpList ) tmpList->release();
204  return *this;
205 }
int Type
Definition: 018_def.c:12
TemplateListIntf * m_list
Definition: template.h:297
virtual int release()=0
Abstract interface for a context value of type struct.
Definition: template.h:406
Delegate m_delegate
Definition: template.h:299
QCString m_strVal
Definition: template.h:291
TemplateStructIntf * m_strukt
Definition: template.h:296
virtual int addRef()=0
virtual int release()=0
virtual int addRef()=0
Abstract read-only interface for a context value of type list.
Definition: template.h:329
bool TemplateVariant::operator== ( TemplateVariant other)
inline

Compares this QVariant with v and returns true if they are equal; otherwise returns false.

Definition at line 211 of file template.h.

212  {
213  if (m_type==None)
214  {
215  return FALSE;
216  }
218  {
219  return m_list==other.m_list; // TODO: improve me
220  }
222  {
223  return m_strukt==other.m_strukt; // TODO: improve me
224  }
225  else
226  {
227  return toString()==other.toString();
228  }
229  }
TemplateListIntf * m_list
Definition: template.h:297
const bool FALSE
Definition: qglobal.h:370
QCString toString() const
Definition: template.h:232
TemplateStructIntf * m_strukt
Definition: template.h:296
bool TemplateVariant::raw ( ) const
inline

Returns whether or not the value of the Value is raw.

See also
setRaw()

Definition at line 287 of file template.h.

287 { return m_raw; }
void TemplateVariant::setRaw ( bool  b)
inline

Sets whether or not the value of the Variant should be escaped or written as-is (raw).

Parameters
[in]bTRUE means write as-is, FALSE means apply escaping.

Definition at line 282 of file template.h.

282 { m_raw = b; }
static bool * b
Definition: config.cpp:1043
bool TemplateVariant::toBool ( ) const

Returns the variant as a boolean.

Definition at line 207 of file template.cpp.

208 {
209  switch (m_type)
210  {
211  case None: return FALSE;
212  case Bool: return m_boolVal;
213  case Integer: return m_intVal!=0;
214  case String: return !m_strVal.isEmpty();
215  case Struct: return TRUE;
216  case List: return m_list->count()!=0;
217  case Function: return FALSE;
218  }
219  return FALSE;
220 }
bool isEmpty() const
Definition: qcstring.h:189
TemplateListIntf * m_list
Definition: template.h:297
const bool FALSE
Definition: qglobal.h:370
QCString m_strVal
Definition: template.h:291
virtual int count() const =0
const bool TRUE
Definition: qglobal.h:371
int TemplateVariant::toInt ( ) const

Returns the variant as an integer.

Definition at line 222 of file template.cpp.

223 {
224  switch (m_type)
225  {
226  case None: return 0;
227  case Bool: return m_boolVal ? 1 : 0;
228  case Integer: return m_intVal;
229  case String: return m_strVal.toInt();
230  case Struct: return 0;
231  case List: return m_list->count();
232  case Function: return 0;
233  }
234  return 0;
235 }
TemplateListIntf * m_list
Definition: template.h:297
QCString m_strVal
Definition: template.h:291
int toInt(bool *ok=0) const
Definition: qcstring.cpp:439
virtual int count() const =0
TemplateListIntf* TemplateVariant::toList ( ) const
inline

Returns the pointer to list referenced by this variant or 0 if this variant does not have list type.

Definition at line 256 of file template.h.

257  {
258  return m_type==List ? m_list : 0;
259  }
TemplateListIntf * m_list
Definition: template.h:297
QCString TemplateVariant::toString ( ) const
inline

Returns the variant as a string.

Definition at line 232 of file template.h.

233  {
234  switch (m_type)
235  {
236  case None: return QCString();
237  case Bool: return m_boolVal ? "true" : "false";
238  case Integer: return QCString().setNum(m_intVal);
239  case String: return m_strVal;
240  case Struct: return "[struct]";
241  case List: return "[list]";
242  case Function: return "[function]";
243  }
244  return QCString();
245  }
QCString m_strVal
Definition: template.h:291
QCString & setNum(short n)
Definition: qcstring.cpp:469
TemplateStructIntf* TemplateVariant::toStruct ( ) const
inline

Returns the pointer to struct referenced by this variant or 0 if this variant does not have struct type.

Definition at line 264 of file template.h.

265  {
266  return m_type==Struct ? m_strukt : 0;
267  }
TemplateStructIntf * m_strukt
Definition: template.h:296
Type TemplateVariant::type ( ) const
inline

Returns the type of the value stored in the variant

Definition at line 142 of file template.h.

142 { return m_type; }
QCString TemplateVariant::typeAsString ( ) const
inline

Return a string representation of the type of the value stored in the variant

Definition at line 145 of file template.h.

146  {
147  switch (m_type)
148  {
149  case None: return "none";
150  case Bool: return "bool";
151  case Integer: return "integer";
152  case String: return "string";
153  case Struct: return "struct";
154  case List: return "list";
155  case Function: return "function";
156  }
157  return "invalid";
158  }

Member Data Documentation

union { ... }
bool TemplateVariant::m_boolVal

Definition at line 295 of file template.h.

Delegate TemplateVariant::m_delegate
private

Definition at line 299 of file template.h.

int TemplateVariant::m_intVal

Definition at line 294 of file template.h.

TemplateListIntf* TemplateVariant::m_list

Definition at line 297 of file template.h.

bool TemplateVariant::m_raw
private

Definition at line 300 of file template.h.

TemplateStructIntf* TemplateVariant::m_strukt

Definition at line 296 of file template.h.

QCString TemplateVariant::m_strVal
private

Definition at line 291 of file template.h.

Type TemplateVariant::m_type
private

Definition at line 290 of file template.h.


The documentation for this class was generated from the following files: