Basic support for std::vector and related fixes
This commit is contained in:
parent
4c317f5861
commit
af221b611d
@ -4,32 +4,36 @@
|
||||
#include "boxedcpp.hpp"
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_reversible_container(BoxedCPP_System &system)
|
||||
void bootstrap_reversible_container(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_random_access_container(BoxedCPP_System &system)
|
||||
void bootstrap_random_access_container(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_reversible_container<ContainerType>(system);
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "[]");
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "at");
|
||||
}
|
||||
|
||||
template<typename Assignable>
|
||||
void bootstrap_assignable(BoxedCPP_System &system)
|
||||
void bootstrap_assignable(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
/*
|
||||
system.register_function(
|
||||
boost::function<Assignable &(Assignable*, Assignable&)>(&Assignable::operator=), "=");
|
||||
*/
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_container(BoxedCPP_System &system)
|
||||
void bootstrap_container(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_assignable<ContainerType>(system);
|
||||
bootstrap_assignable<ContainerType>(system, type);
|
||||
|
||||
system.register_function(
|
||||
boost::function<size_t (ContainerType *)>(&ContainerType::size), "size");
|
||||
@ -38,27 +42,28 @@ void bootstrap_container(BoxedCPP_System &system)
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_forward_container(BoxedCPP_System &system)
|
||||
void bootstrap_forward_container(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_container<ContainerType>(system);
|
||||
bootstrap_container<ContainerType>(system, type);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void bootstrap_default_constructible(BoxedCPP_System &system)
|
||||
void bootstrap_default_constructible(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
system.register_function(build_constructor<Type>(), type);
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_sequence(BoxedCPP_System &system)
|
||||
void bootstrap_sequence(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_forward_container<SequenceType>(system);
|
||||
bootstrap_default_constructible<SequenceType>(system);
|
||||
bootstrap_forward_container<SequenceType>(system, type);
|
||||
bootstrap_default_constructible<SequenceType>(system, type);
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_back_insertion_sequence(BoxedCPP_System &system)
|
||||
void bootstrap_back_insertion_sequence(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_sequence<SequenceType>(system);
|
||||
bootstrap_sequence<SequenceType>(system, type);
|
||||
|
||||
|
||||
typedef typename SequenceType::reference (SequenceType::*backptr)();
|
||||
@ -69,10 +74,11 @@ void bootstrap_back_insertion_sequence(BoxedCPP_System &system)
|
||||
}
|
||||
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(BoxedCPP_System &system)
|
||||
void bootstrap_vector(BoxedCPP_System &system, const std::string &type)
|
||||
{
|
||||
bootstrap_random_access_container<VectorType>(system);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system);
|
||||
system.register_type<VectorType>(type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -89,6 +89,20 @@ struct Cast_Helper<const Result &>
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result *>
|
||||
{
|
||||
const Result *operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result *>
|
||||
{
|
||||
|
@ -90,7 +90,7 @@ int main()
|
||||
{
|
||||
BoxedCPP_System ss;
|
||||
bootstrap(ss);
|
||||
bootstrap_vector<std::vector<int> >(ss);
|
||||
bootstrap_vector<std::vector<int> >(ss, "VectorInt");
|
||||
dump_system(ss);
|
||||
|
||||
//Calling a function by name and allowing the built in dispatch mechanism to
|
||||
|
@ -430,14 +430,15 @@ Lexer build_lexer() {
|
||||
BoxedCPP_System build_eval_system() {
|
||||
BoxedCPP_System ss;
|
||||
bootstrap(ss);
|
||||
bootstrap_vector<std::vector<int> >(ss);
|
||||
//dump_system(ss);
|
||||
bootstrap_vector<std::vector<int> >(ss, "VectorInt");
|
||||
// dump_system(ss);
|
||||
|
||||
//Register a new function, this one with typing for us, so we don't have to ubox anything
|
||||
//right here
|
||||
register_function(ss, &print<bool>, "print");
|
||||
register_function(ss, &print<std::string>, "print");
|
||||
register_function(ss, &print<double>, "print");
|
||||
register_function(ss, &print<size_t>, "print");
|
||||
register_function(ss, &concat_string, "concat_string");
|
||||
register_function(ss, &print<int>, "print");
|
||||
|
||||
@ -516,7 +517,11 @@ int main(int argc, char *argv[]) {
|
||||
Boxed_Value val = evaluate_string(lexer, parser, ss, input, "__EVAL__");
|
||||
if (*(val.get_type_info().m_bare_type_info) != typeid(void)) {
|
||||
std::cout << "result: ";
|
||||
dispatch(ss.get_function("print"), Param_List_Builder() << val);
|
||||
try {
|
||||
dispatch(ss.get_function("print"), Param_List_Builder() << val);
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::cout << "unhandled type: " << val.get_type_info().m_type_info->name() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << "eval> ";
|
||||
std::getline(std::cin, input);
|
||||
|
Loading…
x
Reference in New Issue
Block a user