Main class for the dispatchkit. Handles management of the object stack, functions and registered types.
| 435 | /// Main class for the dispatchkit. Handles management |
| 436 | /// of the object stack, functions and registered types. |
| 437 | class Dispatch_Engine |
| 438 | { |
| 439 | |
| 440 | public: |
| 441 | typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map; |
| 442 | typedef std::vector<std::pair<std::string, Boxed_Value>> Scope; |
| 443 | typedef Stack_Holder::StackData StackData; |
| 444 | |
| 445 | struct State |
| 446 | { |
| 447 | std::vector<std::pair<std::string, std::shared_ptr<std::vector<Proxy_Function>>>> m_functions; |
| 448 | std::vector<std::pair<std::string, Proxy_Function>> m_function_objects; |
| 449 | std::vector<std::pair<std::string, Boxed_Value>> m_boxed_functions; |
| 450 | std::map<std::string, Boxed_Value> m_global_objects; |
| 451 | Type_Name_Map m_types; |
| 452 | }; |
| 453 | |
| 454 | explicit Dispatch_Engine(chaiscript::parser::ChaiScript_Parser_Base &parser) |
| 455 | : m_stack_holder(), |
| 456 | m_parser(parser) |
| 457 | { |
| 458 | } |
| 459 | |
| 460 | /// \brief casts an object while applying any Dynamic_Conversion available |
| 461 | template<typename Type> |
| 462 | decltype(auto) boxed_cast(const Boxed_Value &bv) const |
| 463 | { |
| 464 | Type_Conversions_State state(m_conversions, m_conversions.conversion_saves()); |
| 465 | return(chaiscript::boxed_cast<Type>(bv, &state)); |
| 466 | } |
| 467 | |
| 468 | /// Add a new conversion for upcasting to a base class |
| 469 | void add(const Type_Conversion &d) |
| 470 | { |
| 471 | m_conversions.add_conversion(d); |
| 472 | } |
| 473 | |
| 474 | /// Add a new named Proxy_Function to the system |
| 475 | void add(const Proxy_Function &f, const std::string &name) |
| 476 | { |
| 477 | add_function(f, name); |
| 478 | } |
| 479 | |
| 480 | /// Set the value of an object, by name. If the object |
| 481 | /// is not available in the current scope it is created |
| 482 | void add(Boxed_Value obj, const std::string &name) |
| 483 | { |
| 484 | auto &stack = get_stack_data(); |
| 485 | |
| 486 | for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem) |
| 487 | { |
| 488 | auto itr = std::find_if(stack_elem->begin(), stack_elem->end(), |
| 489 | [&](const std::pair<std::string, Boxed_Value> &o) { |
| 490 | return o.first == name; |
| 491 | }); |
| 492 | |
| 493 | if (itr != stack_elem->end()) |
| 494 | { |
nothing calls this directly
no test coverage detected