Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_getParamTryingTypes.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) 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 // ***********************************************************************
38 //@HEADER
39 */
40 
41 #ifndef IFPACK2_DETAILS_GETPARAMTRYINGTYPES_HPP
42 #define IFPACK2_DETAILS_GETPARAMTRYINGTYPES_HPP
43 
44 #include "Ifpack2_config.h"
47 
48 namespace Ifpack2 {
49 namespace Details {
50 
51 template<class ... CandidateTypes>
52 struct GetParamTryingTypes {
53  template<class ResultType>
54  static void
55  get (ResultType& result,
56  const Teuchos::ParameterEntry& ent,
57  const std::string& paramName,
58  const char prefix[]);
59 };
60 
61 template<>
62 struct GetParamTryingTypes<> {
63  template<class ResultType>
64  static void
65  get (ResultType& /* result */,
66  const Teuchos::ParameterEntry& /* ent */,
67  const std::string& paramName,
68  const char prefix[])
69  {
72  (true, std::invalid_argument, prefix << "\"" << paramName
73  << "\" parameter exists in input ParameterList, but does not "
74  "have the right type. The proper type is "
75  << TypeNameTraits<ResultType>::name () << ".");
76  }
77 };
78 
79 template<class First, class ... Rest>
80 struct GetParamTryingTypes<First, Rest...> {
81  template<class ResultType>
82  static void
83  get (ResultType& result,
84  const Teuchos::ParameterEntry& ent,
85  const std::string& paramName,
86  const char prefix[])
87  {
88  if (ent.template isType<First> ()) {
89  result = static_cast<ResultType> (Teuchos::getValue<First> (ent));
90  }
91  else {
92  using rest_type = GetParamTryingTypes<Rest...>;
93  rest_type::template get<ResultType> (result, ent, paramName, prefix);
94  }
95  }
96 };
97 
98 template<class ResultType, class ... CandidateTypes>
99 void
100 getParamTryingTypes (ResultType& result,
101  const Teuchos::ParameterList& params,
102  const std::string& paramName,
103  const char prefix[])
104 {
106  const ParameterEntry* ent = params.getEntryPtr (paramName);
107  if (ent != nullptr) {
108  using impl_type = GetParamTryingTypes<CandidateTypes...>;
109  impl_type::template get<ResultType> (result, *ent, paramName, prefix);
110  }
111 }
112 
113 } // namespace Details
114 } // namespace Ifpack2
115 
116 #endif // IFPACK2_DETAILS_GETPARAMTRYINGTYPES_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterEntry * getEntryPtr(const std::string &name)