]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/property.h
Merge branch 'optional_q3map2_type' into 'master'
[xonotic/netradiant.git] / libs / property.h
1 #ifndef INCLUDED_IMPORTEXPORT_H
2 #define INCLUDED_IMPORTEXPORT_H
3
4 #include "generic/callback.h"
5 #include "string/string.h"
6
7 template<class T>
8 struct Property {
9     // todo: just return T, don't use continuation passing style
10     Callback<void(const Callback<void(T)> &returnz)> get;
11     Callback<void(T value)> set;
12 };
13
14 // implementation
15
16 template<class Self, class T = Self>
17 struct PropertyImpl {
18     static void Export(const Self &self, const Callback<void(T)> &returnz) {
19         returnz(self);
20     }
21
22     static void Import(Self &self, T value) {
23         self = value;
24     }
25 };
26
27 namespace detail {
28
29     template<class I>
30     using propertyimpl_self = typename std::remove_reference<get_argument<decltype(&I::Import), 0>>::type;
31
32     template<class I>
33     using propertyimpl_other = get_argument<decltype(&I::Import), 1>;
34
35     template<class I>
36     using propertyimpl_other_free = get_argument<decltype(&I::Import), 0>;
37
38 }
39
40 // adaptor
41
42 template<
43         class Self,
44         class T = Self,
45         class I = PropertyImpl<Self, T>
46 >
47 struct PropertyAdaptor {
48     using Type = Self;
49     using Other = T;
50
51     using Get = ConstReferenceCaller<Self, void(const Callback<void(T)> &), I::Export>;
52     using Set = ReferenceCaller<Self, void(T), I::Import>;
53 };
54
55 template<
56         class T,
57         class I
58 >
59 struct PropertyAdaptorFree {
60     using Other = T;
61
62     using Get = FreeCaller<void(const Callback<void(T)> &), I::Export>;
63     using Set = FreeCaller<void(T), I::Import>;
64 };
65
66 // explicit full
67
68 template<class A>
69 Property<typename A::Other> make_property(typename A::Type &self) {
70     return {typename A::Get(self), typename A::Set(self)};
71 }
72
73 template<class A>
74 Property<typename A::Other> make_property() {
75     return {typename A::Get(), typename A::Set()};
76 }
77
78 // explicit impl
79
80 template<class I, class Self = detail::propertyimpl_self<I>, class T = detail::propertyimpl_other<I>>
81 using property_impl = PropertyAdaptor<Self, T, I>;
82
83 template<class I, class Self, class T = detail::propertyimpl_other<I>>
84 Property<T> make_property(Self &self) {
85     return make_property<property_impl<I>>(self);
86 }
87
88 template<class I, class T = detail::propertyimpl_other_free<I>>
89 using property_impl_free = PropertyAdaptorFree<T, I>;
90
91 template<class I, class T = detail::propertyimpl_other_free<I>>
92 Property<T> make_property() {
93     return make_property<property_impl_free<I>>();
94 }
95
96 // implicit
97
98 template<class Self, class T = Self>
99 Property<T> make_property(Self &self) {
100     return make_property<PropertyAdaptor<Self, T>>(self);
101 }
102
103 // chain
104
105 template<class I_Outer, class I_Inner>
106 Property<detail::propertyimpl_other<I_Outer>> make_property_chain(detail::propertyimpl_self<I_Inner> &it) {
107     using DST = detail::propertyimpl_other<I_Outer>;
108     using SRC = detail::propertyimpl_self<I_Outer>;
109     using X = detail::propertyimpl_self<I_Inner>;
110
111     using A = property_impl<I_Inner>;
112     struct I {
113         static void ExportThunk(const Callback<void(DST)> &self, SRC value) {
114             PropertyImpl<SRC, DST>::Export(value, self);
115         }
116
117         static void Export(const X &self, const Callback<void(DST)> &returnz) {
118             A::Get::thunk_(self, ConstReferenceCaller<Callback<void(DST)>, void(SRC), ExportThunk>(returnz));
119         }
120
121         static void Import(X &self, DST value) {
122             SRC out;
123             PropertyImpl<SRC, DST>::Import(out, value);
124             A::Set::thunk_(self, out);
125         }
126     };
127     return make_property<PropertyAdaptor<X, DST, I>>(it);
128 }
129
130 template<class I_Outer, class I_Inner>
131 Property<detail::propertyimpl_other<I_Outer>> make_property_chain() {
132     using DST = detail::propertyimpl_other<I_Outer>;
133     using SRC = detail::propertyimpl_self<I_Outer>;
134
135     using A = property_impl_free<I_Inner>;
136     struct I {
137         static void ExportThunk(const Callback<void(DST)> &self, SRC value) {
138             PropertyImpl<SRC, DST>::Export(value, self);
139         }
140
141         static void Export(const Callback<void(DST)> &returnz) {
142             A::Get::thunk_(nullptr, ConstReferenceCaller<Callback<void(DST)>, void(SRC), ExportThunk>(returnz));
143         }
144
145         static void Import(DST value) {
146             SRC out;
147             PropertyImpl<SRC, DST>::Import(out, value);
148             A::Set::thunk_(nullptr, out);
149         }
150     };
151     return make_property<PropertyAdaptorFree<DST, I>>();
152 }
153
154 // specializations
155
156 template<>
157 struct PropertyImpl<CopiedString, const char *> {
158     static void Export(const CopiedString &self, const Callback<void(const char *)> &returnz) {
159         returnz(self.c_str());
160     }
161
162     static void Import(CopiedString &self, const char *value) {
163         self = value;
164     }
165 };
166
167 #endif