Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_StandardDependencies.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 
45 #include "Teuchos_StandardParameterEntryValidators.hpp"
46 
47 
48 namespace Teuchos{
49 
50 
53  RCP<ParameterEntry> dependent,
54  bool showIf):
55  Dependency(dependee, dependent),
56  dependentVisible_(false),
57  showIf_(showIf)
58  {}
59 
62  ParameterEntryList dependents,
63  bool showIf):
64  Dependency(dependee, dependents),
65  dependentVisible_(false),
66  showIf_(showIf)
67  {}
68 
70  ConstParameterEntryList dependees,
71  RCP<ParameterEntry> dependent,
72  bool showIf):
73  Dependency(dependees, dependent),
74  dependentVisible_(false),
75  showIf_(showIf)
76  {}
77 
79  ConstParameterEntryList dependees,
80  ParameterEntryList dependents,
81  bool showIf):
82  Dependency(dependees, dependents),
83  dependentVisible_(false),
84  showIf_(showIf)
85  {}
86 
88  return dependentVisible_;
89 }
90 
92  return showIf_;
93 }
94 
96  if((getDependeeState() && showIf_) || (!getDependeeState() && !showIf_)){
97  dependentVisible_ = true;
98  }
99  else{
100  dependentVisible_ = false;
101  }
102 }
103 
105  RCP<const ParameterEntry> dependee,
106  RCP<ParameterEntry> dependent):
107  Dependency(dependee, dependent){}
108 
110  RCP<const ParameterEntry> dependee,
111  ParameterEntryList dependents):
112  Dependency(dependee, dependents){}
113 
115  RCP<const ParameterEntry> dependee,
116  RCP<ParameterEntry> dependent,
117  std::string value,
118  bool showIf):
119  VisualDependency(dependee, dependent, showIf),
120  values_(ValueList(1,value))
121 {
122  validateDep();
123 }
124 
126  RCP<const ParameterEntry> dependee,
127  RCP<ParameterEntry> dependent,
128  const ValueList& values,
129  bool showIf):
130  VisualDependency(dependee, dependent, showIf),
131  values_(values)
132 {
133  validateDep();
134 }
135 
137  RCP<const ParameterEntry> dependee,
139  const std::string& value,
140  bool showIf):
141  VisualDependency(dependee, dependents, showIf),
142  values_(ValueList(1,value))
143 {
144  validateDep();
145 }
146 
148  RCP<const ParameterEntry> dependee,
150  const ValueList& values,
151  bool showIf):
152  VisualDependency(dependee, dependents, showIf),
153  values_(values)
154 {
155  validateDep();
156 }
157 
160 {
161  return values_;
162 }
163 
165  return find(values_.begin(), values_.end(),
166  getFirstDependeeValue<std::string>()) != values_.end();
167 }
168 
170  return "StringVisualDependency";
171 }
172 
174  TEUCHOS_TEST_FOR_EXCEPTION(!getFirstDependee()->isType<std::string>(),
176  "Ay no! The dependee of a "
177  "String Visual Dependency must be of type "
178  << TypeNameTraits<std::string>::name() << std::endl <<
179  "Type encountered: " << getFirstDependee()->getAny().typeName() <<
180  std::endl << std::endl);
181 }
182 
185 {
186  std::string blahString = "blah";
187  return rcp(new StringVisualDependency(
188  rcp(new ParameterEntry(blahString)),
190  "i'm a dummy"));
191 }
192 
194  RCP<const ParameterEntry> dependee,
195  RCP<ParameterEntry> dependent,
196  bool showIf):
197  VisualDependency(dependee, dependent, showIf)
198 {
199  validateDep();
200 }
201 
203  RCP<const ParameterEntry> dependee,
205  bool showIf):
206  VisualDependency(dependee, dependents, showIf)
207 {
208  validateDep();
209 }
210 
212  return getFirstDependeeValue<bool>();
213 }
214 
216  return "BoolVisualDependency";
217 }
218 
220  TEUCHOS_TEST_FOR_EXCEPTION(!getFirstDependee()->isType<bool>(),
222  "Ay no! The dependee of a "
223  "Bool Visual Dependency must be of type " <<
224  TypeNameTraits<bool>::name() << std::endl <<
225  "Encountered type: " << getFirstDependee()->getAny().typeName() <<
226  std::endl << std::endl);
227 }
228 
231 {
232  return rcp(new BoolVisualDependency(
233  rcp(new ParameterEntry(true)),
235 }
236 
238  RCP<const Condition> condition,
239  RCP<ParameterEntry> dependent,
240  bool showIf):
241  VisualDependency(condition->getAllParameters(), dependent, showIf),
242  condition_(condition)
243 {
244  validateDep();
245 }
246 
248  RCP<const Condition> condition,
250  bool showIf):
251  VisualDependency(condition->getAllParameters(), dependents, showIf),
252  condition_(condition)
253 {
254  validateDep();
255 }
256 
258  return condition_;
259 }
260 
262  return condition_->isConditionTrue();
263 }
264 
266  return "ConditionVisualDependency";
267 }
268 
271 {
272  return rcp(new ConditionVisualDependency(
275 }
276 
278  RCP<const ParameterEntry> dependee,
279  RCP<ParameterEntry> dependent,
280  ValueToValidatorMap valuesAndValidators,
281  RCP<ParameterEntryValidator> defaultValidator):
282  ValidatorDependency(dependee, dependent),
283  valuesAndValidators_(valuesAndValidators),
284  defaultValidator_(defaultValidator)
285 {
286  validateDep();
287 }
288 
290  RCP<const ParameterEntry> dependee,
292  ValueToValidatorMap valuesAndValidators,
293  RCP<ParameterEntryValidator> defaultValidator):
294  ValidatorDependency(dependee, dependents),
295  valuesAndValidators_(valuesAndValidators),
296  defaultValidator_(defaultValidator)
297 {
298  validateDep();
299 }
300 
303 {
304  return valuesAndValidators_;
305 }
306 
309 {
310  return defaultValidator_;
311 }
312 
314  std::string currentDependeeValue = getFirstDependeeValue<std::string>();
315  for(
316  ParameterEntryList::iterator it = getDependents().begin();
317  it != getDependents().end();
318  ++it)
319  {
320  if(
321  valuesAndValidators_.find(currentDependeeValue)
322  ==
323  valuesAndValidators_.end())
324  {
325  (*it)->setValidator(defaultValidator_);
326  }
327  else{
328  (*it)->setValidator(valuesAndValidators_[currentDependeeValue]);
329  }
330  }
331 }
332 
334  return "StringValidatorDependency";
335 }
336 
338  TEUCHOS_TEST_FOR_EXCEPTION(!getFirstDependee()->isType<std::string>(),
340  "Ay no! The dependee of a "
341  "String Validator Dependency must be of type " <<
342  TypeNameTraits<std::string>::name() << std::endl <<
343  "Type Encountered: " << getFirstDependee()->getAny().typeName() <<
344  std::endl << std::endl);
345 
347  valuesAndValidators_.size() < 1,
349  "The valuesAndValidatord map for a string validator dependency must "
350  "have at least one entry!" << std::endl << std::endl);
351  ValueToValidatorMap::const_iterator it = valuesAndValidators_.begin();
352  RCP<const ParameterEntryValidator> firstVali = (it->second);
353  ++it;
354  for(; it != valuesAndValidators_.end(); ++it){
355  TEUCHOS_TEST_FOR_EXCEPTION( typeid(*firstVali) != typeid(*(it->second)),
357  "Ay no! All of the validators in a StringValidatorDependency "
358  "must have the same type.");
359  }
360 }
361 
362 
365 {
366  std::string blahString = "blah";
370  return rcp(new StringValidatorDependency(
371  rcp(new ParameterEntry(blahString)),
373  dummyMap));
374 }
375 
377  RCP<const ParameterEntry> dependee,
378  RCP<ParameterEntry> dependent,
380  RCP<const ParameterEntryValidator> falseValidator):
381  ValidatorDependency(dependee, dependent),
382  trueValidator_(trueValidator),
383  falseValidator_(falseValidator)
384 {
385  validateDep();
386 }
387 
389  RCP<const ParameterEntry> dependee,
392  RCP<const ParameterEntryValidator> falseValidator):
393  ValidatorDependency(dependee, dependents),
394  trueValidator_(trueValidator),
395  falseValidator_(falseValidator)
396 {
397  validateDep();
398 }
399 
401  bool dependeeValue = getFirstDependeeValue<bool>();
402  for(
403  ParameterEntryList::iterator it = getDependents().begin();
404  it != getDependents().end();
405  ++it)
406  {
407  dependeeValue ?
408  (*it)->setValidator(trueValidator_)
409  :
410  (*it)->setValidator(falseValidator_);
411  }
412 }
413 
416 {
417  return trueValidator_;
418 }
419 
422 {
423  return falseValidator_;
424 }
425 
427  return "BoolValidatorDependency";
428 }
429 
430 
432 
433  TEUCHOS_TEST_FOR_EXCEPTION(!getFirstDependee()->isType<bool>(),
435  "Ay no! The dependee of a "
436  "Bool Validator Dependency must be of type " <<
437  TypeNameTraits<bool>::name() << std::endl <<
438  "Encountered type: " << getFirstDependee()->getAny().typeName() <<
439  std::endl << std::endl);
440 
441  if(!falseValidator_.is_null() && !trueValidator_.is_null()){
442  TEUCHOS_TEST_FOR_EXCEPTION(typeid(*falseValidator_) != typeid(*trueValidator_),
444  "Ay no! The true and false validators of a Bool Validator Dependency "
445  "must be the same type! " <<std::endl << std::endl);
446  }
447 
448 }
449 
452 {
453  return rcp(new BoolValidatorDependency(
454  rcp(new ParameterEntry(true)),
456  null, null));
457 }
458 
459 }
460 
RCP< const ParameterEntryValidator > getDefaultValidator() const
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
std::pair< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorPair
Conveniece typedef.
std::set< RCP< const ParameterEntry >, RCPConstComp > ConstParameterEntryList
A list of dependents.
An abstract parent class for all visual dependencies.
ParameterEntryList & getDependents()
Gets the dependents of the dependency.
BoolValidatorDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent, RCP< const ParameterEntryValidator > trueValidator, RCP< const ParameterEntryValidator > falseValidator=null)
Constructs a BoolValidatorDependency.
void validateDep() const
Validates the dependency to make sure it&#39;s valid/has been setup properly. If subclassing, this fucntion should be called in the new subclasses constructor.
std::set< RCP< ParameterEntry >, RCPComp > ParameterEntryList
A list of Dependees.
This object is held as the "value" in the Teuchos::ParameterList std::map.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool getShowIf() const
Get&#39;s the value of the showIf variable.
const ValueToValidatorMap & getValuesAndValidators() const
retrieve a const reference to the ValueToValidator map being used by this StringValidatorDependency ...
VisualDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent, bool showIf=getShowIfDefaultValue())
Constructs a VisualDependency.
std::map< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorMap
Conveniece typedef.
This class represents a depndency between elements in a Parameter List.
const ValueList & getValues() const
get the List of values the dependee will be checked against.
RCP< const ParameterEntryValidator > getFalseValidator() const
Gets the false validator.
BoolVisualDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent, bool showIf=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
StringVisualDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent, std::string value, bool showIf=true)
Constructs a StringVisualDependency.
RCP< const ParameterEntryValidator > getTrueValidator() const
Gets the true validator.
ValidatorDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent)
Constructs a ValidatorDependency.
A collection of standard dependencies.
RCP< const ParameterEntry > getFirstDependee() const
Gets the first dependee in the dependees list. This is a convience function.
RCP< const Condition > getCondition() const
Gets the condition being used in this dependency.
Default traits class that just returns typeid(T).name().
Class for retrieving a dummy object of type T.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
ConditionVisualDependency(RCP< const Condition > condition, RCP< ParameterEntry > dependent, bool showIf=true)
An abstract base class for all validator dependencies.
StringValidatorDependency(RCP< const ParameterEntry > dependee, RCP< ParameterEntry > dependent, ValueToValidatorMap valuesAndValidators, RCP< ParameterEntryValidator > defaultValidator=null)
Constructs a StringValidatorDependency.
Smart reference counting pointer class for automatic garbage collection.
bool isDependentVisible() const
Determines whether or not the dependent is currently visible.
virtual bool getDependeeState() const =0
Get the state of the dependee in order to evaluate the dependency.
iterator begin()
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
void validateDep() const
Validates the dependency to make sure it&#39;s valid/has been setup properly. If subclassing, this fucntion should be called in the new subclasses constructor.