1/*
2 * Copyright (c) 2012-2014, TU Delft
3 * Copyright (c) 2012-2014, TU Eindhoven
4 * Copyright (c) 2012-2014, TU Kaiserslautern
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * Authors: Andreas Hansson
35 *
36 */
37
38#include "Parametrisable.h"
39
40#include <iostream>
41#include <cstdlib>
42
43using namespace Data;
44using namespace std;
45
46void Parametrisable::pushParameter(const Parameter& parameter)
47{
48  parameters.push_back(parameter);
49}
50
51void Parametrisable::setParameter(const Parameter& parameter,
52                                  unsigned int     index)
53{
54  unsigned int count            = 0;
55
56  vector<Parameter>::iterator p = parameters.begin();
57
58  while (p != parameters.end() && !(p->getId() == parameter.getId() &&
59                                    index == count)) {
60    if (p->getId() == parameter.getId())
61      ++count;
62    ++p;
63  }
64
65  if (p == parameters.end()) {
66    parameters.push_back(parameter);
67  } else {
68    p = parameters.erase(p);
69    parameters.insert(p, parameter);
70  }
71} // Parametrisable::setParameter
72
73bool Parametrisable::removeParameter(const string& id, unsigned int index)
74{
75  unsigned int count = 0;
76
77  for (vector<Parameter>::iterator p = parameters.begin();
78       p != parameters.end(); ++p) {
79    if ((p->getId() == id) && (index == count++)) {
80      parameters.erase(p);
81      return true;
82    }
83  }
84
85  return false;
86}
87
88/**
89 * Get a parameter with a specific id. Should there be a multiplicity,
90 * then the index is used to determine which instance is returned, in
91 * order traversal.
92 */
93Parameter Parametrisable::getParameter(const string& id,
94                                       unsigned int  index) const
95{
96  unsigned int count = 0;
97
98  for (vector<Parameter>::const_iterator p = parameters.begin();
99       p != parameters.end(); ++p) {
100    if ((p->getId() == id) && (index == count++)) {
101      return *p;
102    }
103  }
104
105  cerr << "Could not find parameter '" << id << "' (" << index << ")" << endl;
106  cerr << "Stored parameters are: " << endl;
107  for (vector<Parameter>::const_iterator p = parameters.begin();
108       p != parameters.end(); ++p) {
109    cerr << "   " << p->getId() << ": " << p->getValue() << endl;
110  }
111  exit(1);
112
113  return Parameter("", "", "");
114} // Parametrisable::getParameter
115
116vector<Parameter> Parametrisable::getParameters() const
117{
118  return parameters;
119}
120
121bool Parametrisable::hasParameter(const string& id, unsigned int index) const
122{
123  unsigned int count = 0;
124
125  for (vector<Parameter>::const_iterator p = parameters.begin();
126       p != parameters.end(); ++p) {
127    if ((p->getId() == id) && (index == count++)) {
128      return true;
129    }
130  }
131
132  return false;
133}
134