xnode.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 
15 #include "transaction_impl.h"
16 
17 #include "xnode.h"
18 #include <typeinfo>
19 
20 DECLSPEC_KAME XThreadLocal<std::deque<shared_ptr<XNode> > > XNode::stl_thisCreating;
21 
22 void
23 XNode::Payload::setUIEnabled(bool var) {
24  if(isDisabled()) return;
25  m_flags = (m_flags & ~NODE_UI_ENABLED) | (var ? NODE_UI_ENABLED : 0);
26  tr().mark(onUIFlagsChanged(), &node());
27 }
28 void
29 XNode::Payload::disable() {
30  m_flags = (m_flags & ~(NODE_DISABLED | NODE_UI_ENABLED)) | NODE_DISABLED;
31  tr().mark(onUIFlagsChanged(), &node());
32 }
33 
34 XNode::XNode(const char *name, bool runtime)
35  : Transactional::Node<XNode>(), m_name(name ? name : "") {
36  // temporaly shared_ptr to be able to use shared_from_this() in constructors
37  XNode::stl_thisCreating->push_back(shared_ptr<XNode>(this));
38  assert(shared_from_this());
39 
40  trans( *this).setRuntime(runtime);
41 
42  dbgPrint(QString("xnode %1 is created., addr=0x%2, size=0x%3")
43  .arg(getLabel())
44  .arg((uintptr_t)this, 0, 16)
45  .arg((uintptr_t)sizeof(XNode), 0, 16));
46 }
47 XNode::~XNode() {
48  dbgPrint(QString("xnode %1 is being deleted., addr=0x%2").arg(getLabel()).arg((uintptr_t)this, 0, 16));
49 }
50 XString
51 XNode::getName() const {
52  return m_name;
53 }
54 XString
55 XNode::getTypename() const {
56  XString name = typeid( *this).name();
57  int i = name.find('X');
58  assert(i != std::string::npos);
59  assert(i + 1 < name.length());
60  return name.substr(i + 1);
61 }
62 
63 void
65  trans(*this).disable();
66 }
67 void
69  trans( *this).setUIEnabled(v);
70 }
71 
72 shared_ptr<XNode>
73 XNode::getChild(const XString &var) const {
74  Snapshot shot( *this);
75  shared_ptr<XNode> node;
76  shared_ptr<const NodeList> list(shot.list());
77  if(list) {
78  for(auto it = list->begin(); it != list->end(); it++) {
79  if(dynamic_pointer_cast<XNode>( *it)->getName() == var) {
80  node = dynamic_pointer_cast<XNode>( *it);
81  break;
82  }
83  }
84  }
85  return node;
86 }
87 
88 void
89 XTouchableNode::Payload::touch() {
90  tr().mark(onTouch(), static_cast<XTouchableNode *>(&node()));
91 }
92 
93 template <typename T, int base>
94 void
96 }
97 template <>
98 void
100  bool ok;
101  int var = QString(str).toInt(&ok, 10);
102  if( !ok)
103  throw XKameError(i18n("Ill string conversion to integer."), __FILE__, __LINE__);
104  *this = var;
105 }
106 template <>
107 void
109  bool ok;
110  unsigned int var = QString(str).toUInt(&ok);
111  if( !ok)
112  throw XKameError(i18n("Ill string conversion to unsigned integer."), __FILE__, __LINE__);
113  *this = var;
114 }
115 template <>
116 void
118  bool ok;
119  long var = QString(str).toLong(&ok, 10);
120  if( !ok)
121  throw XKameError(i18n("Ill string conversion to integer."), __FILE__, __LINE__);
122  *this = var;
123 }
124 template <>
125 void
127  bool ok;
128  unsigned long var = QString(str).toULong(&ok);
129  if( !ok)
130  throw XKameError(i18n("Ill string conversion to unsigned integer."), __FILE__, __LINE__);
131  *this = var;
132 }
133 template <>
134 void
136  bool ok;
137  unsigned int var = QString(str).toULong(&ok, 16);
138  if( !ok)
139  throw XKameError(i18n("Ill string conversion to hex."), __FILE__, __LINE__);
140  *this = var;
141 }
142 template <>
143 void
145  bool ok;
146  bool x = QString(str).toInt(&ok);
147  if(ok) {
148  *this = x ? true : false ;
149  return;
150  }
151  if(QString(str).trimmed().toLower() == "true") {
152  *this = true; return;
153  }
154  if(QString(str).trimmed().toLower() == "false") {
155  *this = false; return;
156  }
157  throw XKameError(i18n("Ill string conversion to boolean."), __FILE__, __LINE__);
158 }
159 
160 template <typename T, int base>
161 XString
163  return QString::number(m_var, base);
164 }
165 template <>
166 XString
168  return m_var ? "true" : "false";
169 }
170 
171 template class XIntNodeBase<int, 10>;
172 template class XIntNodeBase<unsigned int, 10>;
173 template class XIntNodeBase<long, 10>;
174 template class XIntNodeBase<unsigned long, 10>;
175 template class XIntNodeBase<unsigned long, 16>;
176 template class XIntNodeBase<bool, 10>;
177 
178 XStringNode::XStringNode(const char *name, bool runtime)
179  : XValueNodeBase(name, runtime) {}
180 
181 XDoubleNode::XDoubleNode(const char *name, bool runtime, const char *format)
182  : XValueNodeBase(name, runtime) {
183  setFormat(format);
184 }
185 
186 XString
188  return formatDouble(
189  static_cast<const XDoubleNode&>(node()).format(), m_var);
190 }
191 void
193  bool ok;
194  double var = QString(str).toDouble(&ok);
195  if( !ok)
196  throw XKameError(i18n("Ill string conversion to double float."), __FILE__, __LINE__);
197  *this = var;
198 }
199 
200 void
201 XDoubleNode::setFormat(const char* format) {
202  XString fmt;
203  if(format)
204  fmt = format;
205  try {
206  formatDoubleValidator(fmt);
207  m_format.reset(new XString(fmt));
208  }
209  catch (XKameError &e) {
210  e.print();
211  }
212 }
213 
214 template DECLSPEC_KAME class Transactional::Node<class XNode>;

Generated for KAME4 by  doxygen 1.8.3