Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTVariable Class Reference

#include <OTVariable.hpp>

Public Types

enum  OTVariable_Type { Var_String, Var_Integer, Var_Bool, Var_Error_Type }
 
enum  OTVariable_Access { Var_Constant, Var_Persistent, Var_Important, Var_Error_Access }
 

Public Member Functions

EXPORT void RegisterForExecution (OTScript &theScript)
 
EXPORT void UnregisterScript ()
 
bool IsDirty () const
 
void SetAsClean ()
 
bool IsConstant () const
 
bool IsPersistent () const
 
bool IsImportant () const
 
void SetBylaw (OTBylaw &theBylaw)
 
bool SetValue (const int32_t &nValue)
 
bool SetValue (bool bValue)
 
bool SetValue (const std::string &str_Value)
 
EXPORT const OTStringGetName () const
 
OTVariable_Type GetType () const
 
OTVariable_Access GetAccess () const
 
bool IsInteger () const
 
bool IsBool () const
 
bool IsString () const
 
int32_t CopyValueInteger () const
 
bool CopyValueBool () const
 
std::string CopyValueString () const
 
int32_t & GetValueInteger ()
 
bool & GetValueBool ()
 
std::string & GetValueString ()
 
bool Compare (OTVariable &rhs)
 
EXPORT OTVariable ()
 
EXPORT OTVariable (std::string str_Name, int32_t nValue, OTVariable_Access theAccess=Var_Persistent)
 
EXPORT OTVariable (std::string str_Name, bool bValue, OTVariable_Access theAccess=Var_Persistent)
 
EXPORT OTVariable (std::string str_Name, std::string str_Value, OTVariable_Access theAccess=Var_Persistent)
 
virtual EXPORT ~OTVariable ()
 
void Serialize (OTString &strAppend, bool bCalculatingID=false) const
 

Detailed Description

Definition at line 145 of file OTVariable.hpp.

Member Enumeration Documentation

Enumerator
Var_Constant 
Var_Persistent 
Var_Important 
Var_Error_Access 

Definition at line 155 of file OTVariable.hpp.

155  {
156  Var_Constant, // Constant -- you cannot change this value.
157  Var_Persistent, // Persistent -- changing value doesn't require notice
158  // to parties.
159  Var_Important, // Important -- changing value requires notice to
160  // parties.
161  Var_Error_Access // should never happen.
162  };
Enumerator
Var_String 
Var_Integer 
Var_Bool 
Var_Error_Type 

Definition at line 148 of file OTVariable.hpp.

148  {
149  Var_String, // std::string
150  Var_Integer, // Integer. (For int64_t int32_t: use strings.)
151  Var_Bool, // Boolean. (True / False)
152  Var_Error_Type // should never happen.
153  };

Constructor & Destructor Documentation

opentxs::OTVariable::OTVariable ( )

Definition at line 234 of file OTVariable.cpp.

235  : m_nValue(0)
236  , m_bValue(false)
237  , m_nValueBackup(0)
238  , m_bValueBackup(false)
239  , m_pBylaw(nullptr)
241  , m_Access(Var_Error_Access)
242  , m_pScript(nullptr)
243 {
244 }
opentxs::OTVariable::OTVariable ( std::string  str_Name,
int32_t  nValue,
OTVariable_Access  theAccess = Var_Persistent 
)

Definition at line 266 of file OTVariable.cpp.

268  : m_strName(str_Name.c_str())
269  , m_nValue(nValue)
270  , m_bValue(false)
271  , m_nValueBackup(nValue)
272  , m_bValueBackup(false)
273  , m_pBylaw(nullptr)
274  , m_Type(OTVariable::Var_Integer)
275  , m_Access(theAccess)
276  , m_pScript(nullptr)
277 {
278 }
opentxs::OTVariable::OTVariable ( std::string  str_Name,
bool  bValue,
OTVariable_Access  theAccess = Var_Persistent 
)

Definition at line 281 of file OTVariable.cpp.

283  : m_strName(str_Name.c_str())
284  , m_nValue(0)
285  , m_bValue(bValue)
286  , m_nValueBackup(0)
287  , m_bValueBackup(bValue)
288  , m_pBylaw(nullptr)
289  , m_Type(OTVariable::Var_Bool)
290  , m_Access(theAccess)
291  , m_pScript(nullptr)
292 {
293 }
opentxs::OTVariable::OTVariable ( std::string  str_Name,
std::string  str_Value,
OTVariable_Access  theAccess = Var_Persistent 
)

