1 #ifndef INCLUDED_IMPORTEXPORT_H
2 #define INCLUDED_IMPORTEXPORT_H
4 #include "generic/callback.h"
5 #include "string/string.h"
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;
16 template<class Self, class T = Self>
18 static void Export(const Self &self, const Callback<void(T)> &returnz) {
22 static void Import(Self &self, T value) {
30 using propertyimpl_self = typename std::remove_reference<get_argument<decltype(&I::Import), 0>>::type;
33 using propertyimpl_other = get_argument<decltype(&I::Import), 1>;
36 using propertyimpl_other_free = get_argument<decltype(&I::Import), 0>;
45 class I = PropertyImpl<Self, T>
47 struct PropertyAdaptor {
51 using Get = ConstReferenceCaller<Self, void(const Callback<void(T)> &), I::Export>;
52 using Set = ReferenceCaller<Self, void(T), I::Import>;
59 struct PropertyAdaptorFree {
62 using Get = FreeCaller<void(const Callback<void(T)> &), I::Export>;
63 using Set = FreeCaller<void(T), I::Import>;
69 Property<typename A::Other> make_property(typename A::Type &self) {
70 return {typename A::Get(self), typename A::Set(self)};
74 Property<typename A::Other> make_property() {
75 return {typename A::Get(), typename A::Set()};
80 template<class I, class Self = detail::propertyimpl_self<I>, class T = detail::propertyimpl_other<I>>
81 using property_impl = PropertyAdaptor<Self, T, I>;
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);
88 template<class I, class T = detail::propertyimpl_other_free<I>>
89 using property_impl_free = PropertyAdaptorFree<T, I>;
91 template<class I, class T = detail::propertyimpl_other_free<I>>
92 Property<T> make_property() {
93 return make_property<property_impl_free<I>>();
98 template<class Self, class T = Self>
99 Property<T> make_property(Self &self) {
100 return make_property<PropertyAdaptor<Self, T>>(self);
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>;
111 using A = property_impl<I_Inner>;
113 static void ExportThunk(const Callback<void(DST)> &self, SRC value) {
114 PropertyImpl<SRC, DST>::Export(value, self);
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));
121 static void Import(X &self, DST value) {
123 PropertyImpl<SRC, DST>::Import(out, value);
124 A::Set::thunk_(self, out);
127 return make_property<PropertyAdaptor<X, DST, I>>(it);
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>;
135 using A = property_impl_free<I_Inner>;
137 static void ExportThunk(const Callback<void(DST)> &self, SRC value) {
138 PropertyImpl<SRC, DST>::Export(value, self);
141 static void Export(const Callback<void(DST)> &returnz) {
142 A::Get::thunk_(nullptr, ConstReferenceCaller<Callback<void(DST)>, void(SRC), ExportThunk>(returnz));
145 static void Import(DST value) {
147 PropertyImpl<SRC, DST>::Import(out, value);
148 A::Set::thunk_(nullptr, out);
151 return make_property<PropertyAdaptorFree<DST, I>>();
157 struct PropertyImpl<CopiedString, const char *> {
158 static void Export(const CopiedString &self, const Callback<void(const char *)> &returnz) {
159 returnz(self.c_str());
162 static void Import(CopiedString &self, const char *value) {