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 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // An example that demonstrates usage of Sacado::TemplateContainer<> for storing
11 // objects templated on the scalar type
12 
14 
15 #include <iostream>
16 #include "Sacado_mpl_vector.hpp"
18 #include "Sacado.hpp"
19 
20 // A templated class that will be instantiated for several types T
21 template <class T>
22 struct MyClass {
23  T x;
24  MyClass() : x() {} // to avoid uninitialized value warning below
25 };
26 
27 // A functor to initialize a container of objects of type MyClass<T>
28 template <class Container>
29 struct SetFunctor {
30  Container& container;
31  double val;
32  SetFunctor(Container& c, double v) : container(c), val(v) {}
33  template <typename T> void operator()(T) const {
34  container.template get<T>().x = val;
35  }
36 };
37 
38 int main() {
40 
41  // Our scalar types
44 
45  // Container to hold objects of type MyClass<T> for each T in MyTypes
47  MyObjects myObjects;
48 
49  // Print out their initial values
50  std::cout << myObjects.get<double>().x << std::endl;
51  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
52 
53  // Set the values to 1.0 using container_for_each
55  std::cout << myObjects.get<double>().x << std::endl;
56  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
57 
58  // Set the values to 2.0 using mpl::for_each
60  std::cout << myObjects.get<double>().x << std::endl;
61  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
62 
63  // Set the values to 3.0 using mpl::for_each
65  std::cout << myObjects.get<double>().x << std::endl;
66  std::cout << myObjects.get<FadType>().x << std::endl << std::endl;
67 
68  // Test
69  bool passed = ( myObjects.get<double>().x == 3.0 &&
70  myObjects.get<FadType>().x.val() == 3.0 );
71  if (passed)
72  std::cout << "Test Passed!" << std::endl;
73  else
74  std::cout << "Test Failed!" << std::endl;
75 
76  /*
77  // This requires C++14 to use generalized lambdas
78  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
79  typedef decltype(x) T;
80  myObjects.get<T>().x = 4;
81  });
82  Sacado::mpl::for_each_no_kokkos<MyObjects>( [&](auto x) {
83  typedef decltype(x) T;
84  std::cout << myObjects.get<T>().x << std::endl;
85  });
86  */
87 
88  return passed ? 0 : 1;
89 }
Container class to manager template instantiations of a template class.
Sacado::Fad::DFad< double > FadType
SetFunctor(Container &c, double v)
#define T
Definition: Sacado_rad.hpp:553
void container_for_each_no_kokkos(TemplateContainer< TypeSeq, ObjectT > &container, const FunctorT &op)
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:171
Sacado::mpl::apply< ObjectT, T >::type & get()
Get object corrensponding ObjectT&lt;T&gt;
void operator()(T) const
testing::Types< VeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogName, int *, MyArray< bool, 42 > > MyTypes