OpenASIP  2.0
CommandRegistry.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 CommandRegistry.cc
26  *
27  * Definition of CommandRegistry class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  * @note reviewed Jun 23 2004 by ml, jn, jm, vpj
32  */
33 
34 #include "CommandRegistry.hh"
35 #include "GUICommand.hh"
36 
37 using std::vector;
38 using std::string;
39 
40 
41 /**
42  * The Constructor.
43  */
45 }
46 
47 
48 /**
49  * The Destructor.
50  */
52  // delete commands in registry
53  vector<GUICommand*>::iterator i = commands_.begin();
54  for (; i != commands_.end(); i++) {
55  delete *i;
56  }
57  commands_.clear();
58 }
59 
60 
61 /**
62  * Creates a new command corresponding to the id.
63  *
64  * @param id Enumerated ID of the GUICommand.
65  * @return Created GUICommand, or NULL, if no GUICommand was found
66  * with the ID.
67  */
70  vector<GUICommand*>::iterator i = commands_.begin();
71  for (; i != commands_.end(); i++) {
72  if ((*i)->id() == id) {
73  return (*i)->create();
74  }
75  }
76  return NULL;
77 }
78 
79 /**
80  * Creates a new command corresponding to the command name.
81  *
82  * @param name Name of the GUICommand.
83  * @return Created GUICommand, or NULL, if no GUICommand was found
84  * with the name.
85  */
87 CommandRegistry::createCommand(const std::string name) {
88  vector<GUICommand*>::iterator i = commands_.begin();
89  for (; i != commands_.end(); i++) {
90  if ((*i)->name() == name) {
91  return (*i)->create();
92  }
93  }
94  return NULL;
95 }
96 
97 
98 /**
99  * Returns first command in the registry.
100  *
101  * @return Pointer to the first command in the registry.
102  */
103 GUICommand*
105  iterator_ = commands_.begin();
106  if (iterator_ == commands_.end()) {
107  return NULL;
108  }
109  return (*iterator_);
110 }
111 
112 
113 
114 /**
115  * Returns next command in the registry.
116  *
117  * @return Pointer to the next command in the registry.
118  */
119 GUICommand*
121  iterator_++;
122  if (iterator_ == commands_.end()) {
123  return NULL;
124  }
125  return (*iterator_);
126 }
127 
128 
129 /**
130  * Returns enumerated ID of the command.
131  *
132  * @param name Name of the GUICommand.
133  * @return Enumerated ID of the GUICommand, or -1 if no command was
134  * found.
135  */
136 int
137 CommandRegistry::commandId(const std::string name) const {
138 
139  vector<GUICommand*>::const_iterator i = commands_.begin();
140  for (; i != commands_.end(); i++) {
141  if ((*i)->name() == name) {
142  return (*i)->id();
143  }
144  }
145  return -1;
146 }
147 
148 /**
149  * Returns icon for the GUICommand.
150  *
151  * @param name Name of the GUICommand.
152  * @return Icon for the GUICommand.
153  */
154 std::string
155 CommandRegistry::commandIcon(const std::string name) const {
156  vector<GUICommand*>::const_iterator i = commands_.begin();
157  for (; i != commands_.end(); i++) {
158  if ((*i)->name() == name) {
159  return (*i)->icon();
160  }
161  }
162  throw InstanceNotFound(__FILE__, __LINE__, __func__, "");
163  return "";
164 }
165 
166 /**
167  * Returns name of the GUICommand.
168  *
169  * @param id ID of the GUICommand.
170  * @return Command name.
171  */
172 std::string
174  vector<GUICommand*>::const_iterator i = commands_.begin();
175  for (; i != commands_.end(); i++) {
176  if ((*i)->id() == id) {
177  return (*i)->name();
178  }
179  }
180  throw InstanceNotFound(__FILE__, __LINE__, __func__, "");
181  return "";
182 }
183 
184 /**
185  * Returns short name of the GUICommand.
186  *
187  * @param name Name of the GUICommand.
188  * @return Short version of the command name.
189  */
190 std::string
191 CommandRegistry::commandShortName(const std::string name) const {
192  vector<GUICommand*>::const_iterator i = commands_.begin();
193  for (; i != commands_.end(); i++) {
194  if ((*i)->name() == name) {
195  return (*i)->shortName();
196  }
197  }
198  throw InstanceNotFound(__FILE__, __LINE__, __func__, "");
199  return "";
200 }
201 
202 /**
203  * Returns true, if command is executable and should be enabled,
204  * false if not.
205  *
206  * @param command The command which is checked for enabled/disabled state.
207  * @return true, if the command is executable.
208  */
209 bool
210 CommandRegistry::isEnabled(const std::string command) {
211  vector<GUICommand*>::iterator i = commands_.begin();
212  for (; i != commands_.end(); i++) {
213  if ((*i)->name() == command) {
214  return (*i)->isEnabled();
215  }
216  }
217  throw InstanceNotFound(__FILE__, __LINE__, __func__, "");
218  return false;
219 }
CommandRegistry::commandName
std::string commandName(int id) const
Definition: CommandRegistry.cc:173
CommandRegistry::isEnabled
bool isEnabled(const std::string command)
Definition: CommandRegistry.cc:210
CommandRegistry::commandShortName
std::string commandShortName(const std::string name) const
Definition: CommandRegistry.cc:191
CommandRegistry.hh
CommandRegistry::~CommandRegistry
~CommandRegistry()
Definition: CommandRegistry.cc:51
CommandRegistry::createCommand
GUICommand * createCommand(const int id)
Definition: CommandRegistry.cc:69
GUICommand.hh
GUICommand
Definition: GUICommand.hh:43
CommandRegistry::CommandRegistry
CommandRegistry()
Definition: CommandRegistry.cc:44
CommandRegistry::commandIcon
std::string commandIcon(const std::string name) const
Definition: CommandRegistry.cc:155
CommandRegistry::commands_
std::vector< GUICommand * > commands_
Commands in the registry.
Definition: CommandRegistry.hh:65
__func__
#define __func__
Definition: Application.hh:67
CommandRegistry::iterator_
std::vector< GUICommand * >::iterator iterator_
The position of the iteration. Used by the firstCommand() and nextCommand().
Definition: CommandRegistry.hh:68
CommandRegistry::nextCommand
GUICommand * nextCommand()
Definition: CommandRegistry.cc:120
CommandRegistry::commandId
int commandId(const std::string name) const
Definition: CommandRegistry.cc:137
CommandRegistry::firstCommand
GUICommand * firstCommand()
Definition: CommandRegistry.cc:104
InstanceNotFound
Definition: Exception.hh:304