GEOSX
DefaultValue.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 Total, S.A
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOSX_DATAREPOSITORY_DEFAULTVALUE_HPP_
20 #define GEOSX_DATAREPOSITORY_DEFAULTVALUE_HPP_
21 
22 // Source includes
23 #include "common/DataTypes.hpp"
24 #include "codingUtilities/traits.hpp"
25 
26 namespace geosx
27 {
28 namespace dataRepository
29 {
30 namespace internal
31 {
32 
38 template< typename T >
39 struct is_defaultable
40 {
42  static constexpr bool value = std::is_arithmetic< T > ::value ||
43  std::is_same< T, string >::value ||
44  std::is_same< T, Path >::value ||
45  traits::is_tensorT< T > ||
46  std::is_enum< T >::value;
47 };
48 
56 template< typename T, typename ENABLE=void >
57 struct Helper
58 {
60  static constexpr bool has_default_value = false;
61 
63  using value_type = void;
64 };
65 
74 template< typename T >
75 struct Helper< T, std::enable_if_t< is_defaultable< T >::value > >
76 {
78  static constexpr bool has_default_value = true;
79 
81  using value_type = T;
82 
84  value_type value = value_type();
85 };
86 
96 template< typename T >
97 struct Helper< T, std::enable_if_t< traits::HasAlias_value_type< T > &&
98  is_defaultable< typename T::value_type >::value &&
99  !traits::is_string< T > &&
100  !traits::is_tensorT< T > > >
101 {
103  static constexpr bool has_default_value = true;
104 
106  using value_type = typename T::value_type;
107 
109  value_type value = value_type();
110 };
111 
112 template< typename T >
113 std::enable_if_t< !Helper< T >::has_default_value, std::ostream & >
114 operator<<( std::ostream & stream, Helper< T > const & GEOSX_UNUSED_PARAM( value ) )
115 {
116  return stream;
117 }
118 
119 template< typename T >
120 std::enable_if_t< Helper< T >::has_default_value, std::ostream & >
121 operator<<( std::ostream & stream, Helper< T > const & value )
122 {
123  return stream << value.value;
124 }
125 
126 } // namespace internal
127 
132 template< typename T >
133 using DefaultValue = internal::Helper< T >;
134 
135 } // namespace dataRepository
136 } // namespace geosx
137 
138 
139 #endif /* GEOSX_DATAREPOSITORY_DEFAULTVALUE_HPP_ */
internal::Helper< T > DefaultValue
A templated alias to hold default values.
#define GEOSX_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
Definition: GeosxMacros.hpp:66