Make inplace vector construction result in const temp return. Also clean up construction of vectors in code

This commit is contained in:
Jason Turner
2010-10-02 22:19:51 +00:00
parent 512d6b342d
commit fa2a7045a7
2 changed files with 13 additions and 19 deletions

View File

@@ -181,11 +181,14 @@ namespace detail {
ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t); typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
typedef typename ContainerType::const_reference(ContainerType::*constindexoper)(size_t) const;
//In the interest of runtime safety for the m, we prefer the at() method for [] access, //In the interest of runtime safety for the m, we prefer the at() method for [] access,
//to throw an exception in an out of bounds condition. //to throw an exception in an out of bounds condition.
m->add( m->add(
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(boost::mem_fn(static_cast<indexoper>(&ContainerType::at)))), "[]"); fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(boost::mem_fn(static_cast<indexoper>(&ContainerType::at)))), "[]");
m->add(
fun(boost::function<typename ContainerType::const_reference (const ContainerType *, int)>(boost::mem_fn(static_cast<constindexoper>(&ContainerType::at)))), "[]");
return m; return m;
} }

View File

@@ -372,29 +372,20 @@ namespace chaiscript
* Evaluates (and generates) an inline array initialization * Evaluates (and generates) an inline array initialization
*/ */
Boxed_Value Inline_Array_AST_Node::eval(Dispatch_Engine &ss) { Boxed_Value Inline_Array_AST_Node::eval(Dispatch_Engine &ss) {
try { std::vector<Boxed_Value> vec;
Boxed_Value retval = ss.call_function("Vector"); if (this->children.size() > 0) {
if (this->children.size() > 0) { for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
for (size_t i = 0; i < this->children[0]->children.size(); ++i) { try {
try { vec.push_back(this->children[0]->children[i]->eval(ss));
ss.call_function("push_back", retval, this->children[0]->children[i]->eval(ss)); }
} catch(Eval_Error &ee) {
catch (const dispatch_error &) { ee.call_stack.push_back(this->children[0]->children[i]);
throw Eval_Error("Can not find appropriate 'push_back'"); throw;
}
catch(Eval_Error &ee) {
ee.call_stack.push_back(this->children[0]->children[i]);
throw;
}
} }
} }
return retval;
}
catch (const dispatch_error &) {
throw Eval_Error("Can not find appropriate 'Vector()'");
} }
return const_var(vec);
} }
/** /**