1
#ifndef CHILON_GETSET_HPP
2
#define CHILON_GETSET_HPP
3
4
#include <chilon/attribute.hpp>
5
6
/// Set CHILON_GET_PREFIX to change the prefix of getter methods.
7
8
/// Set CHILON_GET_CONST_PREFIX to change the prefix of const getter methods,
9
/// it defaults to CHILON_GET_PREFIX.
10
11
/// Set CHILON_SET_PREFIX to change the prefix of setter methods
12
13
#define CHILON_CONCAT_HELPER(arg1,arg2) arg1##arg2
14
#define CHILON_CONCAT(arg1,arg2) CHILON_CONCAT_HELPER(arg1,arg2)
15
16
#ifdef CHILON_GET_PREFIX
17
#   define CHILON_GET_PREFIX_HELPER(attribute) \
18
        CHILON_CONCAT(CHILON_GET_PREFIX,attribute)
19
#   ifndef CHILON_GET_CONST_PREFIX
20
#       define CHILON_GET_CONST_PREFIX CHILON_GET_PREFIX
21
#   endif
22
#else
23
#   define CHILON_GET_PREFIX_HELPER(attribute) attribute
24
#endif
25
26
#ifdef CHILON_GET_CONST_PREFIX
27
#   define CHILON_GET_CONST_PREFIX_HELPER(attribute) \
28
        CHILON_CONCAT(CHILON_GET_CONST_PREFIX,attribute)
29
#else
30
#   define CHILON_GET_CONST_PREFIX_HELPER(attribute) attribute
31
#endif
32
33
#ifdef CHILON_SET_PREFIX
34
#   define CHILON_SET_PREFIX_HELPER(attribute) \
35
        CHILON_CONCAT(CHILON_SET_PREFIX,attribute)
36
#else
37
#   define CHILON_SET_PREFIX_HELPER(attribute) attribute
38
#endif
39
40
/// Declare an accessor that returns a const reference.
41
#define CHILON_GET_HELPER(attribute, var_suffix, suffix) \
42
    decltype(CHILON_ATTRIBUTE(attribute)) var_suffix CHILON_GET_CONST_PREFIX_HELPER(attribute)() suffix { return CHILON_ATTRIBUTE(attribute); }
43
44
/// Accesor that returns whatever type the variable is
45
#define CHILON_GET(attribute) \
46
    CHILON_GET_HELPER(attribute, , )
47
48
/// Accesor that returns whatever type the variable is but make it constant
49
#define CHILON_GET_CONST(attribute) \
50
    CHILON_GET_HELPER(attribute, const, const)
51
52
/// Accesors for the attribute, const and mutable
53
#define CHILON_GET_MUTABLE(attribute) \
54
    CHILON_GET(attribute) \
55
    CHILON_GET_CONST(attribute)
56
57
/// Accessor that returns a reference to whatever type the attribute is.
58
#define CHILON_GET_REF(attribute) \
59
    CHILON_GET_HELPER(attribute, &, )
60
61
/// Accessor that returns a const reference.
62
#define CHILON_GET_REF_CONST(attribute) \
63
    CHILON_GET_HELPER(attribute, const&, const)
64
65
/// Accesors that return mutable and const references.
66
#define CHILON_GET_REF_MUTABLE(attribute) \
67
    CHILON_GET_REF(attribute) \
68
    CHILON_GET_REF_CONST(attribute)
69
70
71
#define CHILON_SET_HELPER(attribute) \
72
    void CHILON_SET_PREFIX_HELPER(attribute)(decltype(CHILON_ATTRIBUTE(attribute)) const& ___attribute) { \
73
        CHILON_ATTRIBUTE(attribute) = ___attribute; }
74
75
/// Accessor as for CHILON_GET and add a setter
76
#define CHILON_GETSET(attribute) \
77
    CHILON_GET(attribute) \
78
    CHILON_SET_HELPER(attribute)
79
80
/// Accessor as for CHILON_GET_CONST and add a setter
81
#define CHILON_GETSET_CONST(attribute) \
82
    CHILON_GET_CONST(attribute) \
83
    CHILON_SET_HELPER(attribute)
84
85
/// Accessors as for CHILON_GET_MUTABLE and add a setter
86
#define CHILON_GETSET_MUTABLE(attribute) \
87
    CHILON_GET_MUTABLE(attribute) \
88
    CHILON_SET_HELPER(attribute)
89
90
/// Accessor as for CHILON_GET_REF and add a setter
91
#define CHILON_GETSET_REF(attribute) \
92
    CHILON_GET_REF(attribute) \
93
    CHILON_SET_HELPER(attribute)
94
95
/// Accessor as for CHILON_GET_REF_CONST and add a setter
96
#define CHILON_GETSET_REF_CONST(attribute) \
97
    CHILON_GET_REF_CONST(attribute) \
98
    CHILON_SET_HELPER(attribute)
99
100
/// Accessors as for CHILON_GET_REF_MUTABLE and add a setter
101
#define CHILON_GETSET_REF_MUTABLE(attribute) \
102
    CHILON_GET_REF_MUTABLE(attribute) \
103
    CHILON_SET_HELPER(attribute)
104
105
#endif