QXRD  0.11.16
qcepobject.cpp
Go to the documentation of this file.
1 #include "qcepobject.h"
2 #include "qcepproperty.h"
3 #include <QMetaProperty>
4 #include <stdio.h>
5 #include <QAtomicInt>
6 #include <QSet>
7 #include <QThread>
8 
9 static QAtomicInt s_ObjectAllocateCount(0);
10 static QAtomicInt s_ObjectDeleteCount(0);
11 
12 #ifndef QT_NO_DEBUG
13 static QSet<QcepObject*> s_Allocated;
14 #endif
15 
17  QObject(NULL),
18  m_Parent(parent),
19  m_ObjectNamer(this, name)/*,
20  m_Name(QcepSettingsSaverWPtr(), this, "name", name, "Object Name")*/
21 {
22  s_ObjectAllocateCount.fetchAndAddOrdered(1);
23 
24 #ifndef QT_NO_DEBUG
25  s_Allocated.insert(this);
26 #endif
27 }
28 
30 {
31 //#ifndef QT_NO_DEBUG
32 // QThread *currTh = QThread::currentThread();
33 // QThread *objTh = thread();
34 
35 // if (objTh && currTh != objTh) {
36 // printf("Deleting object from different thread %s (%s, %s)\n",
37 // qPrintable(objectName()),
38 // qPrintable(currTh ? currTh->objectName() : "null"),
39 // qPrintable(objTh ? objTh->objectName() : "null"));
40 // }
41 //#endif
42 
43  s_ObjectDeleteCount.fetchAndAddOrdered(1);
44 
45 #ifndef QT_NO_DEBUG
46  s_Allocated.remove(this);
47 #endif
48 }
49 
51 {
52  return s_ObjectAllocateCount.load();
53 }
54 
56 {
57  return s_ObjectDeleteCount.load();
58 }
59 
60 #ifndef QT_NO_DEBUG
62 {
63  return s_Allocated;
64 }
65 #endif
66 
68 {
69  setObjectName(name);
70 }
71 
72 QString QcepObject::get_Name() const
73 {
74  return objectName();
75 }
76 
77 void QcepObject::printLine(QString line)
78 {
79  if (m_Parent) {
80  m_Parent->printLine(line);
81  }
82 }
83 
84 void QcepObject::printMessage(QString msg, QDateTime dt) const
85 {
86  if (m_Parent) {
87  m_Parent->printMessage(msg, dt);
88  } else {
89  printf("MESSAGE: %s %s\n",
90  qPrintable(dt.toString("hh:mm:ss")), qPrintable(msg));
91  }
92 }
93 
94 void QcepObject::criticalMessage(QString msg, QDateTime dt) const
95 {
96  if (m_Parent) {
97  m_Parent->criticalMessage(msg, dt);
98  } else {
99  printf("MESSAGE: %s %s\n",
100  qPrintable(dt.toString("hh:mm:ss")), qPrintable(msg));
101  }
102 }
103 
104 void QcepObject::statusMessage(QString msg, QDateTime dt) const
105 {
106  if (m_Parent) {
107  m_Parent->statusMessage(msg, dt);
108  } else {
109  printf("MESSAGE: %s %s\n",
110  qPrintable(dt.toString("hh:mm:ss")), qPrintable(msg));
111  }
112 }
113 
114 void QcepObject::writeSettings(QSettings *set, QString section)
115 {
116  QcepProperty::writeSettings(this, set, section);
117 }
118 
119 void QcepObject::readSettings(QSettings *set, QString section)
120 {
121  QcepProperty::readSettings(this, set, section);
122 }
123 
124 QString QcepObject::addSlashes(QString str)
125 {
126  QString newStr;
127 
128  for(int i=0;i<str.length();i++) {
129  if(str[i] == '\0') {
130  newStr.append('\\');
131  newStr.append('0');
132  } else if(str[i] == '\'') {
133  newStr.append('\\');
134  newStr.append('\'');
135  } else if(str[i] == '\"') {
136  newStr.append('\\');
137  newStr.append('\"');
138  } else if(str[i] == '\\') {
139  newStr.append('\\');
140  newStr.append('\\');
141  } else {
142  newStr.append(str[i]);
143  }
144  }
145 
146  return newStr;
147 }
148 
150 {
151  if (v.type() == QMetaType::QString) {
152  return "\"" + addSlashes(v.toString()) + "\"";
153  } else {
154  return v.toString();
155  }
156 }
157 
159 {
160  QString res = "";
161  const QMetaObject* metaObject = this->metaObject();
162 
163  for (int i=1; i < metaObject->propertyCount(); i++) {
164  QMetaProperty prop = metaObject->property(i);
165 
166  if (prop.isStored()) {
167  res += tr("%1.%2 = %3;\n").arg(this->get_Name())
168  .arg(prop.name())
169  .arg(scriptValueLiteral(this->property(prop.name())));
170  }
171  }
172 
173  return res;
174 }
static QAtomicInt s_ObjectDeleteCount(0)
void set_Name(QString name)
Definition: qcepobject.cpp:67
virtual ~QcepObject()
Definition: qcepobject.cpp:29
static void readSettings(QObject *object, QSettings *settings, QString section)
virtual void readSettings(QSettings *set, QString section)
Definition: qcepobject.cpp:119
QcepObject * m_Parent
Definition: qcepobject.h:45
QcepObjectNamer m_ObjectNamer
Definition: qcepobject.h:46
static QAtomicInt s_ObjectAllocateCount(0)
virtual QString settingsScript()
Definition: qcepobject.cpp:158
QString get_Name() const
Definition: qcepobject.cpp:72
static QSet< QcepObject * > s_Allocated
Definition: qcepobject.cpp:13
static int deletedObjects()
Definition: qcepobject.cpp:55
static void writeSettings(QObject *object, QSettings *settings, QString section)
QString scriptValueLiteral(QVariant v)
Definition: qcepobject.cpp:149
virtual void criticalMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
Definition: qcepobject.cpp:94
QcepObject(QString name, QcepObject *parent)
Definition: qcepobject.cpp:16
#define str(s)
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime()) const
Definition: qcepobject.cpp:84
virtual void writeSettings(QSettings *set, QString section)
Definition: qcepobject.cpp:114
static QSet< QcepObject * > allocatedObjectsSet()
Definition: qcepobject.cpp:61
QString name
Definition: qcepobject.h:49
virtual void statusMessage(QString msg, QDateTime ts=QDateTime::currentDateTime()) const
Definition: qcepobject.cpp:104
static int allocatedObjects()
Definition: qcepobject.cpp:50
virtual void printLine(QString line)
Definition: qcepobject.cpp:77
static QString addSlashes(QString str)
Definition: qcepobject.cpp:124