PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
FunctionMap Class Reference

#include <FunctionMap.h>

Public Member Functions

 FunctionMap ()
 The Standard constructor.
 
 ~FunctionMap ()
 The Standard destructor.

 
void AddFunction (std::string funcname, OpNode *node)
 
PNodeGetFunction (const std::string &funcname)
 
bool IsFunction (const std::string &funcname)
 
void Erase (const std::string &funcname)
 
void DumpValues ()
 
void Destroy ()
 

Detailed Description

This is the primary structure for holding user-defined functions. It is an STL map that takes a string-based function name and stores a PNode. FunctionMap uses an STL multimap, which allows us to have many functions with the same name. The most recently loaded function blocks earlier ones, but earlier ones can be access if the later ones are deleted.

Definition at line 45 of file FunctionMap.h.

Constructor & Destructor Documentation

◆ FunctionMap()

FunctionMap::FunctionMap ( )

The Standard constructor.

This class stores pointers to user-defined pnode-parsed functions, which can be accessed by name. This includes the main function of the PEBL program.

Definition at line 53 of file FunctionMap.cpp.

54{
55 //Initiate anything necessary here.
56}

◆ ~FunctionMap()

FunctionMap::~FunctionMap ( )

The Standard destructor.

Definition at line 59 of file FunctionMap.cpp.

60{
61
62 //cout << "Detelitng functionmap\n";
63 //Delete mFunctionMap if necessary.
64 //go through each function and delete the opnode, because
65 //it is a pointer that is held nowhere else.
66 Destroy();
67}

References Destroy().

Member Function Documentation

◆ AddFunction()

void FunctionMap::AddFunction ( std::string  funcname,
OpNode node 
)

This method will add a new variable with name varname and value val to the variable map, or (if it already exists) change its value to val.

Definition at line 94 of file FunctionMap.cpp.

95{
96
97 //Get the uppercase version of the funcname.
98
99 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
100 //std::cout << "Adding " << upperfuncname << endl;
101 map<string, PNode*>::iterator p;
102 p = mFunctionMap.find(upperfuncname);
103
104 //If a function is in there already, just signal an error.
105 // Maybe we should clean up the PNode tree that
106 //is there and add the new one in its place.
107 if(p!=mFunctionMap.end())
108 {
109 cerr << "Error in function '" << upperfuncname << "'. Function already exists.\n";
110 }
111 else
112 {
113 //variable isn't there yet, so add the new value into map
114 mFunctionMap.insert(pair<string, PNode* >(upperfuncname, node));
115 }
116}
std::string ToUpper(const std::string &text)

References PEBLUtility::ToUpper().

Referenced by Loader::LoadLibraryFunctions(), Loader::LoadUserFunctions(), and main().

◆ Destroy()

void FunctionMap::Destroy ( )

Definition at line 69 of file FunctionMap.cpp.

70{
71
72 //Delete mFunctionMap if necessary.
73 //go through each function and delete the opnode, because
74 //it is a pointer that is held nowhere else.
75 std::map<std::string, PNode*>::iterator i = mFunctionMap.begin();
76
77 while(i != mFunctionMap.end())
78 {
79 if(i->second)
80 {
81 i->second->DestroyChildren();
82 delete i->second;
83 i->second = NULL;
84 }
85 i++;
86 }
87 mFunctionMap.clear();
88}
#define NULL
Definition BinReloc.cpp:317

References NULL.

Referenced by CaptureSignal(), PEBLInterpret(), and ~FunctionMap().

◆ DumpValues()

void FunctionMap::DumpValues ( )

This method is primarily for debugging purposes. It will iterate through the entire variable map, and Display each of the values held in the map.

Definition at line 212 of file FunctionMap.cpp.

213{
214 map<string,PNode *>::iterator p;
215 cerr << "---------------------------\n Function Map:\n";
216 for(p= mFunctionMap.begin(); p!=mFunctionMap.end(); p++)
217 {
218 cerr << "Function Name: [" << std::flush;
219 cerr << p->first << ":";
220 cerr << ":" << *(p->second) << "]\n" ;
221
222 }
223
224}

Referenced by GetFunction(), Loader::LoadLibraryFunctions(), main(), and PEBLInterpret().

◆ Erase()

void FunctionMap::Erase ( const std::string &  funcname)

This will erase the topmost value stored in varname

Definition at line 200 of file FunctionMap.cpp.

201{
202 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
203 mFunctionMap.erase(upperfuncname);
204}

References PEBLUtility::ToUpper().

Referenced by main().

◆ GetFunction()

PNode * FunctionMap::GetFunction ( const std::string &  funcname)

This method will add a new function with name and code associated with the OpNode node, which is of type PEBL_FUNCTION. This method will retrieve the value designated by funcname. If funcname doesn't exist, it will return a null pointer.

Definition at line 140 of file FunctionMap.cpp.

141{
142 // Convert the function name to uppercase, so that it will find
143 // a match when searching through the upper-case functionmap.
144
145 std::string funcname = PEBLUtility::ToUpper(lcasefuncname);
146 map<string,PNode*>::iterator p;
147
148 //Get a the function
149
150 p = mFunctionMap.find(funcname);
151
152 if(p == mFunctionMap.end())
153 {
154 //This should probably signal an error.
155 PError::SignalFatalError(string("Function Name [")+ funcname+string("] not found.\n"));
156 DumpValues();
157 return NULL;//new DataNode(Variant(PEBL_DATA_NODE),"PEBL SELF-GENERATED OBJECT",-1);
158 }
159 else
160 {
161
162 //Otherwise, just return a pointer to the node.
163 //cast it to the appropriate type.
164 if((p->second)->GetType() == PEBL_OP_NODE)
165 return (OpNode*)(p->second);
166
167 else if((p->second)->GetType() == PEBL_DATA_NODE)
168 return (DataNode*)(p->second);
169
170 else
171 return p->second;
172 }
173
174}
@ PEBL_OP_NODE
Definition PNode.h:35
@ PEBL_DATA_NODE
Definition PNode.h:36
void DumpValues()
void SignalFatalError(const std::string &message)

References DumpValues(), NULL, PEBL_DATA_NODE, PEBL_OP_NODE, PError::SignalFatalError(), and PEBLUtility::ToUpper().

Referenced by Evaluator::CallFunction(), Evaluator::Evaluate1(), Loader::GetMainPEBLFunction(), and main().

◆ IsFunction()

bool FunctionMap::IsFunction ( const std::string &  funcname)

This function just determines whether there is a function of the given name. It is useful because GetFunction() may behave badly when the function doesn't exist. It converts the query to uppercase, because everything in the functionmap should be uppercase.

Definition at line 182 of file FunctionMap.cpp.

183{
184
185 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
186 //Get a the function from the map
187 map<string,PNode*>::iterator p;
188 p = mFunctionMap.find(upperfuncname);
189
190 //If it isn't in there, return false, otherwise true.
191 if(p == mFunctionMap.end())
192 return false;
193 else
194 return true;
195}

References PEBLUtility::ToUpper().


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