Definition at line 247 of file OTVariable.cpp.

249  : m_strName(str_Name.c_str())
250  , m_str_Value(str_Value)
251  , m_nValue(0)
252  , m_bValue(false)
253  , m_str_ValueBackup(str_Value)
254  , m_nValueBackup(0)
255  , m_bValueBackup(false)
256  , m_pBylaw(nullptr)
257  , m_Type(OTVariable::Var_String)
258  , m_Access(theAccess)
259  , m_pScript(nullptr)
260 {
261  if (m_str_Value.empty()) m_str_Value = "";
262  if (m_str_ValueBackup.empty()) m_str_ValueBackup = "";
263 }
opentxs::OTVariable::~OTVariable ( )
virtual

Definition at line 295 of file OTVariable.cpp.

296 {
297  if (nullptr != m_pScript) {
298  m_pScript->RemoveVariable(*this);
299  }
300 
301  m_pScript =
302  nullptr; // I wasn't the owner, it was a pointer for convenience only.
303  m_pBylaw =
304  nullptr; // I wasn't the owner, it was a pointer for convenience only.
305 }
EXPORT void RemoveVariable(OTVariable &theVar)
Definition: OTScript.cpp:405

Member Function Documentation

bool opentxs::OTVariable::Compare ( OTVariable rhs)

Definition at line 438 of file OTVariable.cpp.

439 {
440  if (!(GetName().Compare(rhs.GetName()))) {
441  otOut << "OTVariable::Compare: Names don't match: " << GetName()
442  << " / " << rhs.GetName() << " \n";
443  return false;
444  }
445  if (!(GetType() == rhs.GetType())) {
446  otOut << "OTVariable::Compare: Type doesn't match: " << GetName()
447  << " \n";
448  return false;
449  }
450  if (!(GetAccess() == rhs.GetAccess())) {
451  otOut << "OTVariable::Compare: Access types don't match: " << GetName()
452  << " \n";
453  return false;
454  }
455 
456  bool bMatch = false;
457 
458  switch (GetType()) {
460  bMatch = (GetValueInteger() == rhs.GetValueInteger());
461  break;
463  bMatch = (GetValueBool() == rhs.GetValueBool());
464  break;
466  bMatch = (GetValueString().compare(rhs.GetValueString()) == 0);
467  break;
468  default:
469  otErr << "OTVariable::Compare: Unknown type in variable " << m_strName
470  << ".\n";
471  break;
472  }
473 
474  return bMatch;
475 }
OTVariable_Type GetType() const
Definition: OTVariable.hpp:224
OTLOG_IMPORT OTLogStream otOut
EXPORT const OTString & GetName() const
Definition: OTVariable.hpp:220
OTVariable_Access GetAccess() const
Definition: OTVariable.hpp:228
OTLOG_IMPORT OTLogStream otErr
std::string & GetValueString()
Definition: OTVariable.hpp:267
int32_t & GetValueInteger()
Definition: OTVariable.hpp:259
bool Compare(OTVariable &rhs)
Definition: OTVariable.cpp:438
bool opentxs::OTVariable::CopyValueBool ( ) const
inline

Definition at line 250 of file OTVariable.hpp.

251  {
252  return m_bValue;
253  }
int32_t opentxs::OTVariable::CopyValueInteger ( ) const
inline

Definition at line 246 of file OTVariable.hpp.

247  {
248  return m_nValue;
249  }
std::string opentxs::OTVariable::CopyValueString ( ) const
inline

Definition at line 254 of file OTVariable.hpp.

255  {
256  return m_str_Value;
257  }
OTVariable_Access opentxs::OTVariable::GetAccess ( ) const
inline

Definition at line 228 of file OTVariable.hpp.

229  {
230  return m_Access;
231  }
EXPORT const OTString& opentxs::OTVariable::GetName ( ) const
inline

Definition at line 220 of file OTVariable.hpp.

221  {
222  return m_strName;
223  } // variable's name as used in a script.
OTVariable_Type opentxs::OTVariable::GetType ( ) const
inline

Definition at line 224 of file OTVariable.hpp.

225  {
226  return m_Type;
227  }
bool& opentxs::OTVariable::GetValueBool ( )
inline

Definition at line 263 of file OTVariable.hpp.

264  {
265  return m_bValue;
266  }
int32_t& opentxs::OTVariable::GetValueInteger ( )
inline

