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 LICENSE 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 // N.B., DESIGNABLE was removed in v32-Pre to prepare for Qt 6.
38 //
39 //
40 // * Q_CLASSINFO( "defaultProp", "<propname>" ) is used to indicate the
41 // default property (used for node text in XML)
42 //
44 
45 #ifndef DATACONTRACTHELPER_H_
46 #define DATACONTRACTHELPER_H_
47 
48 #include <QList>
49 #include <QStringList>
50 #include <QVariantMap>
51 
53 //
55 
56 #define PROPERTYIMP( type, name ) \
57  private: type m_##name; \
58  public: \
59  type name() const \
60  { \
61  return m_##name; \
62  } \
63  void set##name(const type val) \
64  { \
65  m_##name = val; \
66  }
67 
69 
70 #define PROPERTYIMP_REF( type, name ) \
71  private: type m_##name; \
72  public: \
73  type name() const \
74  { \
75  return m_##name; \
76  } \
77  void set##name(const type& val) \
78  { \
79  m_##name = val; \
80  }
81 
83 
84 #define PROPERTYIMP_ENUM( type, name ) \
85  private: type m_##name; \
86  public: \
87  type name() const \
88  { \
89  return m_##name; \
90  } \
91  void set##name(const type val) \
92  { \
93  m_##name = val; \
94  } \
95  void set##name( int val) \
96  { \
97  m_##name = (type)val; \
98  }
99 
101 
102 #define PROPERTYIMP_PTR( type, name ) \
103  private: type* m_##name; /* NOLINT(bugprone-macro-parentheses) */ \
104  public: \
105  type* name() /* NOLINT(bugprone-macro-parentheses) */ \
106  { \
107  if (m_##name == nullptr) \
108  m_##name = new type( this );\
109  return m_##name; \
110  }
111 
113 
114 #define PROPERTYIMP_RO_REF( type, name ) \
115  private: type m_##name; \
116  public: \
117  type &name() /* NOLINT(bugprone-macro-parentheses) */ \
118  { \
119  return m_##name; \
120  }
121 
122 
123 namespace DTC
124 {
125 
127 //
129 
130 inline void DeleteListContents( QVariantList &list )
131 {
132  while( !list.isEmpty() )
133  {
134  QVariant vValue = list.takeFirst();
135 
136  const QObject *pObject = vValue.value< QObject* >();
137 
138  delete pObject;
139  }
140 }
141 
143 
144 template< class T >
145 void CopyListContents( QObject *pParent, QVariantList &dst, const QVariantList &src )
146 {
147  for (const auto& vValue : std::as_const(src))
148  {
149  if ( vValue.canConvert< QObject* >())
150  {
151  const QObject *pObject = vValue.value< QObject* >();
152 
153  if (pObject != nullptr)
154  {
155  QObject *pNew = new T( pParent );
156 
157  ((T *)pNew)->Copy( (const T *)pObject );
158 
159  dst.append( QVariant::fromValue<QObject *>( pNew ));
160  }
161  }
162  }
163 }
164 
165 } // namespace DTC
166 
167 #endif
168 
169 
DTC::CopyListContents
void CopyListContents(QObject *pParent, QVariantList &dst, const QVariantList &src)
Definition: datacontracthelper.h:145
DTC
Definition: datacontracthelper.h:123
DTC::DeleteListContents
void DeleteListContents(QVariantList &list)
Definition: datacontracthelper.h:130