TTMath  0.9.4
 C++ bignum library
Data Structures | Public Types | Public Member Functions | Static Public Member Functions
ttmath::Objects Class Reference

#include <ttmathobjects.h>

Data Structures

struct  Item
 

Public Types

typedef std::map< std::string, ItemTable
 
typedef Table::iterator Iterator
 
typedef Table::const_iterator CIterator
 

Public Member Functions

bool IsDefined (const std::string &name)
 
bool IsDefined (const std::wstring &name)
 
ErrorCode Add (const std::string &name, const std::string &value, int param=0)
 
ErrorCode Add (const std::wstring &name, const std::wstring &value, int param=0)
 
bool Empty () const
 
void Clear ()
 
CIterator Begin () const
 
CIterator End () const
 
ErrorCode EditValue (const std::string &name, const std::string &value, int param=0)
 
ErrorCode EditValue (const std::wstring &name, const std::wstring &value, int param=0)
 
ErrorCode EditName (const std::string &old_name, const std::string &new_name)
 
ErrorCode EditName (const std::wstring &old_name, const std::wstring &new_name)
 
ErrorCode Delete (const std::string &name)
 
ErrorCode Delete (const std::wstring &name)
 
ErrorCode GetValue (const std::string &name, std::string &value) const
 
ErrorCode GetValue (const std::wstring &name, std::wstring &value)
 
ErrorCode GetValue (const std::string &name, const char **value) const
 
ErrorCode GetValue (const std::wstring &name, const char **value)
 
ErrorCode GetValueAndParam (const std::string &name, std::string &value, int *param) const
 
ErrorCode GetValueAndParam (const std::wstring &name, std::wstring &value, int *param)
 
ErrorCode GetValueAndParam (const std::string &name, const char **value, int *param) const
 
ErrorCode GetValueAndParam (const std::wstring &name, const char **value, int *param)
 
Table * GetTable ()
 

Static Public Member Functions

static bool CorrectCharacter (int c, bool can_be_digit)
 
template<class string_type >
static bool IsNameCorrect (const string_type &name)
 

Detailed Description

objects of this class are used with the mathematical parser they hold variables or functions defined by a user

each object has its own table in which we're keeping variables or functions

Definition at line 65 of file ttmathobjects.h.

Member Function Documentation

§ Add() [1/2]

ErrorCode ttmath::Objects::Add ( const std::string &  name,
const std::string &  value,
int  param = 0 
)

this method adds one object (variable of function) into the table

Definition at line 175 of file ttmathobjects.h.

References IsNameCorrect().

Referenced by Add(), and EditName().

176  {
177  if( !IsNameCorrect(name) )
178  return err_incorrect_name;
179 
180  Iterator i = table.find(name);
181 
182  if( i != table.end() )
183  // we have this object in our table
184  return err_object_exists;
185 
186  table.insert( std::make_pair(name, Item(value, param)) );
187 
188  return err_ok;
189  }
static bool IsNameCorrect(const string_type &name)

§ Add() [2/2]

ErrorCode ttmath::Objects::Add ( const std::wstring &  name,
const std::wstring &  value,
int  param = 0 
)

this method adds one object (variable of function) into the table

Definition at line 197 of file ttmathobjects.h.

References Add(), ttmath::Misc::AssignString(), and IsNameCorrect().

198  {
199  // we should check whether the name (in wide characters) are correct
200  // before calling AssignString() function
201  if( !IsNameCorrect(name) )
202  return err_incorrect_name;
203 
204  Misc::AssignString(str_tmp1, name);
205  Misc::AssignString(str_tmp2, value);
206 
207  return Add(str_tmp1, str_tmp2, param);
208  }
ErrorCode Add(const std::string &name, const std::string &value, int param=0)
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ Begin()

CIterator ttmath::Objects::Begin ( ) const

this method returns 'const_iterator' on the first item on the table

Definition at line 234 of file ttmathobjects.h.

235  {
236  return table.begin();
237  }

§ Clear()

void ttmath::Objects::Clear ( )

this method clears the table

Definition at line 225 of file ttmathobjects.h.

226  {
227  return table.clear();
228  }

§ CorrectCharacter()

static bool ttmath::Objects::CorrectCharacter ( int  c,
bool  can_be_digit 
)
static

this method returns true if a character 'c' is a character which can be in a name

