Skip to content

Commit

Permalink
early stage binding for std::list, targetting GNU gcc pre-c++11 and p…
Browse files Browse the repository at this point in the history
…ost-c++11 standards.

tests added for bindings and more to come.
  • Loading branch information
Emmankoko committed Nov 15, 2023
1 parent f4e3b52 commit 6b57c91
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"name": "unittest",
"lflags": [ "-lstdc++" ],
"preGenerateCommands": [
"g++ -c extras/vector.cpp -o extras/vector.o"
"g++ -c extras/list.cpp -o extras/list.o"
],
"sourceFiles": [ "extras/vector.o" ]
"sourceFiles": [ "extras/list.o" ]
},
]
}
3 changes: 3 additions & 0 deletions extras/list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <list>

template class std::list<int>;
318 changes: 318 additions & 0 deletions source/stdcpp/list.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
/**
* D bindings for std::list
*
* Copyright: Copyright (c) 2023 D Language Foundation
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1.0.txt, Boost Software License 1.0).
* (See accompanying file License)
* Authors: Mathias Lang
* Emmanuel Nyarko
*/

module stdcpp.list;

import stdcpp.allocator;

version(CppRuntime_Gcc)
{
version(GLIBCXX_USE_CXX98_ABI)
{
private enum listNamespace = "std";
}
else
{
import core.internal.traits : AliasSeq;
private enum listNamespace = AliasSeq!("std", "__cxx11");
}
}
else
{
import stdcpp.xutility : StdNamespace;

alias listNamespace = StdNamespace;
}
enum def {value}; //for later use
extern(C++, (listNamespace)):

alias list(Type) = list!(Type, allocator!Type);
extern(C++, class) struct list(Type, Allocator)
{
static assert(!is(Type == bool), " list not required to have specialization for bool");


//MEMBER TYPES

alias value_type = Type;

alias allocator_type = Allocator;

alias size_type = size_t;

alias pointer = Type*;

alias const_pointer = const(Type)*;

alias difference_type = ptrdiff_t;

//references can't be aliased,'ref' has no effect in type aliases


//MEMBER FUNCTIONS


ref list opAssign();



@disable this();

version(CppRuntime_Gcc)
{
version(GLIBCXX_USE_CXX98_ABI)
{

//allocator ctor
this(ref const allocator!Type);


//list(n,value) ctor until c++11
this(size_type __n, ref const value_type value, ref const allocator!Type);

//copy ctor
this(ref const list!Type __x);

extern(D) this(size_type n)
{
value_type type_instance = value_type.init;
allocator!Type alloc_instance = allocator!(Type).init;
this(n, type_instance, alloc_instance);
}

~this();

extern(D) void assign(size_type n, const value_type item)
{
this.assign(n, item);
}

extern(D) void push_back(const Type item)
{
this.push_back(item);
}

extern(D) void push_front(const Type item)
{
this.push_front(item);
}

extern(D) void remove(const value_type item)
{
this.remove(item);
}

void assign(size_type count, ref const value_type value);

ref list opAssign(ref const list!Type other);

//just const until c++11
allocator_type get_allocator() const;

ref value_type front();

// const(value_type) ref front() const;

ref value_type back();

pointer begin();

pointer end();

//just const until c++11
bool empty() const;

size_type size() const;

size_type max_size() const nothrow;

void clear();

//insert halted for now

void push_back(ref const Type val);

void pop_back();

void push_front(ref const value_type val);

void pop_front();

void swap(ref const list!Type other);

void merge( ref const list!Type other);

void merge(U)(ref const list!Type other, U comp);

void remove(const ref value_type val);

void reverse();

void sort();

size_type unique();

void sort(U)(U comp);
private:
struct node
{
node* prev;
node* next;
}
node A; //pre-c++1 doesn't keep track of its size
}
else
{

this(def)
{
allocator!Type alloc_instance = allocator!(Type).init;
this(alloc_instance);
}


//allocator ctor
this(ref const allocator!Type);

//copy ctor
this(ref const list!Type __x);

//list(n,value) ctor
this(size_type __n, ref const value_type value, ref const allocator!Type);

extern(D) this(size_type n, const value_type element)
{
allocator!Type alloc_instance = allocator!(Type).init;
this(n, element, alloc_instance);
}

this(ref const list!Type other, ref const allocator!Type);

//list(n) ctor
this(size_type __n, ref const allocator!Type);

extern(D) this(size_type n)
{
allocator!Type alloc_instance = allocator!(Type).init;
this(n, alloc_instance);
}

~this();

extern(D) void assign(size_type n, const value_type item)
{
this.assign(n, item);
}

extern(D) void push_back(const Type item)
{
this.push_back(item);
}

extern(D) void push_front(const Type item)
{
this.push_front(item);
}

extern(D) void resize(size_type n, const value_type item)
{
this.resize(n, item);
}

extern(D) void remove(const value_type item)
{
this.remove(item);
}

/* extern(D) size_type remove(const value_type item)
{
return this.remove(item);
}
*/
ref list opAssign(ref const list!Type other);

void assign(size_type count, ref const value_type value);

//const nothrow since C++11
allocator_type get_allocator() const nothrow;

ref value_type front();

// const(value_type) ref front() const;

ref value_type back();

// ref const(value_type) back() const;

pointer begin() nothrow;

pointer end() nothrow;

//const nothrow since c++11
bool empty() const nothrow;

size_type size() const nothrow;

void clear() nothrow;

//insert halted for now

void push_back(ref const Type val);

void pop_back();

void push_front(ref const value_type val);

void pop_front();

void resize(size_type count);

void resize(size_type count, ref const value_type val);

void swap(ref const list!Type other) nothrow;

void merge( ref const list!Type other);

void merge(U)(ref const list!Type other, U comp);

void remove(const ref value_type val);

// size_type remove(const ref value_type val);

void reverse() nothrow;

void sort();

void sort(U)(U comp);

void unique();

void unique(U)(U p);

size_type unique();

size_type unique(U)(U p);

private:
struct node
{
node* prev;
node* next;
}
node A;
size_type _M_size; //new list keeps track of it's size

}
}
else version(CppRuntime_Microsft)
{
}
}
36 changes: 36 additions & 0 deletions source/stdcpp/test/list.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************
test for std::list
******************************/

module stdcpp.test.list;

import stdcpp.list;

unittest
{
auto p = list!int(5);
p.push_back(5);
assert(p.sizeof == 24);
assert(p.size() == 6);
assert(p.front() == 0);
assert(p.back() == 5);
p.push_front(7);
assert(p.front() == 7);
p.clear();
assert(p.size() == 0);
p.assign(5,5);
assert(p.size == 5);
p.pop_front();
assert(p.size == 4);
p.resize(3);
assert(p.size == 3);

list!int cp_obj = p; //opAssign
assert(cp_obj.size == 3);
cp_obj.clear();
cp_obj.push_back(45);
cp_obj.push_back(56);
assert(cp_obj.front == 45);
assert(cp_obj.back == 56);
}

0 comments on commit 6b57c91

Please sign in to comment.