OpenASIP  2.0
Options.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file Options.cc
26  *
27  * A generic container of option values.
28  *
29  * @author Jari Mäntyneva 2005 (jari.mantyneva-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <typeinfo>
35 
36 #include "Options.hh"
37 #include "MapTools.hh"
38 #include "OptionValue.hh"
39 
40 using std::string;
41 using std::vector;
42 using std::pair;
43 
44 #include <iostream>
45 using std::cerr;
46 using std::endl;
47 
48 /**
49  * Constructor.
50  */
52 }
53 
54 /**
55  * Destructor.
56  */
58  std::map<string, vector<OptionValue*> >::iterator i = options_.begin();
59  vector<OptionValue*>::iterator j;
60  for (; i != options_.end(); i++) {
61  j = (*i).second.begin();
62  for (; j != (*i).second.end(); j++) {
63  delete (*j);
64  }
65  (*i).second.clear();
66  }
67  options_.clear();
68 }
69 
70 /**
71  * Adds one option value to given option.
72  *
73  * @param name Name of the option.
74  * @param option Value of the option.
75  * @exception TypeMismatch If new value's type differ the old ones.
76  */
77 void
78 Options::addOptionValue(const string& name, OptionValue* option) {
79  try {
80  vector<OptionValue*> values;
81  values = MapTools::valueForKey<vector<OptionValue*> >(options_, name);
82 
83  OptionValue& other = *values.at(0);
84  // check that new value is same type that old ones
85  if (typeid(*option) == typeid(other)) {
86  values.push_back(option);
87  options_.erase(name);
88  options_.insert(pair<string, vector<OptionValue*> >(name, values));
89  } else {
90  string procName = "Options::addOptionValue";
91  string errorMsg =
92  "Type of new OptionValue differs from the type of old values";
93  throw TypeMismatch(__FILE__, __LINE__, procName, errorMsg);
94  }
95  } catch (KeyNotFound& e) {
96  vector<OptionValue*> values;
97  values.push_back(option);
98  options_.insert(pair<string, vector<OptionValue*> >(name, values));
99  }
100 }
101 
102 /**
103  * Returns optionValue of the given option.
104  *
105  * The index must be given between 0 and the option count where 0 is the
106  * latest value.
107  *
108  * @param name Name of the option.
109  * @param index Index for lists of options.
110  * @return The optionValue of an option.
111  * @exception OutOfRange The index is out of range of values.
112  * @exception KeyNotFound No option with given name.
113  */
115 Options::optionValue(const std::string& name, int index) {
116  if (index < 0 || index > valueCount(name) - 1) {
117  string procName = "OptionValue::optionValue";
118  throw OutOfRange(__FILE__, __LINE__, procName);
119  }
120  vector<OptionValue*> values;
121  values = MapTools::valueForKey<vector<OptionValue*> >(options_,name);
122 
123  return *values[values.size() - index - 1];
124 }
125 
126 /**
127  * Returns the count of optionValues of an option.
128  *
129  * @param name Name of the option.
130  * @return Number of values for the option.
131  * @exception KeyNotFound No option with given name.
132  */
133 int
134 Options::valueCount(const std::string& name) {
135  vector<OptionValue*> values;
136  try {
137  values = MapTools::valueForKey<vector<OptionValue*> >(options_,name);
138  } catch (KeyNotFound& e) {
139  string procName = "OptionValue::valueCount";
140  throw KeyNotFound(__FILE__, __LINE__, procName);
141  }
142  return values.size();
143 }
Options::optionValue
OptionValue & optionValue(const string &name, int index=0)
Definition: Options.cc:115
OutOfRange
Definition: Exception.hh:320
MapTools.hh
Options.hh
Options::options_
map< string, vector< OptionValue * > > options_
Definition: Options.hh:62
Options::~Options
virtual ~Options()
Definition: Options.cc:57
TypeMismatch
Definition: Exception.hh:803
Options::addOptionValue
void addOptionValue(const string &name, OptionValue *option)
Definition: Options.cc:78
Options::valueCount
int valueCount(const string &name)
Definition: Options.cc:134
OptionValue
Definition: OptionValue.hh:51
KeyNotFound
Definition: Exception.hh:285
OptionValue.hh
Options::Options
Options()
Definition: Options.cc:51