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
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
#ifndef __TEST__BUILDER_PATTERN_HPP__
#define __TEST__BUILDER_PATTERN_HPP__
namespace Test {
template<class Field>
struct DefaultValue {
using Type = typename Field::Type;
static Option<Type> value() { return Option<Type>(); }
};
template<class C>
struct BuilderInitializer
{ using Type = C; };
template<class A>
struct BuilderInitializer<Stack<A>>
{ using Type = std::initializer_list<A>; };
#define BUILDER_METHOD(Self, ty, field) \
private: \
Option<ty> _##field; \
\
Option<ty> field() const \
{ \
return _##field.isSome() \
? _##field \
: getDefault<__##field##_default>(); \
} \
\
public: \
Self field(typename BuilderInitializer<ty>::Type field) \
{ \
_##field = Option<ty>(std::move(field)); \
return std::move(*this); \
} \
struct __ ## field ## _default { using Type = ty; }; \
\
\
template<class Field>
Option<typename Field::Type> getDefault()
{ return DefaultValue<Field>::value(); }
} // namespace Test
#endif // __TEST__BUILDER_PATTERN_HPP__