NCBI C++ ToolKit
PluginArgSet.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: PluginArgSet.cpp 14666 2007-07-09 13:40:22Z dicuccio $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Mike DiCuccio, Denis Vakatov, Anatoliy Kuznetsov
27  *
28  * File Description:
29  * CPluginArgSet -- defines an interface for collections of plugin arguments
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using specifications from the data definition file
34  * 'plugin.asn'.
35  */
36 
37 // standard includes
38 #include <ncbi_pch.hpp>
40 
41 // generated includes
45 
46 // generated classes
47 
49 
50 BEGIN_objects_SCOPE // namespace ncbi::objects::
51 
52 // destructor
54 {
55 }
56 
57 
59 {
60  NON_CONST_ITERATE (Tdata, iter, Set()) {
61  (*iter)->ClearObjects();
62  }
63 }
64 
65 //
66 // indexing operator for accessing arguments based on names
67 //
68 const CPluginArg& CPluginArgSet::operator[] (const string& name) const
69 {
70  ITERATE (Tdata, iter, Get()) {
71  if ( (*iter)->GetName() == name) {
72  return (**iter);
73  }
74  }
75 
76  // not found - we throw
77  NCBI_THROW(CPluginException, eInvalidArg,
78  "No argument named '" + name + "'");
79 }
80 
81 
83 {
84  NON_CONST_ITERATE (Tdata, iter, Set()) {
85  if ( (*iter)->GetName() == name) {
86  return (**iter);
87  }
88  }
89 
90  // not found - we throw
91  NCBI_THROW(CPluginException, eInvalidArg,
92  "No argument named '" + name + "'");
93 }
94 
95 
96 // HasArgument() can be used to tell whether this set of arguments contains an
97 // argument named 'name'
98 bool CPluginArgSet::HasArgument(const string& name) const
99 {
100  ITERATE (Tdata, iter, Get()) {
101  if ( (*iter)->GetName() == name) {
102  return true;
103  }
104  }
105 
106  return false;
107 }
108 
109 /// Delete an existing argument
110 void CPluginArgSet::RemoveArgument(const string& arg_name)
111 {
112  NON_CONST_ITERATE (Tdata, iter, Set()) {
113  if ( (*iter)->GetName() == arg_name) {
114  Set().erase(iter);
115  break;
116  }
117  }
118 }
119 
120 
121 
122 // add a named argument. This argument is required, and has no default
123 // value. Attempts to access the value without setting the value will
124 // result in an exception being thrown.
125 CPluginArg&
126 CPluginArgSet::AddArgument(const string& name, const string& desc,
128  CPluginArg::TData::E_Choice single_or_array)
129 {
130  if (HasArgument(name)) {
131  NCBI_THROW(CPluginException, eInvalidArg,
132  string("Argument `") + name + string("' already exists"));
133  }
134 
135  CRef<CPluginArg> arg(new CPluginArg());
136  switch (type) {
138  arg->SetBoolean();
139  break;
141  arg->SetInteger();
142  break;
143  case CPluginArg::eDouble:
144  arg->SetDouble();
145  break;
146  case CPluginArg::eString:
147  arg->SetString();
148  break;
150  arg->SetSecretString();
151  break;
153  arg->SetInputFile();
154  break;
156  arg->SetOutputFile();
157  break;
159  arg->SetProject();
160  break;
161  case CPluginArg::eObject:
162  arg->SetObject();
163  break;
165  arg->SetObject("SerialObject");
166  break;
167 
168  default:
169  case CPluginArg::eNotSet:
170  NCBI_THROW(CPluginException, eInvalidArg,
171  "Invalid argument type in AddArgument()");
172  }
173 
174  if (single_or_array == CPluginArg::TData::e_Array) {
175  arg->SetList();
176  }
177 
178  arg->SetName(name);
179  arg->SetDesc(desc);
180  Set().push_back(arg);
181  return *arg;
182 }
183 
184 
185 // add a named argument. This argument is required, and has no default
186 // value. Attempts to access the value without setting the value will
187 // result in an exception being thrown.
188 CPluginArg&
189 CPluginArgSet::AddArgument(const string& name, const string& desc,
190  const CTypeInfo* type,
191  CPluginArg::TData::E_Choice single_or_array)
192 {
193  if (HasArgument(name)) {
194  NCBI_THROW(CPluginException, eInvalidArg,
195  string("Argument `") + name + string("' already exists"));
196  }
197 
198  CRef<CPluginArg> arg(new CPluginArg());
199  arg->SetObject(type);
200  arg->SetName(name);
201  arg->SetDesc(desc);
202 
203  if (single_or_array == CPluginArg::TData::e_Array) {
204  arg->SetList();
205  }
206 
207  Set().push_back(arg);
208  return *arg;
209 }
210 
211 
212 // Add a default argument to the set of arguments. This function creates a
213 // named argument with a default value of type string.
214 CPluginArg&
215 CPluginArgSet::AddDefaultArgument(const string& name, const string& desc,
217  const string& val)
218 {
219  if (HasArgument(name)) {
220  NCBI_THROW(CPluginException, eInvalidArg,
221  string("Argument `") + name + string("' already exists"));
222  }
223 
224  CRef<CPluginArg> arg(new CPluginArg());
225  arg->SetName(name);
226  arg->SetDesc(desc);
227  arg->SetDefault(true);
228  switch (type) {
230  arg->SetInteger(NStr::StringToInt(val));
231  break;
232 
234  arg->SetBoolean(NStr::StringToBool(val));
235  break;
236 
237  case CPluginArg::eDouble:
238  arg->SetDouble(NStr::StringToDouble(val));
239  break;
240 
242  arg->SetSecretString(val);
243  break;
244 
245  case CPluginArg::eString:
246  arg->SetString(val);
247  break;
248 
250  arg->SetInputFile(val);
251  break;
252 
254  arg->SetOutputFile(val);
255  break;
256 
257  default:
258  NCBI_THROW(CPluginException, eInvalidArg,
259  "Attempt to create non-String argument type with "
260  "string default value");
261  }
262 
263  Set().push_back(arg);
264  return *arg;
265 }
266 
267 
268 CPluginArg&
269 CPluginArgSet::AddFlag(const string& name, const string& desc)
270 {
271  return AddArgument(name, desc, CPluginArg::eBoolean);
272 }
273 
274 
275 CPluginArg&
276 CPluginArgSet::AddDefaultFlag(const string& name, const string& desc,
277  bool val)
278 {
279  return AddDefaultArgument(name, desc, CPluginArg::eBoolean,
281 }
282 
283 
284 // add an optional argument to the set of arguments. This supports an
285 // optional default value argument. For non-built-in types, the default
286 // value is ignored.
287 CPluginArg&
288 CPluginArgSet::AddOptionalArgument(const string& name, const string& desc,
291 {
292  if (HasArgument(name)) {
293  NCBI_THROW(CPluginException, eInvalidArg,
294  string("Argument `") + name + string("' already exists"));
295  }
296 
297  CRef<CPluginArg> arg(new CPluginArg());
298  switch (type) {
300  arg->SetBoolean();
301  break;
303  arg->SetInteger();
304  break;
305  case CPluginArg::eDouble:
306  arg->SetDouble();
307  break;
309  arg->SetSecretString();
310  break;
311  case CPluginArg::eString:
312  arg->SetString();
313  break;
315  arg->SetInputFile();
316  break;
318  arg->SetOutputFile();
319  break;
321  arg->SetProject();
322  break;
323  case CPluginArg::eObject:
324  arg->SetObject();
325  break;
327  arg->SetObject("SerialObject");
328  break;
329 
330  default:
331  case CPluginArg::eNotSet:
332  NCBI_THROW(CPluginException, eInvalidArg,
333  "Invalid argument type in AddArgument()");
334  }
335 
336  arg->SetName(name);
337  arg->SetDesc(desc);
338  arg->SetOptional(true);
339  if (s_or_a == CPluginArg::TData::e_Array) {
340  arg->SetList();
341  }
342 
343  Set().push_back(arg);
344  return *arg;
345 }
346 
347 
348 CPluginArg&
349 CPluginArgSet::AddOptionalArgument(const string& name, const string& desc,
350  const CTypeInfo* info,
352 {
353  if (HasArgument(name)) {
354  NCBI_THROW(CPluginException, eInvalidArg,
355  string("Argument `") + name + string("' already exists"));
356  }
357 
358  CRef<CPluginArg> arg(new CPluginArg());
359  arg->SetName(name);
360  arg->SetDesc(desc);
361  arg->SetOptional(true);
362  arg->SetObject(info);
363  if (s_or_a == CPluginArg::TData::e_Array) {
364  arg->SetList();
365  }
366 
367  Set().push_back(arg);
368  return *arg;
369 }
370 
371 
372 //
373 // SetConstraint() resets the constraints for a given named argument and adds a
374 // new argument constraint
375 //
376 void CPluginArgSet::SetConstraint(const string& name,
377  CPluginValueConstraint& constraint)
378 {
379  CPluginArg& arg = (*this)[name];
380  arg.SetConstraint().clear();
381  arg.SetConstraint().push_back( CRef<CPluginValueConstraint>(&constraint) );
382 }
383 
384 
385 //
386 // AddConstraint() adds a constraint to a named argument
387 //
388 void CPluginArgSet::AddConstraint(const string& name,
389  CPluginValueConstraint& constraint)
390 {
391  CPluginArg& arg = (*this)[name];
392  arg.SetConstraint().push_back( CRef<CPluginValueConstraint>(&constraint) );
393 }
394 
395 void CPluginArgSet::RemoveConstraint(const string& name)
396 {
397  CPluginArg& arg = (*this)[name];
398  arg.ResetConstraint();
399 }
400 
401 
402 END_objects_SCOPE // namespace ncbi::objects::
403 
405 
406 /* Original file checksum: lines: 64, chars: 1873, CRC32: 1571727e */
CPluginArg & AddFlag(const string &name, const string &desc)
Add a flag value.
void RemoveArgument(const string &arg_name)
Delete an existing argument.
void RemoveConstraint(const string &name)
Remove the constraint of a named argument.
void SetConstraint(const string &name, CPluginValueConstraint &constraint)
Set the constraint of a named argument.
bool HasArgument(const string &name) const
HasArgument() verifies that an argument exists.
~CPluginArgSet(void)
destructor
CPluginArg & AddArgument(const string &name, const string &desc, CPluginArg::EType type, CPluginArg::TData::E_Choice single_or_array=CPluginArg::TData::e_Single)
add a named argument.
void AddConstraint(const string &name, CPluginValueConstraint &constraint)
Add a constraint to a named argument.
void ClearObjects()
Clear all objects from object-based arguments.
CPluginArg & AddOptionalArgument(const string &name, const string &description, CPluginArg::EType type, CPluginArg::TData::E_Choice single_or_array=CPluginArg::TData::e_Single)
add an optional argument to the set of arguments.
const CPluginArg & operator[](const string &name) const
operator[] for indexing based on named argument this will throw if the named argument isn't found
CPluginArg & AddDefaultArgument(const string &name, const string &description, CPluginArg::EType type, const string &val)
Add a default argument to the set of arguments.
CPluginArg & AddDefaultFlag(const string &name, const string &desc, bool val)
Add a flag argument with a default value.
EType
enumerated list of types we support
Definition: PluginArg.hpp:69
@ eAnySerialObject
special type: could be any CSerialObject-derived object
Definition: PluginArg.hpp:83
CRef –.
Definition: ncbiobj.hpp:618
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static bool StringToBool(const CTempString str)
Convert string to bool.
Definition: ncbistr.cpp:2819
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
static double StringToDouble(const CTempStringEx str, TStringToNumFlags flags=0)
Convert string to double.
Definition: ncbistr.cpp:1387
static const string BoolToString(bool value)
Convert bool to string.
Definition: ncbistr.cpp:2813
void ResetConstraint(void)
Reset Constraint data member.
Definition: PluginArg_.cpp:185
TConstraint & SetConstraint(void)
Assign a value to Constraint data member.
Definition: PluginArg_.hpp:997
list< CRef< CPluginArg > > Tdata
Tdata & Set(void)
Assign a value to data member.
E_Choice
Choice variants.
Definition: PluginArg_.hpp:115
const Tdata & Get(void) const
Get the member data.
static MDB_envinfo info
Definition: mdb_load.c:37
Definition: type.c:6
Modified on Wed Mar 27 11:19:24 2024 by modify_doxy.py rev. 669887