if 'can_be_digit' is true that means when the 'c' is a digit this method returns true otherwise it returns false

Definition at line 102 of file ttmathobjects.h.

Referenced by IsNameCorrect().

103  {
104  if( (c>='a' && c<='z') || (c>='A' && c<='Z') )
105  return true;
106 
107  if( can_be_digit && ((c>='0' && c<='9') || c=='_') )
108  return true;
109 
110  return false;
111  }

§ Delete() [1/2]

ErrorCode ttmath::Objects::Delete ( const std::string &  name)

this method deletes an object

Definition at line 349 of file ttmathobjects.h.

References IsNameCorrect().

Referenced by Delete().

350  {
351  if( !IsNameCorrect(name) )
352  return err_incorrect_name;
353 
354  Iterator i = table.find(name);
355 
356  if( i == table.end() )
357  return err_unknown_object;
358 
359  table.erase( i );
360 
361  return err_ok;
362  }
static bool IsNameCorrect(const string_type &name)

§ Delete() [2/2]

ErrorCode ttmath::Objects::Delete ( const std::wstring &  name)

this method deletes an object

Definition at line 371 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), Delete(), and IsNameCorrect().

372  {
373  // we should check whether the name (in wide characters) are correct
374  // before calling AssignString() function
375  if( !IsNameCorrect(name) )
376  return err_incorrect_name;
377 
378  Misc::AssignString(str_tmp1, name);
379 
380  return Delete(str_tmp1);
381  }
ErrorCode Delete(const std::string &name)
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ EditName() [1/2]

ErrorCode ttmath::Objects::EditName ( const std::string &  old_name,
const std::string &  new_name 
)

this method changes the name of a specific object

Definition at line 295 of file ttmathobjects.h.

References Add(), and IsNameCorrect().

Referenced by EditName().

296  {
297  if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
298  return err_incorrect_name;
299 
300  Iterator old_i = table.find(old_name);
301  if( old_i == table.end() )
302  return err_unknown_object;
303 
304  if( old_name == new_name )
305  // the new name is the same as the old one
306  // we treat it as a normal situation
307  return err_ok;
308 
309  ErrorCode err = Add(new_name, old_i->second.value, old_i->second.param);
310 
311  if( err == err_ok )
312  {
313  old_i = table.find(old_name);
314  TTMATH_ASSERT( old_i != table.end() )
315 
316  table.erase(old_i);
317  }
318 
319  return err;
320  }
ErrorCode Add(const std::string &name, const std::string &value, int param=0)
static bool IsNameCorrect(const string_type &name)

§ EditName() [2/2]

ErrorCode ttmath::Objects::EditName ( const std::wstring &  old_name,
const std::wstring &  new_name 
)

this method changes the name of a specific object

Definition at line 330 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), EditName(), and IsNameCorrect().

331  {
332  // we should check whether the name (in wide characters) are correct
333  // before calling AssignString() function
334  if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
335  return err_incorrect_name;
336 
337  Misc::AssignString(str_tmp1, old_name);
338  Misc::AssignString(str_tmp2, new_name);
339 
340  return EditName(str_tmp1, str_tmp2);
341  }
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)
ErrorCode EditName(const std::string &old_name, const std::string &new_name)

§ EditValue() [1/2]

ErrorCode ttmath::Objects::EditValue ( const std::string &  name,
const std::string &  value,
int  param = 0 
)

this method changes the value and the number of parameters for a specific object

Definition at line 253 of file ttmathobjects.h.

References IsNameCorrect().

Referenced by EditValue().

254  {
255  if( !IsNameCorrect(name) )
256  return err_incorrect_name;
257 
258  Iterator i = table.find(name);
259 
260  if( i == table.end() )
261  return err_unknown_object;
262 
263  i->second.value = value;
264  i->second.param = param;
265 
266  return err_ok;
267  }
static bool IsNameCorrect(const string_type &name)

§ EditValue() [2/2]

ErrorCode ttmath::Objects::EditValue ( const std::wstring &  name,
const std::wstring &  value,
int  param = 0 
)

this method changes the value and the number of parameters for a specific object

Definition at line 276 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), EditValue(), and IsNameCorrect().

