ROL
ROL_Sacado_Traits.hpp
Go to the documentation of this file.
1#ifndef ROL_SACADO_TRAITS_HPP
2#define ROL_SACADO_TRAITS_HPP
3
4#include <type_traits>
5#include <utility>
6
7#include "Sacado.hpp"
8#include "ROL_Objective.hpp"
9
10constexpr bool is_Objective( ... ) { return false; }
11constexpr bool is_Constraint( ... ) { return false; }
12
13template<class Real>
14constexpr bool is_Objective( const ROL::Objective<Real>& ) { return true; }
15
16template<class Real>
17constexpr bool is_Constraint( const ROL::Constraint<Real>& ) { return true; }
18
19constexpr bool is_DFad( ... ) { return false; }
20constexpr bool is_SFad( ... ) { return false; }
21
22template<typename T>
23constexpr bool is_DFad( const Sacado::Fad::DFad<T>& ) { return true; }
24
25template<typename T, int Num>
26constexpr bool is_SFad( const Sacado::Fad::SFad<T,Num>& ) { return true; }
27
28struct InvalidType {};
29
30constexpr bool InvalidType Fad_value( ... );
31
32template<typename T>
33constexpr T Fad_value( const Sacado::Fad::DFad<T>& );
34
35template<typename T, int Num>
36constexpr T Fad_value( const Sacado::Fad::SFad<T,Num>& );
37
38template<typename T>
39using Fad_value_t = decltype( Fad_value( std::declval<T>() ) );
40
41template<typename VectorType>
42struct Accessor {};
43
44template<typename Real>
45struct Accessor<std::vector<Real>> {
46 using container_type = std::vector<Real>;
47 using value_type = typename std::vector<Real>::value_type;
48 using size_type = typename std::vector<Real>::size_type;
49 inline value_type get_value( const container_type& x, size_type i ) const {
50 return x[i];
51 }
52 inline void set_value( container_type& x, size_type i, value_type xi ) const {
53 x[i] = xi;
54 }
55};
56
57// 1. Define the trait structure
58template <typename T, typename = void> // SFINAE helper for more complex traits, not strictly needed here but good practice
59struct is_accessor_specialization : std::false_type {};
60
61// 2. Define the partial specialization for Accessor<VectorType>
62template <typename VectorType>
63struct is_accessor_specialization<Accessor<VectorType>> : std::true_type {};
64
65// 3. Define the C++17 style _v variable template
66template <typename T>
68
69// --- Example Usage (C++17) ---
70
71// SFINAE example for a function
72template <typename T,
73 typename std::enable_if_t<has_accessor_v<T>, int> = 0>
74void process_if_accessor(const T& acc_specialization) {
75 // This function will only be available in overload resolution if has_accessor_v<T> is true.
76 // You can use acc_specialization here.
77 // For demonstration:
78 if constexpr (has_accessor_v<T>) { // if constexpr is C++17
79 // Additional compile-time checks or type usage
80 }
81}
82
83
84template<typename Real>
86 template<typename ScalarT>
87 ScalarT value
88};
89
90#endif // ROL_SACADO_TRAITS_HPP
constexpr bool is_DFad(...)
constexpr bool InvalidType Fad_value(...)
void process_if_accessor(const T &acc_specialization)
constexpr bool is_Constraint(...)
constexpr bool is_SFad(...)
constexpr bool has_accessor_v
constexpr bool is_Objective(...)
decltype(Fad_value(std::declval< T >())) Fad_value_t
Defines the general constraint operator interface.
Provides the interface to evaluate objective functions.
void value(ROL::Vector< Real > &c, const ROL::Vector< Real > &sol, const Real &mu)
typename std::vector< Real >::size_type size_type
typename std::vector< Real >::value_type value_type
value_type get_value(const container_type &x, size_type i) const
void set_value(container_type &x, size_type i, value_type xi) const