Bridge++  Ver. 2.0.2
evalexpr_symbol.cpp
Go to the documentation of this file.
1 
14 #include "evalexpr_symbol.h"
15 
16 using Bridge::vout;
17 
18 //====================================================================
19 bool SymbolTable::find_symbol(const std::string& name)
20 {
21  return table.find(name) != table.end();
22 }
23 
24 
25 //====================================================================
26 bool SymbolTable::put_symbol(const std::string& name, const double value)
27 {
28  if (find_symbol(name)) {
29  vout.detailed("key \"%s\" already exists. overwrite.\n", name.c_str());
30  }
31 
32  SymbolRecord rec;
33  rec.type = VARIABLE;
34  rec.value.val = value;
35  table[name] = rec;
36 
37  return true;
38 }
39 
40 
41 //====================================================================
42 bool SymbolTable::put_symbol(const std::string& name, const function_t tptr)
43 {
44  if (find_symbol(name)) {
45  vout.detailed("key \"%s\" already exists. overwrite.\n", name.c_str());
46  }
47 
48  SymbolRecord rec;
49  rec.type = FUNCTION;
50  rec.value.fptr = tptr;
51  table[name] = rec;
52 
53  return true;
54 }
55 
56 
57 //====================================================================
58 double SymbolTable::get_symbol_value(const std::string& name) const
59 {
60  SymbolMap_t::const_iterator p = table.find(name);
61 
62  if (p != table.end()) {
63  return p->second.value.val;
64  } else {
65  vout.detailed("key \"%s\" not found.\n", name.c_str());
66  return double();
67  }
68 }
69 
70 
71 //====================================================================
72 function_t SymbolTable::get_symbol_function(const std::string& name) const
73 {
74  SymbolMap_t::const_iterator p = table.find(name);
75 
76  if (p != table.end()) {
77  return p->second.value.fptr;
78  } else {
79  vout.detailed("key \"%s\" not found.\n", name.c_str());
80  return (function_t)0;
81  }
82 }
83 
84 
85 //====================================================================
86 void SymbolTable::print() const
87 {
88  for (SymbolMap_t::const_iterator p = table.begin(); p != table.end(); ++p) {
89  vout.paranoiac("key = %s, ", (p->first).c_str());
90 
91  ValueType t = (p->second).type;
92  if (t == VARIABLE) {
93  vout.paranoiac("type = VARIABLE, value = %f", (p->second).value.val);
94  } else if (t == FUNCTION) {
95  vout.paranoiac("type = FUNCTION, value = %p", (p->second).value.fptr);
96  } else {
97  vout.paranoiac("type = UNKNOWN, ");
98  }
99 
100  vout.paranoiac("\n");
101  }
102 }
103 
104 
105 //==========================================================
106 //==================================================END=====
SymbolTable::FUNCTION
@ FUNCTION
Definition: evalexpr_symbol.h:27
SymbolTable::get_symbol_value
double get_symbol_value(const std::string &name) const
Definition: evalexpr_symbol.cpp:58
SymbolTable::put_symbol
bool put_symbol(const std::string &name, const double value)
Definition: evalexpr_symbol.cpp:26
SymbolTable::print
void print() const
Definition: evalexpr_symbol.cpp:86
SymbolTable::SymbolRecord::value
union SymbolTable::SymbolRecord::@1 value
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
function_t
double(* function_t)(double)
Definition: evalexpr_symbol.h:21
SymbolTable::SymbolRecord::fptr
function_t fptr
Definition: evalexpr_symbol.h:35
SymbolTable::table
SymbolMap_t table
Definition: evalexpr_symbol.h:42
Bridge::BridgeIO::paranoiac
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:238
SymbolTable::ValueType
ValueType
Definition: evalexpr_symbol.h:27
SymbolTable::SymbolRecord::val
double val
Definition: evalexpr_symbol.h:34
SymbolTable::SymbolRecord::type
ValueType type
Definition: evalexpr_symbol.h:31
evalexpr_symbol.h
SymbolTable::SymbolRecord
Definition: evalexpr_symbol.h:29
SymbolTable::find_symbol
bool find_symbol(const std::string &name)
Definition: evalexpr_symbol.cpp:19
SymbolTable::get_symbol_function
function_t get_symbol_function(const std::string &name) const
Definition: evalexpr_symbol.cpp:72
SymbolTable::VARIABLE
@ VARIABLE
Definition: evalexpr_symbol.h:27
Element_type::type
type
Definition: bridge_defs.h:41
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512