277  {
278  // we should check whether the name (in wide characters) are correct
279  // before calling AssignString() function
280  if( !IsNameCorrect(name) )
281  return err_incorrect_name;
282 
283  Misc::AssignString(str_tmp1, name);
284  Misc::AssignString(str_tmp2, value);
285 
286  return EditValue(str_tmp1, str_tmp2, param);
287  }
ErrorCode EditValue(const std::string &name, const std::string &value, int param=0)
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ Empty()

bool ttmath::Objects::Empty ( ) const

this method returns 'true' if the table is empty

Definition at line 216 of file ttmathobjects.h.

217  {
218  return table.empty();
219  }

§ End()

CIterator ttmath::Objects::End ( ) const

this method returns 'const_iterator' pointing at the space after last item (returns table.end())

Definition at line 244 of file ttmathobjects.h.

245  {
246  return table.end();
247  }

§ GetTable()

Table* ttmath::Objects::GetTable ( )

this method returns a pointer into the table

Definition at line 576 of file ttmathobjects.h.

577  {
578  return &table;
579  }

§ GetValue() [1/4]

ErrorCode ttmath::Objects::GetValue ( const std::string &  name,
std::string &  value 
) const

this method gets the value of a specific object

Definition at line 389 of file ttmathobjects.h.

References IsNameCorrect().

Referenced by GetValue(), and ttmath::Parser< ValueType >::GetValueOfUserDefinedVariable().

390  {
391  if( !IsNameCorrect(name) )
392  return err_incorrect_name;
393 
394  CIterator i = table.find(name);
395 
396  if( i == table.end() )
397  {
398  value.clear();
399  return err_unknown_object;
400  }
401 
402  value = i->second.value;
403 
404  return err_ok;
405  }
static bool IsNameCorrect(const string_type &name)

§ GetValue() [2/4]

ErrorCode ttmath::Objects::GetValue ( const std::wstring &  name,
std::wstring &  value 
)

this method gets the value of a specific object

Definition at line 413 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), GetValue(), and IsNameCorrect().

414  {
415  // we should check whether the name (in wide characters) are correct
416  // before calling AssignString() function
417  if( !IsNameCorrect(name) )
418  return err_incorrect_name;
419 
420  Misc::AssignString(str_tmp1, name);
421  ErrorCode err = GetValue(str_tmp1, str_tmp2);
422  Misc::AssignString(value, str_tmp2);
423 
424  return err;
425  }
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)
ErrorCode GetValue(const std::string &name, std::string &value) const

§ GetValue() [3/4]

ErrorCode ttmath::Objects::GetValue ( const std::string &  name,
const char **  value 
) const

this method gets the value of a specific object (this version is used for not copying the whole string)

Definition at line 434 of file ttmathobjects.h.

References IsNameCorrect().

435  {
436  if( !IsNameCorrect(name) )
437  return err_incorrect_name;
438 
439  CIterator i = table.find(name);
440 
441  if( i == table.end() )
442  {
443  *value = 0;
444  return err_unknown_object;
445  }
446 
447  *value = i->second.value.c_str();
448 
449  return err_ok;
450  }
static bool IsNameCorrect(const string_type &name)

§ GetValue() [4/4]

ErrorCode ttmath::Objects::GetValue ( const std::wstring &  name,
const char **  value 
)

this method gets the value of a specific object (this version is used for not copying the whole string)

Definition at line 459 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), GetValue(), and IsNameCorrect().

460  {
461  // we should check whether the name (in wide characters) are correct
462  // before calling AssignString() function
463  if( !IsNameCorrect(name) )
464  return err_incorrect_name;
465 
466  Misc::AssignString(str_tmp1, name);
467 
468  return GetValue(str_tmp1, value);
469  }
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)
ErrorCode GetValue(const std::string &name, std::string &value) const

§ GetValueAndParam() [1/4]

ErrorCode ttmath::Objects::GetValueAndParam ( const std::string &  name,
std::string &  value,
int *  param 
) const

this method gets the value and the number of parameters of a specific object

Definition at line 478 of file ttmathobjects.h.

References IsNameCorrect().

Referenced by GetValueAndParam(), and ttmath::Parser< ValueType >::GetValueOfVariable().

479  {
480  if( !IsNameCorrect(name) )
481  return err_incorrect_name;
482 
483  CIterator i = table.find(name);
484 
485  if( i == table.end() )
486  {
487  value.clear();
488  *param = 0;
489  return err_unknown_object;
490  }
491 
492  value = i->second.value;
493  *param = i->second.param;
494 
495  return err_ok;
496  }
static bool IsNameCorrect(const string_type &name)