Definition at line 259 of file OTVariable.hpp.

260  {
261  return m_nValue;
262  }
std::string& opentxs::OTVariable::GetValueString ( )
inline

Definition at line 267 of file OTVariable.hpp.

268  {
269  return m_str_Value;
270  }
bool opentxs::OTVariable::IsBool ( ) const
inline

Definition at line 237 of file OTVariable.hpp.

238  {
239  return (Var_Bool == m_Type);
240  }
bool opentxs::OTVariable::IsConstant ( ) const
inline

Definition at line 200 of file OTVariable.hpp.

201  {
202  return (Var_Constant == m_Access);
203  }
bool opentxs::OTVariable::IsDirty ( ) const

Definition at line 353 of file OTVariable.cpp.

354 {
355  bool bReturnVal = false;
356 
357  switch (m_Type) {
359  if (0 != m_str_Value.compare(m_str_ValueBackup)) // If they do NOT
360  // match, then it's
361  // dirty.
362  bReturnVal = true;
363  break;
365  if (m_nValue !=
366  m_nValueBackup) // If they do NOT match, then it's dirty.
367  bReturnVal = true;
368  break;
370  if (m_bValue !=
371  m_bValueBackup) // If they do NOT match, then it's dirty.
372  bReturnVal = true;
373  break;
374  default:
375  otErr << "OTVariable::IsDirty: Error: unknown type for variable: "
376  << m_strName << "\n";
377  break;
378  }
379 
380  return bReturnVal;
381 }
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTVariable::IsImportant ( ) const
inline

Definition at line 208 of file OTVariable.hpp.

209  {
210  return (Var_Important == m_Access);
211  }
bool opentxs::OTVariable::IsInteger ( ) const
inline

Definition at line 233 of file OTVariable.hpp.

234  {
235  return (Var_Integer == m_Type);
236  }
bool opentxs::OTVariable::IsPersistent ( ) const
inline

Definition at line 204 of file OTVariable.hpp.

205  {
206  return ((Var_Persistent == m_Access) || (Var_Important == m_Access));
207  } // important vars are persistent, too.
bool opentxs::OTVariable::IsString ( ) const
inline

Definition at line 241 of file OTVariable.hpp.

242  {
243  return (Var_String == m_Type);
244  }
void opentxs::OTVariable::RegisterForExecution ( OTScript theScript)

Definition at line 424 of file OTVariable.cpp.

425 {
426  SetAsClean(); // so we can check for dirtiness after execution.
427 
428  const std::string str_var_name = m_strName.Get();
429 
430  theScript.AddVariable(str_var_name, *this);
431 
432  m_pScript = &theScript; // So later, if the variable destructs, and
433  // this pointer is set, the variable can
434  // remove itself from the script.
435 }
EXPORT const char * Get() const
Definition: OTString.cpp:1045
void opentxs::OTVariable::Serialize ( OTString strAppend,
bool  bCalculatingID = false 
) const

Definition at line 144 of file OTVariable.cpp.

