MythTV  master
datacontracthelper.h
Go to the documentation of this file.
1 // Program Name: datacontracthelper.h
3 // Created : Jan. 15, 2010
4 //
5 // Copyright (c) 2010 David Blain <dblain@mythtv.org>
6 //
7 // Licensed under the GPL v2 or later, see COPYING for details
8 //
10 //
11 // * Copy Constructors (needed for Q_PROPERTY) don't do Deep Copies yet.
12 //
13 // * DECLARE_METATYPE not working if this header is not included... need
14 // to find solution since Serializer classes doesn't include this header.
15 //
16 // * Q_CLASSINFO is used to add metadata options to individual properties
17 // the the Qt Metadata system doesn't account for.
18 // It can be used in each data contract class. The format is as follows:
19 //
20 // Q_CLASSINFO( "<PropName>", "<option>=<value>;<option>=<value" );
21 //
22 // Valid options/values are:
23 //
24 // type=<containedType> - used by collections to know what type of
25 // object is stored. (QVariantMap, QVariantList)
26 // name=<name> - used for QVariantMap & QVariantList to hint
27 // to the serializer what name to use for each
28 // object in collection (child element in XML).
29 // transient=true - If present, this property will not be used
30 // when calculating the SHA1 hash used for ETag
31 // http header.
32 //
33 //
34 // * DESIGNABLE in Q_PROPERTY is used to indicate if it should be Serialized
35 // (can specify a propery to support runtime logic)
36 //
37 // * Q_CLASSINFO( "defaultProp", "<propname>" ) is used to indicate the
38 // default property (used for node text in XML)
39 //
41 
42 #ifndef DATACONTRACTHELPER_H_
43 #define DATACONTRACTHELPER_H_
44 
45 #include <QList>
46 #include <QStringList>
47 #include <QVariantMap>
48 
50 //
52 
53 #define PROPERTYIMP( type, name ) \
54  private: type m_##name; \
55  public: \
56  type name() const \
57  { \
58  return m_##name; \
59  } \
60  void set##name(const type val) \
61  { \
62  m_##name = val; \
63  }
64 
66 
67 #define PROPERTYIMP_ENUM( type, name ) \
68  private: type m_##name; \
69  public: \
70  type name() const \
71  { \
72  return m_##name; \
73  } \
74  void set##name(const type val) \
75  { \
76  m_##name = val; \
77  } \
78  void set##name( int val) \
79  { \
80  m_##name = (type)val; \
81  }
82 
84 
85 #define PROPERTYIMP_PTR( type, name ) \
86  private: type* m_##name; \
87  public: \
88  type* name() \
89  { \
90  if (m_##name == nullptr) \
91  m_##name = new type( this );\
92  return m_##name; \
93  }
94 
96 
97 #define PROPERTYIMP_RO_REF( type, name ) \
98  private: type m_##name; \
99  public: \
100  type &name() \
101  { \
102  return m_##name; \
103  }
104 
105 
106 namespace DTC
107 {
108 
110 //
112 
113 inline void DeleteListContents( QVariantList &list )
114 {
115  while( !list.isEmpty() )
116  {
117  QVariant vValue = list.takeFirst();
118 
119  const QObject *pObject = vValue.value< QObject* >();
120 
121  if (pObject != nullptr)
122  delete pObject;
123  }
124 }
125 
127 
128 template< class T >
129 void CopyListContents( QObject *pParent, QVariantList &dst, const QVariantList &src )
130 {
131  for( int nIdx = 0; nIdx < src.size(); nIdx++ )
132  {
133  QVariant vValue = src[ nIdx ];
134 
135  if ( vValue.canConvert< QObject* >())
136  {
137  const QObject *pObject = vValue.value< QObject* >();
138 
139  if (pObject != nullptr)
140  {
141  QObject *pNew = new T( pParent );
142 
143  ((T *)pNew)->Copy( (const T *)pObject );
144 
145  dst.append( QVariant::fromValue<QObject *>( pNew ));
146  }
147  }
148  }
149 }
150 
151 } // namespace DTC
152 
153 #endif
154 
155 
void CopyListContents(QObject *pParent, QVariantList &dst, const QVariantList &src)
void DeleteListContents(QVariantList &list)