Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  //using the raw pointer avoids a Clang warning about side effects in typeid
354  const ParameterEntryValidator* rawFirstValidatorPtr = firstVali.get();
355  ++it;
356  for(; it != valuesAndValidators_.end(); ++it){
357  const ParameterEntryValidator* rawValidatorPtr = it->second.get();
358  TEUCHOS_TEST_FOR_EXCEPTION( typeid(*rawFirstValidatorPtr) != typeid(*rawValidatorPtr),
360  "Ay no! All of the validators in a StringValidatorDependency "
361  "must have the same type.");
362  }
363 }
364 
365 
368 {
369  std::string blahString = "blah";
373  return rcp(new StringValidatorDependency(
374  rcp(new ParameterEntry(blahString)),
376  dummyMap));
377 }
378 
380  RCP<const ParameterEntry> dependee,
381  RCP<ParameterEntry> dependent,
383  RCP<const ParameterEntryValidator> falseValidator):
384  ValidatorDependency(dependee, dependent),
385  trueValidator_(trueValidator),
386  falseValidator_(falseValidator)
387 {
388  validateDep();
389 }
390 
392  RCP<const ParameterEntry> dependee,
395  RCP<const ParameterEntryValidator> falseValidator):
396  ValidatorDependency(dependee, dependents),
397  trueValidator_(trueValidator),
398  falseValidator_(falseValidator)
399 {
400  validateDep();
401 }
402 
404  bool dependeeValue = getFirstDependeeValue<bool>();
405  for(
406  ParameterEntryList::iterator it = getDependents().begin();
407  it != getDependents().end();
408  ++it)
409  {
410  dependeeValue ?
411  (*it)->setValidator(trueValidator_)
412  :
413  (*it)->setValidator(falseValidator_);
414  }
415 }
416 
419 {
420  return trueValidator_;
421 }
422 
425 {
426  return falseValidator_;
427 }
428 
430  return "BoolValidatorDependency";
431 }
432 
433 
435 
436  TEUCHOS_TEST_FOR_EXCEPTION(!getFirstDependee()->isType<bool>(),
438  "Ay no! The dependee of a "
439  "Bool Validator Dependency must be of type " <<
440  TypeNameTraits<bool>::name() << std::endl <<
441  "Encountered type: " << getFirstDependee()->getAny().typeName() <<
442  std::endl << std::endl);
443 
444  if(!falseValidator_.is_null() && !trueValidator_.is_null()){
445  //avoid Clang warnings about side effects in typeid
446  const ParameterEntryValidator* rawFalseValidatorPtr = falseValidator_.get();
447  const ParameterEntryValidator* rawTrueValidatorPtr = trueValidator_.get();
448  TEUCHOS_TEST_FOR_EXCEPTION(typeid(*rawFalseValidatorPtr) != typeid(*rawTrueValidatorPtr),
450  "Ay no! The true and false validators of a Bool Validator Dependency "
451  "must be the same type! " <<std::endl << std::endl);
452  }
453 
454 }
455 
458 {
459  return rcp(new BoolValidatorDependency(
460  rcp(new ParameterEntry(true)),
462  null, null));
463 }
464 
465 }
466 
RCP< const ParameterEntryValidator > getDefaultValidator() const
static RCP< T > getDummyObject()
Retrieves a dummy object of type T.
A BoolValidatorDependency says the following about the relationship between two parameters: Dependeni...
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.
A string visual depdencies says the following about the relationship between two elements in a Parame...
A bool visual dependency says the following about the relationship between two elements in a Paramete...
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 &quot;value&quot; 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 ...
T * get() const
Get the raw C++ pointer to the underlying object.
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.
Abstract interface for an object that can validate a ParameterEntry&#39;s value.
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.
A StringValidatorDependency says the following about the relationship between two parameters: Depende...
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.
A condition visual dependency says the following about the relationship between elements in a Paramet...
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.