§ GetValueAndParam() [2/4]

ErrorCode ttmath::Objects::GetValueAndParam ( const std::wstring &  name,
std::wstring &  value,
int *  param 
)

this method gets the value and the number of parameters of a specific object

Definition at line 505 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), GetValueAndParam(), and IsNameCorrect().

506  {
507  // we should check whether the name (in wide characters) are correct
508  // before calling AssignString() function
509  if( !IsNameCorrect(name) )
510  return err_incorrect_name;
511 
512  Misc::AssignString(str_tmp1, name);
513  ErrorCode err = GetValueAndParam(str_tmp1, str_tmp2, param);
514  Misc::AssignString(value, str_tmp2);
515 
516  return err;
517  }
ErrorCode GetValueAndParam(const std::string &name, std::string &value, int *param) const
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ GetValueAndParam() [3/4]

ErrorCode ttmath::Objects::GetValueAndParam ( const std::string &  name,
const char **  value,
int *  param 
) const

this method sets the value and the number of parameters of a specific object (this version is used for not copying the whole string)

Definition at line 527 of file ttmathobjects.h.

References IsNameCorrect().

528  {
529  if( !IsNameCorrect(name) )
530  return err_incorrect_name;
531 
532  CIterator i = table.find(name);
533 
534  if( i == table.end() )
535  {
536  *value = 0;
537  *param = 0;
538  return err_unknown_object;
539  }
540 
541  *value = i->second.value.c_str();
542  *param = i->second.param;
543 
544  return err_ok;
545  }
static bool IsNameCorrect(const string_type &name)

§ GetValueAndParam() [4/4]

ErrorCode ttmath::Objects::GetValueAndParam ( const std::wstring &  name,
const char **  value,
int *  param 
)

this method sets the value and the number of parameters of a specific object (this version is used for not copying the whole string but in fact we make one copying during AssignString())

Definition at line 557 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), GetValueAndParam(), and IsNameCorrect().

558  {
559  // we should check whether the name (in wide characters) are correct
560  // before calling AssignString() function
561  if( !IsNameCorrect(name) )
562  return err_incorrect_name;
563 
564  Misc::AssignString(str_tmp1, name);
565 
566  return GetValueAndParam(str_tmp1, value, param);
567  }
ErrorCode GetValueAndParam(const std::string &name, std::string &value, int *param) const
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ IsDefined() [1/2]

bool ttmath::Objects::IsDefined ( const std::string &  name)

this method returns true if such an object is defined (name exists)

Definition at line 139 of file ttmathobjects.h.

Referenced by IsDefined().

140  {
141  Iterator i = table.find(name);
142 
143  if( i != table.end() )
144  // we have this object in our table
145  return true;
146 
147  return false;
148  }

§ IsDefined() [2/2]

bool ttmath::Objects::IsDefined ( const std::wstring &  name)

this method returns true if such an object is defined (name exists)

Definition at line 157 of file ttmathobjects.h.

References ttmath::Misc::AssignString(), IsDefined(), and IsNameCorrect().

158  {
159  // we should check whether the name (in wide characters) are correct
160  // before calling AssignString() function
161  if( !IsNameCorrect(name) )
162  return false;
163 
164  Misc::AssignString(str_tmp1, name);
165 
166  return IsDefined(str_tmp1);
167  }
bool IsDefined(const std::string &name)
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static bool IsNameCorrect(const string_type &name)

§ IsNameCorrect()

template<class string_type >
static bool ttmath::Objects::IsNameCorrect ( const string_type &  name)
static

this method returns true if the name can be as a name of an object

Definition at line 118 of file ttmathobjects.h.

References CorrectCharacter().

Referenced by Add(), Delete(), EditName(), EditValue(), GetValue(), GetValueAndParam(), and IsDefined().

119  {
120  if( name.empty() )
121  return false;
122 
123  if( !CorrectCharacter(name[0], false) )
124  return false;
125 
126  typename string_type::const_iterator i = name.begin();
127 
128  for(++i ; i!=name.end() ; ++i)
129  if( !CorrectCharacter(*i, true) )
130  return false;
131 
132  return true;
133  }
static bool CorrectCharacter(int c, bool can_be_digit)

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