Add std::list support

This commit is contained in:
Jason Turner 2009-08-29 14:00:07 +00:00
parent c5e1d5fa20
commit c8c1c65e8c
7 changed files with 55 additions and 0 deletions

View File

@ -322,6 +322,45 @@ namespace chaiscript
return m; return m;
} }
/**
*Front insertion sequence
*http://www.sgi.com/tech/stl/FrontInsertionSequence.html
*/
template<typename ContainerType>
ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
sequence_type<ContainerType>(type, m);
typedef typename ContainerType::reference (ContainerType::*frontptr)();
m->add(fun(frontptr(&ContainerType::front)), "front");
std::string push_front_name;
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
{
push_front_name = "push_front_ref";
} else {
push_front_name = "push_front";
}
m->add(fun(&ContainerType::push_front), push_front_name);
m->add(fun(&ContainerType::pop_front), "pop_front");
return m;
}
/**
* hopefully working List type
* http://www.sgi.com/tech/stl/List.html
*/
template<typename ListType>
ModulePtr list_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
m->add(user_type<ListType>(), type);
front_insertion_sequence_type<ListType>(type, m);
back_insertion_sequence_type<ListType>(type, m);
return m;
}
/** /**
* Create a vector type with associated concepts * Create a vector type with associated concepts
* http://www.sgi.com/tech/stl/Vector.html * http://www.sgi.com/tech/stl/Vector.html

View File

@ -44,6 +44,8 @@ def odd(x) { if (x % 2 == 1) { true } else { false } } \
def even(x) { if (x % 2 == 0) { true } else { false } } \ def even(x) { if (x % 2 == 0) { true } else { false } } \
# Pushes the second value onto the container first value while making a clone of the value\n\ # Pushes the second value onto the container first value while making a clone of the value\n\
def push_back(container, x) : call_exists(push_back_ref, container, x) { container.push_back_ref(clone(x)) } \n\ def push_back(container, x) : call_exists(push_back_ref, container, x) { container.push_back_ref(clone(x)) } \n\
# Pushes the second value onto the front of the container first value while making a clone of the value\n\
def push_front(container, x) : call_exists(push_front_ref, container, x) { container.push_front_ref(clone(x)) } \n\
# Inserts the third value at the position of the second value into the container of the first\n\ # Inserts the third value at the position of the second value into the container of the first\n\
# while making a clone. \n\ # while making a clone. \n\
def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\ def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\

View File

@ -6,6 +6,8 @@
#include <iostream> #include <iostream>
#include <list>
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
void print_help() { void print_help() {
@ -19,6 +21,8 @@ int main(int argc, char *argv[]) {
std::string input; std::string input;
chaiscript::ChaiScript chai; chaiscript::ChaiScript chai;
chai.add(chaiscript::bootstrap::list_type<std::list<chaiscript::Boxed_Value> >("List"));
if (argc < 2) { if (argc < 2) {
std::cout << "eval> "; std::cout << "eval> ";
std::getline(std::cin, input); std::getline(std::cin, input);

View File

@ -0,0 +1,4 @@
var x = List()
x.push_back(3)
x.push_back("A")
print(x)

View File

@ -0,0 +1 @@
[3, A]

View File

@ -0,0 +1,4 @@
var x = List()
x.push_front(3)
x.push_front("A")
print(x)

View File

@ -0,0 +1 @@
[A, 3]