145 {
146 
147  std::string str_access("");
148 
149  switch (m_Access) {
150  case OTVariable::Var_Constant: // This cannot be changed from inside the
151  // script.
152  str_access = "constant";
153  break;
154  case OTVariable::Var_Persistent: // This can be changed without notifying
155  // the parties.
156  str_access = "persistent";
157  break;
158  case OTVariable::Var_Important: // This cannot be changed without notifying
159  // the parties.
160  str_access = "important";
161  break;
162  default:
163  otErr << "OTVariable::Serialize: ERROR: Bad variable type.\n";
164  break;
165  }
166 
167  std::string str_type;
168 
169  switch (m_Type) {
170  case OTVariable::Var_String: {
171  str_type = "string";
172 
173  if ((false == bCalculatingID) && // we don't serialize the variable's
174  // value when calculating the
175  (m_str_Value.size() > 0)) // owner OTScriptable's ID, since the
176  // value can change over time.
177  {
178  OTString strVal(m_str_Value.c_str());
179  OTASCIIArmor ascVal(strVal);
180  strAppend.Concatenate("<variable\n name=\"%s\"\n"
181  " value=\"%s\"\n"
182  " type=\"%s\"\n"
183  " access=\"%s\" >\n%s</variable>\n\n",
184  m_strName.Get(), "exists", str_type.c_str(),
185  str_access.c_str(), ascVal.Get());
186  }
187  else {
188  strAppend.Concatenate("<variable\n name=\"%s\"\n"
189  " value=\"%s\"\n"
190  " type=\"%s\"\n"
191  " access=\"%s\" />\n\n",
192  m_strName.Get(), "none", // value
193  str_type.c_str(), str_access.c_str());
194  }
195  } break;
197  str_type = "integer";
198  strAppend.Concatenate(
199  "<variable\n name=\"%s\"\n"
200  " value=\"%d\"\n"
201  " type=\"%s\"\n"
202  " access=\"%s\" />\n\n",
203  m_strName.Get(),
204  bCalculatingID ? 0 : m_nValue, // we don't serialize the variable's
205  // value when calculating the smart
206  // contract's ID.
207  str_type.c_str(), str_access.c_str());
208  break;
210  str_type = "bool";
211  strAppend.Concatenate(
212  "<variable\n name=\"%s\"\n"
213  " value=\"%s\"\n"
214  " type=\"%s\"\n"
215  " access=\"%s\" />\n\n",
216  m_strName.Get(),
217  bCalculatingID
218  ? "false"
219  : (m_bValue ? "true" : "false"), // we don't serialize the
220  // variable's value when
221  // calculating the smart
222  // contract's ID.
223  str_type.c_str(),
224  str_access.c_str());
225  break;
226  default:
227  otErr
228  << "OTVariable::Serialize: Error, Wrong Type -- not serializing.\n";
229  break;
230  }
231 }
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
void opentxs::OTVariable::SetAsClean ( )

Definition at line 385 of file OTVariable.cpp.

386 {
387  switch (m_Type) {
389  m_str_ValueBackup = m_str_Value; // Save a copy of the current value, so
390  // we can check later and see if
391  // they're different.
392  break;
394  m_nValueBackup = m_nValue; // Save a copy of the current value, so we
395  // can check later and see if they're
396  // different.
397  break;
399  m_bValueBackup = m_bValue; // Save a copy of the current value, so we
400  // can check later and see if they're
401  // different.
402  break;
403  default:
404  otErr << "OTVariable::SetAsClean: Error: unknown type for variable: "
405  << m_strName << "\n";
406  m_str_ValueBackup = m_str_Value;
407  m_nValueBackup = m_nValue;
408  m_bValueBackup = m_bValue;
409  break;
410  }
411 }
OTLOG_IMPORT OTLogStream otErr
void opentxs::OTVariable::SetBylaw ( OTBylaw theBylaw)
inline

Definition at line 212 of file OTVariable.hpp.

213  {
214  m_pBylaw = &theBylaw;
215  }
bool opentxs::OTVariable::SetValue ( const int32_t &  nValue)

Definition at line 307 of file OTVariable.cpp.

308 {
309  if (!IsInteger()) {
310  otErr << "OTVariable::SetValue(int64_t): Error: This variable ("
311  << m_strName << ") is not an integer.\n";
312  return false;
313  }
314 
315  m_nValue = m_nValueBackup = nValue;
316 
317  return true;
318 }
bool IsInteger() const
Definition: OTVariable.hpp:233
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTVariable::SetValue ( bool  bValue)

Definition at line 320 of file OTVariable.cpp.

321 {
322  if (!IsBool()) {
323  otErr << "OTVariable::SetValue(bool): Error: This variable ("
324  << m_strName << ") is not a bool.\n";
325  return false;
326  }
327 
328  m_bValue = m_bValueBackup = bValue;
329 
330  return true;
331 }
bool IsBool() const
Definition: OTVariable.hpp:237
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTVariable::SetValue ( const std::string &  str_Value)

Definition at line 333 of file OTVariable.cpp.

334 {
335  if (!IsString()) {
336  otErr << "OTVariable::SetValue(std::string): Error: This variable ("
337  << m_strName << ") is not a string.\n";
338  return false;
339  }
340 
341  m_str_Value = m_str_ValueBackup = str_Value;
342 
343  if (m_str_Value.empty()) m_str_Value = "";
344  if (m_str_ValueBackup.empty()) m_str_ValueBackup = "";
345 
346  return true;
347 }
bool IsString() const
Definition: OTVariable.hpp:241
OTLOG_IMPORT OTLogStream otErr
void opentxs::OTVariable::UnregisterScript ( )

Definition at line 416 of file OTVariable.cpp.

417 {
418  m_pScript = nullptr;
419 }

The documentation for this class was generated from the following files: