Sacado Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
example
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
33
#include "
Sacado_TemplateContainer.hpp
"
34
35
#include <iostream>
36
#include "
Sacado_mpl_vector.hpp
"
37
#include "
Sacado_mpl_placeholders.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
() {
59
using
Sacado::mpl::placeholders::_
;
60
61
// Our scalar types
62
typedef
Sacado::Fad::DFad<double>
FadType
;
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
79
Sacado::mpl::for_each_no_kokkos<MyTypes>
(
SetFunctor<MyObjects>
(myObjects,2) );
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
84
Sacado::mpl::for_each_no_kokkos<MyObjects>
(
SetFunctor<MyObjects>
(myObjects,3) );
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.hpp
c
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
Definition
Sacado_LFad_LogicalSparseOps.hpp:450
Sacado_TemplateContainer.hpp
Sacado_mpl_placeholders.hpp
Sacado_mpl_vector.hpp
T
#define T
Definition
Sacado_rad.hpp:573
FadType
Sacado::Fad::DFad< double > FadType
Definition
blas_example.cpp:49
Sacado::Fad::DFad
Definition
Sacado_Fad_DFadTraits.hpp:65
Sacado::TemplateContainer
Container class to manager template instantiations of a template class.
Definition
Sacado_TemplateContainer.hpp:96
Sacado::TemplateContainer::get
Sacado::mpl::apply< ObjectT, T >::type & get()
Get object corrensponding ObjectT<T>.
Definition
Sacado_TemplateContainer.hpp:138
MyTypes
testing::Types< VeryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogName, int *, MyArray< bool, 42 > > MyTypes
Definition
googletest-list-tests-unittest_.cc:124
Sacado::mpl::placeholders::_
arg<-1 > _
Definition
Sacado_mpl_placeholders.hpp:102
Sacado::container_for_each_no_kokkos
void container_for_each_no_kokkos(TemplateContainer< TypeSeq, ObjectT > &container, const FunctorT &op)
Definition
Sacado_TemplateContainer.hpp:174
MyClass::MyClass
MyClass()
Definition
template_container_example.cpp:44
MyClass::x
T x
Definition
template_container_example.cpp:43
Sacado::mpl::for_each_no_kokkos
Definition
Sacado_mpl_for_each.hpp:68
Sacado::mpl::vector
Definition
Sacado_mpl_vector.hpp:57
SetFunctor
Definition
template_container_example.cpp:49
SetFunctor::val
double val
Definition
template_container_example.cpp:51
SetFunctor::container
Container & container
Definition
template_container_example.cpp:50
SetFunctor::SetFunctor
SetFunctor(Container &c, double v)
Definition
template_container_example.cpp:52
SetFunctor::operator()
void operator()(T) const
Definition
template_container_example.cpp:53
main
int main()
Definition
template_container_example.cpp:58
Generated by
1.17.0