PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
VariableMap.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/base/VariableMap.cpp
4// Purpose: Structure that holds global or local variables
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#include "VariableMap.h"
29
30#include "../utility/PError.h"
31#include "../utility/PEBLUtility.h"
32
33#include <iostream>
34#include <map>
35#include <string>
36
37
38using std::cerr;
39using std::cout;
40using std::endl;
41using std::map;
42using std::pair;
43using std::string;
44
45
47{
48 //cout << "Creating variable map\n";
49 //Initiate anything necessary here.
50}
51
52
54{
55
56 //cout << "Deleting variablemap\n";
57 //cout << "contains " << mVariableMap.size() << " values\n";
58 //DumpValues();
59 //Delete mVariableMap if necessary.
60 //Erase things by hand, for debugging's sake
61 // std::map<std::string, Variant>::iterator i = mVariableMap.begin();
62
63 // while(i != mVariableMap.end())
64 // {
65 // i = mVariableMap.begin();
66 // mVariableMap.erase(i);
67 // }
68 mVariableMap.clear();
69
70}
71
72
74{
75 mVariableMap.clear();
76}
77
78
79
84void VariableMap::AddVariable(const string & varname, const Variant & val)
85{
86 string tmpVarName = PEBLUtility::ToUpper(varname);
87
88 map<string, Variant>::iterator p;
89
90 p = mVariableMap.find(tmpVarName);
91
92 //If the variable is in there already, change its value
93
94 if(p!=mVariableMap.end())
95 {
96
97 p->second = val;
98 }
99 else
100 {
101 //variable isn't there yet, so add the new value into map
102 mVariableMap.insert(pair<string, Variant>(tmpVarName, val));
103 }
104
105}
106
107
108
113Variant VariableMap::RetrieveValue(const string & varname)
114{
115
116 map<string,Variant>::iterator p;
117
118 //Get a the variable
119 string tmpVarName = PEBLUtility::ToUpper(varname);
120
121 p = mVariableMap.find(tmpVarName);
122
123 if(p == mVariableMap.end())
124 {
125 string message = "Trying to use an undefined variable: " + string(varname);
127 return Variant(0); //This really won't happen.
128 }
129 else
130 {
131 Variant vt = p->second;
132 return vt;
133 }
134}
135
136
137//Checks to see if a variable exists in the map.
138bool VariableMap::Exists(const string & varname)
139{
140 string tmpVarName = PEBLUtility::ToUpper(varname);
141 map<string,Variant>::iterator p = mVariableMap.find(tmpVarName);
142 if(p == mVariableMap.end())
143 return false;
144 else
145 return true;
146}
147
150void VariableMap::Erase(const string & varname)
151{
152 string tmpVarname = PEBLUtility::ToUpper(varname);
153 mVariableMap.erase(tmpVarname);
154}
155
156
157
163{
164 map<string,Variant>::iterator p;
165
166 for(p= mVariableMap.begin(); p!=mVariableMap.end(); p++)
167 {
168 cout << "VariableName: [" << p->first << ":"<< (p->first).length() << "] | Value: [" << p->second << "]\n";
169 }
170
171}
VariableMap()
The Standard constructor.
void DumpValues()
bool Exists(const std::string &varname)
void Erase(const std::string &varname)
~VariableMap()
The Standard destructor.
void AddVariable(const std::string &varname, const Variant &val)
Variant RetrieveValue(const std::string &varname)
std::string ToUpper(const std::string &text)
void SignalFatalError(const std::string &message)