Sacado Package Browser (Single Doxygen Collection)  Version of the Day
template_container_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 // An example that demonstrates usage of Sacado::TemplateContainer<> for storing
31 // objects templated on the scalar type
32 
34 
35 #include <iostream>
36 #include "Sacado_mpl_vector.hpp"
38 #include "Sacado.hpp"
39 
40 // A templated class that will be instantiated for several types T
41 template <class T>
42 struct MyClass {
43  T x;
44 };
45 
46 // A functor to initialize a container of objects of type MyClass<T>
47 template <class Container>
48 struct SetFunctor {
49  Container& container;
50  double val;
51  SetFunctor(Container& c, double v) : container(c), val(v) {}
52  template <typename T> void operator()(T) const {
53  container.template get<T>().x = val;
54  }
55 };
56 
57 int main() {
59 
60  // Our scalar types
62  typedef Sacado::mpl::vector<double,FadType> MyTypes;
63 
64  // Container to hold objects of type MyClass<T> for each T in MyTypes
65  typedef Sacado::TemplateContainer< MyTypes,MyClass<_> > MyObjects;
66  MyObjects myObjects;
67 
68  // Print out their initial values
69  std::cout << myObjects.get<double>().x << std::endl;
70  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
71 
72  // Set the values to 1.0 using container_for_each
73  Sacado::container_for_each_no_kokkos( myObjects, SetFunctor<MyObjects>(myObjects,1) );
74  std::cout << myObjects.get<double>().x << std::endl;
75  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
76 
77  // Set the values to 2.0 using mpl::for_each
79  std::cout << myObjects.get<double>().x << std::endl;
80  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
81 
82  // Set the values to 3.0 using mpl::for_each
84  std::cout << myObjects.get<double>().x << std::endl;
85  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
86 
87  // Test
88  bool passed = ( myObjects.get<double>().x == 3.0 &&
89  myObjects.get<FadType>().x.val() == 3.0 );
90  if (passed)
91  std::cout << "Test Passed!" << std::endl;
92  else
93  std::cout << "Test Failed!" << std::endl;
94 
95  /*
96  // This requires C++14 to use generalized lambdas
97  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
98  typedef decltype(x) T;
99  myObjects.get<T>().x = 4;
100  });
101  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
102  typedef decltype(x) T;
103  std::cout << myObjects.get<T>().x << std::endl;
104  });
105  */
106 
107  return passed ? 0 : 1;
108 }
Sacado::Fad::DFad< double > FadType
SetFunctor(Container &c, double v)
#define T
Definition: Sacado_rad.hpp:573
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c