summaryrefslogtreecommitdiff
path: root/project2/iHaveParameters.cpp
blob: e254a4f26ad2d0d1c683ca81487b6cb83f096441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "iHaveParameters.h"
#include "exceptions.h"
#include "appEngine.h"
#include <boost/foreach.hpp>

IHaveParameters::Stack IHaveParameters::scope;

IHaveParameters::IHaveParameters(const xmlpp::Element * p)
{
	BOOST_FOREACH(xmlpp::Node * node, p->find("parameters/param")) {
		const xmlpp::Element * elem = dynamic_cast<const xmlpp::Element *>(node);
		if (elem) {
			ParameterPtr p = new Parameter(elem);
			parameters[p->name] = p;
		}
	}
}

IHaveParameters::~IHaveParameters()
{
}

IHaveParameters::Parameter::Parameter(const xmlpp::Element * p) :
	name(p->get_attribute_value("name")),
	value(p, "value")
{
}

VariableType
IHaveParameters::getParameter(const Glib::ustring & name) const
{
	Parameters::const_iterator i = parameters.find(name);
	if (i != parameters.end()) {
		return i->second->value;
	}
	throw ParamNotFound(name);
}

void
IHaveParameters::push(const IHaveParameters * p)
{
	scope.push_back(p);
}

void
IHaveParameters::pop(const IHaveParameters * p)
{
	assert(scope.back() == p);
	scope.pop_back();
}

VariableType
IHaveParameters::getScopedParameter(const Glib::ustring & name)
{
	for(Stack::const_reverse_iterator ihp = scope.rbegin(); ihp != scope.rend(); ihp++) {
		Parameters::const_iterator i = (*ihp)->parameters.find(name);
		if (i != (*ihp)->parameters.end()) {
			return i->second->value;
		}
	}
	throw ParamNotFound(name);
}

const IHaveParameters::Parameters &
IHaveParameters::allParameters() const
{
	return parameters;
}