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 // 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_ENUM( 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  void set##name( int val) \
82  { \
83  m_##name = (type)val; \
84  }
85 
87 
88 #define PROPERTYIMP_PTR( type, name ) \
89  private: type* m_##name; /* NOLINT(bugprone-macro-parentheses) */ \
90  public: \
91  type* name() /* NOLINT(bugprone-macro-parentheses) */ \
92  { \
93  if (m_##name == nullptr) \
94  m_##name = new type( this );\
95  return m_##name; \
96  }
97 
99 
100 #define PROPERTYIMP_RO_REF( type, name ) \
101  private: type m_##name; \
102  public: \
103  type &name() /* NOLINT(bugprone-macro-parentheses) */ \
104  { \
105  return m_##name; \
106  }
107 
108 
109 namespace DTC
110 {
111 
113 //
115 
116 inline void DeleteListContents( QVariantList &list )
117 {
118  while( !list.isEmpty() )
119  {
120  QVariant vValue = list.takeFirst();
121 
122  const QObject *pObject = vValue.value< QObject* >();
123 
124  delete pObject;
125  }
126 }
127 
129 
130 template< class T >
131 void CopyListContents( QObject *pParent, QVariantList &dst, const QVariantList &src )
132 {
133  for (const auto& vValue : qAsConst(src))
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 
DTC::CopyListContents
void CopyListContents(QObject *pParent, QVariantList &dst, const QVariantList &src)
Definition: datacontracthelper.h:131
DTC
Definition: datacontracthelper.h:109
DTC::DeleteListContents
void DeleteListContents(QVariantList &list)
Definition: datacontracthelper.h:116