Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  MyClass() : x() {} // to avoid uninitialized value warning below
45 };
46 
47 // A functor to initialize a container of objects of type MyClass<T>
48 template <class Container>
49 struct SetFunctor {
50  Container& container;
51  double val;
52  SetFunctor(Container& c, double v) : container(c), val(v) {}
53  template <typename T> void operator()(T) const {
54  container.template get<T>().x = val;
55  }
56 };
57 
58 int main() {
60 
61  // Our scalar types
63  typedef Sacado::mpl::vector<double,FadType> MyTypes;
64 
65  // Container to hold objects of type MyClass<T> for each T in MyTypes
66  typedef Sacado::TemplateContainer< MyTypes,MyClass<_> > MyObjects;
67  MyObjects myObjects;
68 
69  // Print out their initial values
70  std::cout << myObjects.get<double>().x << std::endl;
71  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
72 
73  // Set the values to 1.0 using container_for_each
74  Sacado::container_for_each_no_kokkos( myObjects, SetFunctor<MyObjects>(myObjects,1) );
75  std::cout << myObjects.get<double>().x << std::endl;
76  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
77 
78  // Set the values to 2.0 using mpl::for_each
80  std::cout << myObjects.get<double>().x << std::endl;
81  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
82 
83  // Set the values to 3.0 using mpl::for_each
85  std::cout << myObjects.get<double>().x << std::endl;
86  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
87 
88  // Test
89  bool passed = ( myObjects.get<double>().x == 3.0 &&
90  myObjects.get<FadType>().x.val() == 3.0 );
91  if (passed)
92  std::cout << "Test Passed!" << std::endl;
93  else
94  std::cout << "Test Failed!" << std::endl;
95 
96  /*
97  // This requires C++14 to use generalized lambdas
98  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
99  typedef decltype(x) T;
100  myObjects.get<T>().x = 4;
101  });
102  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
103  typedef decltype(x) T;
104  std::cout << myObjects.get<T>().x << std::endl;
105  });
106  */
107 
108  return passed ? 0 : 1;
109 }
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
int main()
Definition: ad_example.cpp:191
void operator()(T) const