6 #pragma clang diagnostic push
7 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
8 #pragma clang diagnostic ignored "-Wold-style-cast"
9 #include <boost/describe.hpp>
10 #include <boost/mp11/algorithm.hpp>
11 #pragma clang diagnostic pop
19 #include <unordered_map>
38 class Bases = boost::describe::describe_bases<T, boost::describe::mod_any_access>,
39 class Members = boost::describe::describe_members<T, boost::describe::mod_any_access>,
40 class Enable = std::enable_if_t<!std::is_union<T>::value>>
65 throw std::runtime_error(
"Cannot convert long long to val");
69 return Nui::val::undefined();
71 template <
typename... Ts>
74 template <
typename T,
class Members>
76 void convertFromValObjImpl(
Nui::val const&
val, T& obj);
78 template <
typename T,
class Bases,
class Members>
82 template <
typename T,
class Members>
83 requires(!std::is_union_v<T> && !boost::describe::has_describe_bases<T>::value)
101 template <
typename T>
105 throw std::invalid_argument(
"Cannot convert from val to long long");
108 template <
typename T,
class Bases,
class Members,
class Enable>
111 if (
val.typeOf().as<std::string>() !=
"object")
112 val = Nui::val::object();
114 boost::mp11::mp_for_each<Bases>([&](
auto&& base) {
115 using type =
typename std::decay_t<decltype(base)>::type;
119 boost::mp11::mp_for_each<Members>([&](
auto&& memAccessor) {
125 class Bases = boost::describe::describe_bases<T, boost::describe::mod_any_access>,
126 class Members = boost::describe::describe_members<T, boost::describe::mod_any_access>,
127 class Enable = std::enable_if_t<!std::is_union<T>::value>>
135 template <
typename T>
138 return option ?
convertToVal(*option) : Nui::val::undefined();
140 template <
typename T>
160 return Nui::val{std::string{value}};
162 template <
typename T>
165 Nui::val result = Nui::val::array();
166 for (
auto const& element : vector)
170 template <
typename T>
175 template <
typename T>
178 Nui::val result = Nui::val::object();
179 for (
auto const& [key, value] : map)
183 template <
typename T>
186 Nui::val result = Nui::val::object();
187 for (
auto const& [key, value] : map)
191 template <
typename T>
197 return Nui::val::null();
199 template <
typename T>
205 return Nui::val::null();
207 template <
typename... Ts>
217 template <
typename T,
class Members = boost::describe::describe_members<T, boost::describe::mod_any_access>>
218 requires(!std::is_union_v<T> && !boost::describe::has_describe_bases<T>::value)
221 convertFromValObjImpl<T, Members>(
val, obj);
226 class Bases = boost::describe::describe_bases<T, boost::describe::mod_any_access>,
227 class Members = boost::describe::describe_members<T, boost::describe::mod_any_access>>
228 requires(!std::is_union_v<T> && boost::describe::has_describe_bases<T>::value)
231 boost::mp11::mp_for_each<Bases>([&](
auto&& base) {
232 using type =
typename std::decay_t<decltype(base)>::type;
235 convertFromValObjImpl<T, Members>(
val, obj);
238 template <
typename T,
class Members = boost::describe::describe_members<T, boost::describe::mod_any_access>>
240 void convertFromValObjImpl(
Nui::val const&
val, T& obj)
242 boost::mp11::mp_for_each<Members>([&](
auto&& memAccessor) {
243 if (
val.hasOwnProperty(memAccessor.name))
245 if constexpr (!Detail::IsOptional<decltype(obj.*memAccessor.pointer)>::value)
247 if (val[memAccessor.name].isNull() || val[memAccessor.name].isUndefined())
248 Nui::val::global(
"console").call<void>(
250 std::string{
"Expected member "} + memAccessor.name +
" to be defined and non null");
252 convertFromVal(val[memAccessor.name], obj.*memAccessor.pointer);
260 template <
typename T>
268 str =
val.as<std::string>();
270 template <
typename T>
273 if (
val.isNull() ||
val.isUndefined())
274 option = std::nullopt;
284 value =
val.as<std::string>();
290 template <
typename T>
294 const auto length =
val[
"length"].as<std::size_t>();
295 vector.reserve(length);
296 for (std::size_t i = 0; i < length; ++i)
300 vector.push_back(std::move(value));
303 template <
typename T>
307 vector = emscripten::convertJSArrayToNumberVector<T>(
val);
309 template <
typename T>
312 auto proxy = observed.
modify();
315 template <
typename T>
319 const auto keys = Nui::val::global(
"Object").call<
Nui::val>(
"keys",
val);
320 const auto length = keys[
"length"].as<std::size_t>();
321 for (std::size_t i = 0; i < length; ++i)
323 const auto key = keys[i].as<std::string>();
326 map.emplace(key, std::move(value));
ModificationProxy modify()
Can be used to make mutations to the underlying class that get commited when the returned proxy is de...
Definition: observed_value.hpp:293
ContainedT const & value() const
Definition: observed_value.hpp:313
Definition: observed_value.hpp:1202
Definition: file_dialog.hpp:6
requires(!std::is_union_v< T >) void convertFromValObjImpl(Nui requires(!std::is_union_v< T >) void convertFromVal(Nui requires(!std::is_union_v< T > &&!boost::describe::has_describe_bases< T >::value) void convertFromVal(Nui requires Fundamental< T > void convertFromVal(Nui::val const &val, T &value)
Definition: val_conversion.hpp:262
requires(IsObservedLike< ObservedValues > &&...) ObservedValueCombinator< std
Definition: observed_value_combinator.hpp:191
void convertToVal(Nui::val &val, T const &obj)
Definition: val_conversion.hpp:109
requires(!std::is_union_v< T >) void convertFromValObjImpl(Nui
Definition: val_conversion.hpp:239
requires Fundamental< T > void convertFromVal(Nui::val const &val, T &value)
Definition: val_conversion.hpp:262
emscripten::val val
Definition: val.hpp:5
Definition: val_conversion.hpp:30