type_conversions.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // This file is distributed under the BSD License.
  2. // See "license.txt" for details.
  3. // Copyright 2009-2012, Jonathan Turner ([email protected])
  4. // Copyright 2009-2017, Jason Turner ([email protected])
  5. // http://www.chaiscript.com
  6. // This is an open source non-commercial project. Dear PVS-Studio, please check it.
  7. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  8. #ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
  9. #define CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
  10. #include <atomic>
  11. #include <memory>
  12. #include <set>
  13. #include <stdexcept>
  14. #include <string>
  15. #include <type_traits>
  16. #include <typeinfo>
  17. #include "../chaiscript_threading.hpp"
  18. #include "bad_boxed_cast.hpp"
  19. #include "boxed_cast_helper.hpp"
  20. #include "boxed_value.hpp"
  21. #include "type_info.hpp"
  22. namespace chaiscript
  23. {
  24. namespace exception
  25. {
  26. class bad_boxed_dynamic_cast : public bad_boxed_cast
  27. {
  28. public:
  29. bad_boxed_dynamic_cast(const Type_Info &t_from, const std::type_info &t_to,
  30. const std::string &t_what) noexcept
  31. : bad_boxed_cast(t_from, t_to, t_what)
  32. {
  33. }
  34. bad_boxed_dynamic_cast(const Type_Info &t_from, const std::type_info &t_to) noexcept
  35. : bad_boxed_cast(t_from, t_to)
  36. {
  37. }
  38. explicit bad_boxed_dynamic_cast(const std::string &w) noexcept
  39. : bad_boxed_cast(w)
  40. {
  41. }
  42. bad_boxed_dynamic_cast(const bad_boxed_dynamic_cast &) = default;
  43. ~bad_boxed_dynamic_cast() noexcept override = default;
  44. };
  45. class bad_boxed_type_cast : public bad_boxed_cast
  46. {
  47. public:
  48. bad_boxed_type_cast(const Type_Info &t_from, const std::type_info &t_to,
  49. const std::string &t_what) noexcept
  50. : bad_boxed_cast(t_from, t_to, t_what)
  51. {
  52. }
  53. bad_boxed_type_cast(const Type_Info &t_from, const std::type_info &t_to) noexcept
  54. : bad_boxed_cast(t_from, t_to)
  55. {
  56. }
  57. explicit bad_boxed_type_cast(const std::string &w) noexcept
  58. : bad_boxed_cast(w)
  59. {
  60. }
  61. bad_boxed_type_cast(const bad_boxed_type_cast &) = default;
  62. ~bad_boxed_type_cast() noexcept override = default;
  63. };
  64. }
  65. namespace detail
  66. {
  67. class Type_Conversion_Base
  68. {
  69. public:
  70. virtual Boxed_Value convert(const Boxed_Value &from) const = 0;
  71. virtual Boxed_Value convert_down(const Boxed_Value &to) const = 0;
  72. const Type_Info &to() const
  73. {
  74. return m_to;
  75. }
  76. const Type_Info &from() const
  77. {
  78. return m_from;
  79. }
  80. virtual bool bidir() const
  81. {
  82. return true;
  83. }
  84. virtual ~Type_Conversion_Base() = default;
  85. protected:
  86. Type_Conversion_Base(Type_Info t_to, Type_Info t_from)
  87. : m_to(std::move(t_to)), m_from(std::move(t_from))
  88. {
  89. }
  90. private:
  91. const Type_Info m_to;
  92. const Type_Info m_from;
  93. };
  94. template<typename From, typename To>
  95. class Static_Caster
  96. {
  97. public:
  98. static Boxed_Value cast(const Boxed_Value &t_from)
  99. {
  100. if (t_from.get_type_info().bare_equal(chaiscript::user_type<From>()))
  101. {
  102. if (t_from.is_pointer())
  103. {
  104. // Dynamic cast out the contained boxed value, which we know is the type we want
  105. if (t_from.is_const())
  106. {
  107. return Boxed_Value(
  108. [&](){
  109. if (auto data = std::static_pointer_cast<const To>(detail::Cast_Helper<std::shared_ptr<const From> >::cast(t_from, nullptr)))
  110. {
  111. return data;
  112. } else {
  113. throw std::bad_cast();
  114. }
  115. }()
  116. );
  117. } else {
  118. return Boxed_Value(
  119. [&](){
  120. if (auto data = std::static_pointer_cast<To>(detail::Cast_Helper<std::shared_ptr<From> >::cast(t_from, nullptr)))
  121. {
  122. return data;
  123. } else {
  124. throw std::bad_cast();
  125. }
  126. }()
  127. );
  128. }
  129. } else {
  130. // Pull the reference out of the contained boxed value, which we know is the type we want
  131. if (t_from.is_const())
  132. {
  133. const From &d = detail::Cast_Helper<const From &>::cast(t_from, nullptr);
  134. const To &data = static_cast<const To &>(d);
  135. return Boxed_Value(std::cref(data));
  136. } else {
  137. From &d = detail::Cast_Helper<From &>::cast(t_from, nullptr);
  138. To &data = static_cast<To &>(d);
  139. return Boxed_Value(std::ref(data));
  140. }
  141. }
  142. } else {
  143. throw chaiscript::exception::bad_boxed_dynamic_cast(t_from.get_type_info(), typeid(To), "Unknown dynamic_cast_conversion");
  144. }
  145. }
  146. };
  147. template<typename From, typename To>
  148. class Dynamic_Caster
  149. {
  150. public:
  151. static Boxed_Value cast(const Boxed_Value &t_from)
  152. {
  153. if (t_from.get_type_info().bare_equal(chaiscript::user_type<From>()))
  154. {
  155. if (t_from.is_pointer())
  156. {
  157. // Dynamic cast out the contained boxed value, which we know is the type we want
  158. if (t_from.is_const())
  159. {
  160. return Boxed_Value(
  161. [&](){
  162. if (auto data = std::dynamic_pointer_cast<const To>(detail::Cast_Helper<std::shared_ptr<const From> >::cast(t_from, nullptr)))
  163. {
  164. return data;
  165. } else {
  166. throw std::bad_cast();
  167. }
  168. }()
  169. );
  170. } else {
  171. return Boxed_Value(
  172. [&](){
  173. if (auto data = std::dynamic_pointer_cast<To>(detail::Cast_Helper<std::shared_ptr<From> >::cast(t_from, nullptr)))
  174. {
  175. return data;
  176. } else {
  177. #ifdef CHAISCRIPT_LIBCPP
  178. /// \todo fix this someday after libc++ is fixed.
  179. if (std::string(typeid(To).name()).find("Assignable_Proxy_Function") != std::string::npos) {
  180. auto from = detail::Cast_Helper<std::shared_ptr<From> >::cast(t_from, nullptr);
  181. if (std::string(typeid(*from).name()).find("Assignable_Proxy_Function_Impl") != std::string::npos) {
  182. return std::static_pointer_cast<To>(from);
  183. }
  184. }
  185. #endif
  186. throw std::bad_cast();
  187. }
  188. }()
  189. );
  190. }
  191. } else {
  192. // Pull the reference out of the contained boxed value, which we know is the type we want
  193. if (t_from.is_const())
  194. {
  195. const From &d = detail::Cast_Helper<const From &>::cast(t_from, nullptr);
  196. const To &data = dynamic_cast<const To &>(d);
  197. return Boxed_Value(std::cref(data));
  198. } else {
  199. From &d = detail::Cast_Helper<From &>::cast(t_from, nullptr);
  200. To &data = dynamic_cast<To &>(d);
  201. return Boxed_Value(std::ref(data));
  202. }
  203. }
  204. } else {
  205. throw chaiscript::exception::bad_boxed_dynamic_cast(t_from.get_type_info(), typeid(To), "Unknown dynamic_cast_conversion");
  206. }
  207. }
  208. };
  209. template<typename Base, typename Derived>
  210. class Dynamic_Conversion_Impl : public Type_Conversion_Base
  211. {
  212. public:
  213. Dynamic_Conversion_Impl()
  214. : Type_Conversion_Base(chaiscript::user_type<Base>(), chaiscript::user_type<Derived>())
  215. {
  216. }
  217. Boxed_Value convert_down(const Boxed_Value &t_base) const override
  218. {
  219. return Dynamic_Caster<Base, Derived>::cast(t_base);
  220. }
  221. Boxed_Value convert(const Boxed_Value &t_derived) const override
  222. {
  223. return Static_Caster<Derived, Base>::cast(t_derived);
  224. }
  225. };
  226. template<typename Base, typename Derived>
  227. class Static_Conversion_Impl : public Type_Conversion_Base
  228. {
  229. public:
  230. Static_Conversion_Impl()
  231. : Type_Conversion_Base(chaiscript::user_type<Base>(), chaiscript::user_type<Derived>())
  232. {
  233. }
  234. Boxed_Value convert_down(const Boxed_Value &t_base) const override
  235. {
  236. throw chaiscript::exception::bad_boxed_dynamic_cast(t_base.get_type_info(), typeid(Derived),
  237. "Unable to cast down inheritance hierarchy with non-polymorphic types");
  238. }
  239. bool bidir() const override
  240. {
  241. return false;
  242. }
  243. Boxed_Value convert(const Boxed_Value &t_derived) const override
  244. {
  245. return Static_Caster<Derived, Base>::cast(t_derived);
  246. }
  247. };
  248. template<typename Callable>
  249. class Type_Conversion_Impl : public Type_Conversion_Base
  250. {
  251. public:
  252. Type_Conversion_Impl(Type_Info t_from, Type_Info t_to, Callable t_func)
  253. : Type_Conversion_Base(t_to, t_from),
  254. m_func(std::move(t_func))
  255. {
  256. }
  257. Boxed_Value convert_down(const Boxed_Value &) const override
  258. {
  259. throw chaiscript::exception::bad_boxed_type_cast("No conversion exists");
  260. }
  261. Boxed_Value convert(const Boxed_Value &t_from) const override
  262. {
  263. /// \todo better handling of errors from the conversion function
  264. return m_func(t_from);
  265. }
  266. bool bidir() const override
  267. {
  268. return false;
  269. }
  270. private:
  271. Callable m_func;
  272. };
  273. }
  274. class Type_Conversions
  275. {
  276. public:
  277. struct Conversion_Saves
  278. {
  279. bool enabled = false;
  280. std::vector<Boxed_Value> saves;
  281. };
  282. struct Less_Than
  283. {
  284. bool operator()(const std::type_info *t_lhs, const std::type_info *t_rhs) const
  285. {
  286. return *t_lhs != *t_rhs && t_lhs->before(*t_rhs);
  287. }
  288. };
  289. Type_Conversions()
  290. : m_mutex(),
  291. m_conversions(),
  292. m_convertableTypes(),
  293. m_num_types(0)
  294. {
  295. }
  296. Type_Conversions(const Type_Conversions &t_other) = delete;
  297. Type_Conversions(Type_Conversions &&) = default;
  298. Type_Conversions &operator=(const Type_Conversions &) = delete;
  299. Type_Conversions &operator=(Type_Conversions &&) = default;
  300. const std::set<const std::type_info *, Less_Than> &thread_cache() const
  301. {
  302. auto &cache = *m_thread_cache;
  303. if (cache.size() != m_num_types)
  304. {
  305. chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
  306. cache = m_convertableTypes;
  307. }
  308. return cache;
  309. }
  310. void add_conversion(const std::shared_ptr<detail::Type_Conversion_Base> &conversion)
  311. {
  312. chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
  313. /// \todo error if a conversion already exists
  314. m_conversions.insert(conversion);
  315. m_convertableTypes.insert({conversion->to().bare_type_info(), conversion->from().bare_type_info()});
  316. m_num_types = m_convertableTypes.size();
  317. }
  318. template<typename T>
  319. bool convertable_type() const
  320. {
  321. return thread_cache().count(user_type<T>().bare_type_info()) != 0;
  322. }
  323. template<typename To, typename From>
  324. bool converts() const
  325. {
  326. return converts(user_type<To>(), user_type<From>());
  327. }
  328. bool converts(const Type_Info &to, const Type_Info &from) const
  329. {
  330. const auto &types = thread_cache();
  331. if (types.count(to.bare_type_info()) != 0 && types.count(from.bare_type_info()) != 0)
  332. {
  333. return has_conversion(to, from);
  334. } else {
  335. return false;
  336. }
  337. }
  338. template<typename To>
  339. Boxed_Value boxed_type_conversion(Conversion_Saves &t_saves, const Boxed_Value &from) const
  340. {
  341. return boxed_type_conversion(user_type<To>(), t_saves, from);
  342. }
  343. template<typename From>
  344. Boxed_Value boxed_type_down_conversion(Conversion_Saves &t_saves, const Boxed_Value &to) const
  345. {
  346. return boxed_type_down_conversion(user_type<From>(), t_saves, to);
  347. }
  348. Boxed_Value boxed_type_conversion(const Type_Info &to, Conversion_Saves &t_saves, const Boxed_Value &from) const
  349. {
  350. try {
  351. Boxed_Value ret = get_conversion(to, from.get_type_info())->convert(from);
  352. if (t_saves.enabled) { t_saves.saves.push_back(ret); }
  353. return ret;
  354. } catch (const std::out_of_range &) {
  355. throw exception::bad_boxed_dynamic_cast(from.get_type_info(), *to.bare_type_info(), "No known conversion");
  356. } catch (const std::bad_cast &) {
  357. throw exception::bad_boxed_dynamic_cast(from.get_type_info(), *to.bare_type_info(), "Unable to perform dynamic_cast operation");
  358. }
  359. }
  360. Boxed_Value boxed_type_down_conversion(const Type_Info &from, Conversion_Saves &t_saves, const Boxed_Value &to) const
  361. {
  362. try {
  363. Boxed_Value ret = get_conversion(to.get_type_info(), from)->convert_down(to);
  364. if (t_saves.enabled) { t_saves.saves.push_back(ret); }
  365. return ret;
  366. } catch (const std::out_of_range &) {
  367. throw exception::bad_boxed_dynamic_cast(to.get_type_info(), *from.bare_type_info(), "No known conversion");
  368. } catch (const std::bad_cast &) {
  369. throw exception::bad_boxed_dynamic_cast(to.get_type_info(), *from.bare_type_info(), "Unable to perform dynamic_cast operation");
  370. }
  371. }
  372. static void enable_conversion_saves(Conversion_Saves &t_saves, bool t_val)
  373. {
  374. t_saves.enabled = t_val;
  375. }
  376. std::vector<Boxed_Value> take_saves(Conversion_Saves &t_saves)
  377. {
  378. std::vector<Boxed_Value> ret;
  379. std::swap(ret, t_saves.saves);
  380. return ret;
  381. }
  382. bool has_conversion(const Type_Info &to, const Type_Info &from) const
  383. {
  384. chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
  385. return find_bidir(to, from) != m_conversions.end();
  386. }
  387. std::shared_ptr<detail::Type_Conversion_Base> get_conversion(const Type_Info &to, const Type_Info &from) const
  388. {
  389. chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
  390. const auto itr = find(to, from);
  391. if (itr != m_conversions.end())
  392. {
  393. return *itr;
  394. } else {
  395. throw std::out_of_range("No such conversion exists from " + from.bare_name() + " to " + to.bare_name());
  396. }
  397. }
  398. Conversion_Saves &conversion_saves() const {
  399. return *m_conversion_saves;
  400. }
  401. private:
  402. std::set<std::shared_ptr<detail::Type_Conversion_Base> >::const_iterator find_bidir(
  403. const Type_Info &to, const Type_Info &from) const
  404. {
  405. return std::find_if(m_conversions.begin(), m_conversions.end(),
  406. [&to, &from](const std::shared_ptr<detail::Type_Conversion_Base> &conversion) -> bool
  407. {
  408. return (conversion->to().bare_equal(to) && conversion->from().bare_equal(from))
  409. || (conversion->bidir() && conversion->from().bare_equal(to) && conversion->to().bare_equal(from));
  410. }
  411. );
  412. }
  413. std::set<std::shared_ptr<detail::Type_Conversion_Base> >::const_iterator find(
  414. const Type_Info &to, const Type_Info &from) const
  415. {
  416. return std::find_if(m_conversions.begin(), m_conversions.end(),
  417. [&to, &from](const std::shared_ptr<detail::Type_Conversion_Base> &conversion)
  418. {
  419. return conversion->to().bare_equal(to) && conversion->from().bare_equal(from);
  420. }
  421. );
  422. }
  423. std::set<std::shared_ptr<detail::Type_Conversion_Base>> get_conversions() const
  424. {
  425. chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
  426. return m_conversions;
  427. }
  428. mutable chaiscript::detail::threading::shared_mutex m_mutex;
  429. std::set<std::shared_ptr<detail::Type_Conversion_Base>> m_conversions;
  430. std::set<const std::type_info *, Less_Than> m_convertableTypes;
  431. std::atomic_size_t m_num_types;
  432. mutable chaiscript::detail::threading::Thread_Storage<std::set<const std::type_info *, Less_Than>> m_thread_cache;
  433. mutable chaiscript::detail::threading::Thread_Storage<Conversion_Saves> m_conversion_saves;
  434. };
  435. class Type_Conversions_State
  436. {
  437. public:
  438. Type_Conversions_State(const Type_Conversions &t_conversions,
  439. Type_Conversions::Conversion_Saves &t_saves)
  440. : m_conversions(t_conversions),
  441. m_saves(t_saves)
  442. {
  443. }
  444. const Type_Conversions *operator->() const {
  445. return &m_conversions.get();
  446. }
  447. const Type_Conversions *get() const {
  448. return &m_conversions.get();
  449. }
  450. Type_Conversions::Conversion_Saves &saves() const {
  451. return m_saves;
  452. }
  453. private:
  454. std::reference_wrapper<const Type_Conversions> m_conversions;
  455. std::reference_wrapper<Type_Conversions::Conversion_Saves> m_saves;
  456. };
  457. typedef std::shared_ptr<chaiscript::detail::Type_Conversion_Base> Type_Conversion;
  458. /// \brief Used to register a to / parent class relationship with ChaiScript. Necessary if you
  459. /// want automatic conversions up your inheritance hierarchy.
  460. ///
  461. /// Create a new to class registration for applying to a module or to the ChaiScript engine
  462. /// Currently, due to limitations in module loading on Windows, and for the sake of portability,
  463. /// if you have a type that is introduced in a loadable module and is used by multiple modules
  464. /// (through a tertiary dll that is shared between the modules, static linking the new type
  465. /// into both loadable modules would not be portable), you need to register the type
  466. /// relationship in all modules that use the newly added type in a polymorphic way.
  467. ///
  468. /// Example:
  469. /// \code
  470. /// class Base
  471. /// {};
  472. /// class Derived : public Base
  473. /// {};
  474. ///
  475. /// chaiscript::ChaiScript chai;
  476. /// chai.add(chaiscript::to_class<Base, Derived>());
  477. /// \endcode
  478. ///
  479. template<typename Base, typename Derived>
  480. Type_Conversion base_class(typename std::enable_if<std::is_polymorphic<Base>::value && std::is_polymorphic<Derived>::value>::type* = nullptr)
  481. {
  482. //Can only be used with related polymorphic types
  483. //may be expanded some day to support conversions other than child -> parent
  484. static_assert(std::is_base_of<Base,Derived>::value, "Classes are not related by inheritance");
  485. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Dynamic_Conversion_Impl<Base, Derived>>();
  486. }
  487. template<typename Base, typename Derived>
  488. Type_Conversion base_class(typename std::enable_if<!std::is_polymorphic<Base>::value || !std::is_polymorphic<Derived>::value>::type* = nullptr)
  489. {
  490. //Can only be used with related polymorphic types
  491. //may be expanded some day to support conversions other than child -> parent
  492. static_assert(std::is_base_of<Base,Derived>::value, "Classes are not related by inheritance");
  493. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Static_Conversion_Impl<Base, Derived>>();
  494. }
  495. template<typename Callable>
  496. Type_Conversion type_conversion(const Type_Info &t_from, const Type_Info &t_to,
  497. const Callable &t_func)
  498. {
  499. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<Callable>>(t_from, t_to, t_func);
  500. }
  501. template<typename From, typename To, typename Callable>
  502. Type_Conversion type_conversion(const Callable &t_function)
  503. {
  504. auto func = [t_function](const Boxed_Value &t_bv) -> Boxed_Value {
  505. // not even attempting to call boxed_cast so that we don't get caught in some call recursion
  506. return chaiscript::Boxed_Value(t_function(detail::Cast_Helper<const From &>::cast(t_bv, nullptr)));
  507. };
  508. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<From>(), user_type<To>(), func);
  509. }
  510. template<typename From, typename To>
  511. Type_Conversion type_conversion()
  512. {
  513. static_assert(std::is_convertible<From, To>::value, "Types are not automatically convertible");
  514. auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
  515. // not even attempting to call boxed_cast so that we don't get caught in some call recursion
  516. return chaiscript::Boxed_Value(To(detail::Cast_Helper<From>::cast(t_bv, nullptr)));
  517. };
  518. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<From>(), user_type<To>(), func);
  519. }
  520. template<typename To>
  521. Type_Conversion vector_conversion()
  522. {
  523. auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
  524. const std::vector<Boxed_Value> &from_vec = detail::Cast_Helper<const std::vector<Boxed_Value> &>::cast(t_bv, nullptr);
  525. To vec;
  526. vec.reserve(from_vec.size());
  527. for (const Boxed_Value &bv : from_vec) {
  528. vec.push_back(detail::Cast_Helper<typename To::value_type>::cast(bv, nullptr));
  529. }
  530. return Boxed_Value(std::move(vec));
  531. };
  532. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::vector<Boxed_Value>>(), user_type<To>(), func);
  533. }
  534. template<typename To>
  535. Type_Conversion map_conversion()
  536. {
  537. auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
  538. const std::map<std::string, Boxed_Value> &from_map = detail::Cast_Helper<const std::map<std::string, Boxed_Value> &>::cast(t_bv, nullptr);
  539. To map;
  540. for (const std::pair<std::string, Boxed_Value> &p : from_map) {
  541. map.insert(std::make_pair(p.first, detail::Cast_Helper<typename To::mapped_type>::cast(p.second, nullptr)));
  542. }
  543. return Boxed_Value(std::move(map));
  544. };
  545. return chaiscript::make_shared<detail::Type_Conversion_Base, detail::Type_Conversion_Impl<decltype(func)>>(user_type<std::map<std::string, Boxed_Value>>(), user_type<To>(), func);
  546. }
  547. }
  548. #endif