-
Notifications
You must be signed in to change notification settings - Fork 0
/
optionalargs.h
197 lines (164 loc) · 5.02 KB
/
optionalargs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
#include <algorithm>
template<size_t N>
struct CompileTimeStringLiteral_t
//< Allows us to template by a string constant
{
constexpr CompileTimeStringLiteral_t( const char ( &str )[N])
{
std::copy_n(str, N, m_value);
}
char m_value[N];
};
template<class ValueType_t, CompileTimeStringLiteral_t filenameTag, int lineNumberTag>
struct OptionalArg_t
{
ValueType_t m_value{};
};
/// These macros define option types, using the filename + line number to uniquify the type generated.
#define DECLARE_OPTION( optionName, ValueType_t ) \
using optionName = OptionalArg_t<ValueType_t, __FILE__, __LINE__>;
#define DECLARE_OPTION_DEFAULT( optionName, ValueType_t, defaultValue ) \
struct optionName \
{ \
ValueType_t m_value = defaultValue; \
}
//: Search functions for parameter packs:
template<class T>
inline constexpr auto GetOptionValue()
{
T tmp;
return tmp.m_value;
}
template<typename T, typename FirstArgType_t, typename ... restTypes>
inline constexpr auto GetOptionValue( FirstArgType_t arg, restTypes... restArgs )
{
if constexpr( std::is_same_v<FirstArgType_t, T> )
{
return arg.m_value; // for options that wrap the value in a trivial struct
}
else
{
return GetOptionValue<T>( restArgs... );
}
}
template<class T, class FirstArgType_t>
inline constexpr auto GetOptionValue( FirstArgType_t arg )
{
if constexpr( std::is_same_v<FirstArgType_t, T> )
{
return arg.m_value;
}
else
{
T tmp;
return tmp.m_value;
}
}
//: Gets w/ provided default value for when the option isn't present:
template<class T>
inline constexpr auto GetOptionValueWithDefault( auto defaultValue )
{
return defaultValue;
}
template<typename T, typename FirstArgType_t, typename ... restTypes, class ValueType_t>
inline constexpr ValueType_t GetOptionValueWithDefault( ValueType_t defaultValue, FirstArgType_t arg, restTypes... restArgs )
{
if constexpr( std::is_same_v<FirstArgType_t, T> )
{
if constexpr ( std::is_convertible_v<FirstArgType_t, ValueType_t> ) // for scalar options like enums
{
return arg;
}
else
{
return arg.m_value; // for options that wrap the value in a trivial struct
}
}
else
{
return GetOptionValueWithDefault<T>( defaultValue, restArgs... );
}
}
template<class T, class FirstArgType_t, class ValueType_t>
inline constexpr ValueType_t GetOptionValueWithDefault( ValueType_t defaultValue, FirstArgType_t arg )
{
if constexpr( std::is_same_v<FirstArgType_t, T> )
{
return arg.m_value;
}
else
{
return defaultValue;
}
}
#ifdef OPTIONALARGS_SAMPLE_TEST_CODE
// Sample usage and compilation test
namespace opt
//< Use a namespace for option types so as not to pollute the clobal space with short names
{
DECLARE_OPTION_DEFAULT( ItemCount, int, 256 );
DECLARE_OPTION_DEFAULT( VerboseLogs, bool, false );
};
//: Example of a function taking named options:
template< typename ...optionTypes_t>
void TestFunction( char const *pString, optionTypes_t... options )
{
Log( "TestFunction ", pString );
int nItemCount = GetOptionValue<opt::ItemCount>( options...);
if ( GetOptionValue<opt::VerboseLogs>( options...) )
{
Log( "Item count=%d", nItemCount );
}
}
//: Example of a class with a constructor taking named options:
class TestClass
{
public:
template< typename ...optionTypes_t>
TestClass( char const *pMsg, optionTypes_t... options )
{
// We will call the SetOption method with each optional arg instead of using GetOptionValue. This gets you compile-time type checking for unsupported args
Log( "TestClass", pMsg );
( SetOption( options), ...); // fold expression. Calls our SetItem with each option
m_pData.reset( new int[m_nNumItems] );
if ( m_bVerboseLogs )
{
Log( "item count=%d", m_nNumItems );
}
}
void SetOption( opt::ItemCount x ) { m_nNumItems = x.m_value; }
void SetOption( opt::VerboseLogs x ) { m_bVerboseLogs = x.m_value; }
int m_nNumItems = 101;
std::unique_ptr<int[]> m_pData;
bool m_bVerboseLogs = false;
};
//: Example of a class with named template arguments:
template<auto... options>
class TestTemplate
{
public:
int m_nData[GetOptionValue<opt::ItemCount>( options...)];
bool m_bVerboseLogs = GetOptionValue<opt::VerboseLogs>( options... );
TestTemplate()
{
Log( "TestTemplate" );
if constexpr( GetOptionValue<opt::VerboseLogs>( options... ) )
{
Log( "data size=%?", sizeof( m_nData ) );
}
}
};
inline void TestFunctionality()
{
TestFunction( "no args" );
TestFunction( "itemcount=50", opt::ItemCount( 50 ) );
TestFunction( "both args", opt::VerboseLogs(), opt::ItemCount( 100 ) );
TestTemplate x;
TestTemplate<opt::VerboseLogs( true )> x1;
TestTemplate<opt::ItemCount( 50 ), opt::VerboseLogs( true)> x2;
TestClass yy( "no args" );;
TestClass y( "vblogs arg", opt::VerboseLogs( true) );
TestClass y2( "both args", opt::VerboseLogs( true), opt::ItemCount( 1024 ) );
}
#endif