Add std::list support
This commit is contained in:
parent
c5e1d5fa20
commit
c8c1c65e8c
@ -322,6 +322,45 @@ namespace chaiscript
|
||||
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
|
||||
* http://www.sgi.com/tech/stl/Vector.html
|
||||
|
@ -44,6 +44,8 @@ def odd(x) { if (x % 2 == 1) { 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\
|
||||
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\
|
||||
# while making a clone. \n\
|
||||
def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <list>
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
void print_help() {
|
||||
@ -19,6 +21,8 @@ int main(int argc, char *argv[]) {
|
||||
std::string input;
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::bootstrap::list_type<std::list<chaiscript::Boxed_Value> >("List"));
|
||||
|
||||
if (argc < 2) {
|
||||
std::cout << "eval> ";
|
||||
std::getline(std::cin, input);
|
||||
|
4
unittests/list_push_back.chai
Normal file
4
unittests/list_push_back.chai
Normal file
@ -0,0 +1,4 @@
|
||||
var x = List()
|
||||
x.push_back(3)
|
||||
x.push_back("A")
|
||||
print(x)
|
1
unittests/list_push_back.txt
Normal file
1
unittests/list_push_back.txt
Normal file
@ -0,0 +1 @@
|
||||
[3, A]
|
4
unittests/list_push_front.chai
Normal file
4
unittests/list_push_front.chai
Normal file
@ -0,0 +1,4 @@
|
||||
var x = List()
|
||||
x.push_front(3)
|
||||
x.push_front("A")
|
||||
print(x)
|
1
unittests/list_push_front.txt
Normal file
1
unittests/list_push_front.txt
Normal file
@ -0,0 +1 @@
|
||||
[A, 3]
|
Loading…
x
Reference in New Issue
Block a user