forked from bitekas/properties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty.hpp
97 lines (78 loc) · 3.68 KB
/
property.hpp
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
#ifndef LIBPROPERTY_PROPERTY_HPP_c
#define LIBPROPERTY_PROPERTY_HPP_c
#if 0
The MIT License (MIT)
Copyright (c) 2015 Gašper Ažman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#endif
#include "property_impl.hpp"
#include <utility> // for std::forward
// only call in class scope!
#define LIBPROPERTY_PROPERTY_WITH_STORAGE(type, name, host, getter, setter) \
struct LIBPROPERTY__TAG_NAME(name); \
::libproperty::rw_property< \
::meta::type_<host::LIBPROPERTY__TAG_NAME(name)>, \
host, \
::meta::value_<decltype(&host::getter), &host::getter>, \
::meta::value_<decltype(&host::setter), &host::setter>, \
type> name; \
auto static constexpr LIBPROPERTY__FUNC_NAME( \
decltype(::libproperty::impl::type_tag(name))) { \
return &host::name; \
} \
/* so that we can have the missing semicolon... */ \
static_assert(true, "")
// end define
#define LIBPROPERTY_PROPERTY(name, host, getter, setter) \
LIBPROPERTY_PROPERTY_WITH_STORAGE(char, name, host, getter, setter)
namespace libproperty {
template <typename TypeTag,
typename Host,
typename Getter,
typename Setter,
typename ValueType>
struct rw_property {
using getter = Getter;
using setter = Setter;
using type_tag = TypeTag;
using host = Host;
using value_type = ValueType;
friend host;
operator decltype(auto)() const {
using namespace ::libproperty::impl;
return (get_host<host>(this).*getter::value)();
}
template <typename X>
decltype(auto) operator=(X&& x) {
using namespace ::libproperty::impl;
return (get_host<host>(this).*setter::value)(std::forward<X>(x));
}
/* TODO: write comment that explains you don't have to use 'value' and that
* it's not required to be related to the getter and setter at all. */
private: // for the use of host, not for nobody's!
ValueType value; // possibly unused.
/// disallow copying for non-friend users of the class - this doesn't have a
/// value, but if copied, it can get really, really bad (stack corruption).
rw_property() = default;
rw_property& operator=(rw_property const&) = default;
rw_property(rw_property const&) = default;
~rw_property() = default;
rw_property& operator=(rw_property&&) = default;
rw_property(rw_property&&) = default;
};
} // property
#endif