From 3f2615f75d6932beafb86eb0e7e833566848d5b8 Mon Sep 17 00:00:00 2001 From: Emmanuel Nyarko Date: Fri, 12 Apr 2024 20:52:25 +0000 Subject: [PATCH 1/2] Refactoring stdcpp/list.d to remove duplications some of the functions have no difference in the interface structure on all our target runtimes so these have been moved out of any version compilation as they increase duplications in the API. they are removed from all three runtimes and just represented by a single interface since they are linkable and callable from C++ without any foreign additions --- source/stdcpp/list.d | 239 +++++++++++++------------------------------ 1 file changed, 71 insertions(+), 168 deletions(-) diff --git a/source/stdcpp/list.d b/source/stdcpp/list.d index c86267e..db86da8 100644 --- a/source/stdcpp/list.d +++ b/source/stdcpp/list.d @@ -29,6 +29,12 @@ else alias listNamespace = StdNamespace; } +//to set up interfaces common to both gcc and microsoft but not with clang runtime +version(CppRuntime_Gcc) + version = NonClang; +else version(CppRuntime_Microsoft) + version = NonClang; + enum def {value}; //for later use extern(C++, (listNamespace)): @@ -40,80 +46,106 @@ extern(C++, class) struct list(Type, Allocator) /// 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; - /// ref list opAssign(); - /// @disable this() @safe pure nothrow @nogc scope; + //allocator ctor + this(ref const allocator!Type); + + /// Copy constructor + this(ref const list __x); + /// + extern(D) void assign(size_type n, const value_type item) + { + this.assign(n, item); + } + /// + void assign(size_type count, ref const value_type value); + /// + extern(D) void push_back(const Type item) + { + this.push_back(item); + } + /// + void push_back(ref const Type val); + /// + extern(D) void push_front(const Type item) + { + this.push_front(item); + } + /// + void push_front(ref const value_type val); + /// + void pop_front(); + /// + void pop_back(); + /// + void resize(size_type count); + + ~this(); + + version(NonClang) + { + /// + inout(ref) value_type back() inout; + /// + inout(ref) value_type front() inout; + } + version (CppRuntime_Gcc) { - version (GLIBCXX_USE_CXX98_ABI) + extern(D) void remove(const value_type item) { - //allocator ctor - this(ref const allocator!Type); + this.remove(item); + } + /// + void remove(const ref value_type val); + /// + private struct node + { + node* prev; + node* next; + } + private node A; + + version (GLIBCXX_USE_CXX98_ABI) + { //list(n,value) ctor until c++11 this(size_type __n, ref const value_type value, ref const allocator!Type); - /// Copy constructor - 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); } - - 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; + inout(ref) value_type front() inout; - ref value_type back(); + inout(ref) value_type back() inout; pointer begin(); @@ -130,14 +162,6 @@ extern(C++, class) struct list(Type, Allocator) //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); @@ -153,29 +177,9 @@ extern(C++, class) struct list(Type, Allocator) size_type unique(); void sort(U)(U comp); - - private struct node - { - node* prev; - node* next; - } - // pre-c++11 doesn't keep track of its size - private node A; } else // !GLIBCXX_USE_CXX98_ABI { - this(def) - { - allocator!Type alloc_instance = allocator!(Type).init; - this(alloc_instance); - } - - //allocator ctor - this(ref const allocator!Type); - - // Copy constructor - this(ref const list!Type __x); - //list(n,value) ctor this(size_type __n, ref const value_type value, ref const allocator!Type); @@ -196,21 +200,6 @@ extern(C++, class) struct list(Type, Allocator) this(n, alloc_instance); } - 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); @@ -228,10 +217,6 @@ extern(C++, class) struct list(Type, Allocator) //const nothrow since C++11 allocator_type get_allocator() const nothrow; - ref value_type front(); - - ref value_type back(); - pointer begin() nothrow; pointer end() nothrow; @@ -243,16 +228,6 @@ extern(C++, class) struct list(Type, Allocator) void clear() nothrow; - 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; @@ -267,33 +242,17 @@ extern(C++, class) struct list(Type, Allocator) void sort(); - void sort(U)(U comp); - - void unique(); - - void unique(U)(U p); + //todo: add sort(U)() size_type unique(); - size_type unique(U)(U p); + // todo: add size_type unique(U)(U p); - private struct node - { - node* prev; - node* next; - } - private node A; private size_type _M_size; //new list keeps track of it's size } } else version (CppRuntime_Clang) { - /// - this(ref const allocator!Type); - - /// Copy constructor - this(ref const list!Type __x); - this(size_type __n, ref const value_type value); /// extern(D) this(size_type n, const value_type element) @@ -307,23 +266,6 @@ extern(C++, class) struct list(Type, Allocator) /// this(size_type n); /// - ~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); @@ -336,17 +278,15 @@ extern(C++, class) struct list(Type, Allocator) /// ref list opAssign(ref const list!Type other); /// - void assign(size_type count, ref const value_type value); - /// allocator_type get_allocator() const nothrow; /// - ref value_type front() + inout(ref) value_type front() inout { assert(!empty, "list.front called on empty list"); return base.__end_.next.__as_node.__get_value; } /// - ref value_type back() + inout(ref) value_type back() inout { assert(!empty, "list.back called on empty list"); return base.__end_.prev.__as_node.__get_value; @@ -371,16 +311,6 @@ extern(C++, class) struct list(Type, Allocator) base.clear(); } - 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; @@ -406,10 +336,6 @@ extern(C++, class) struct list(Type, Allocator) else version(CppRuntime_Microsoft) { - this(ref const allocator!Type); - - this(ref const list!Type __x); - this(size_type __n, ref const value_type value, ref const allocator!Type); extern(D) this(size_type n, const value_type element) @@ -424,23 +350,6 @@ extern(C++, class) struct list(Type, Allocator) this(size_type n); - ~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); @@ -448,14 +357,8 @@ extern(C++, class) struct list(Type, Allocator) ref list opAssign(ref const list!Type other); - void assign(size_type count, ref const value_type value); - allocator_type get_allocator() const nothrow; - ref value_type front(); - - ref value_type back(); - pointer begin() nothrow; pointer end() nothrow; From 9a081156d509c00c6f692a41317428d5f04ca6cf Mon Sep 17 00:00:00 2001 From: Emmanuel Nyarko Date: Fri, 12 Apr 2024 20:56:16 +0000 Subject: [PATCH 2/2] Todo lists: These ones have been moved to todo lists because as at now, D cannot call C++ templated member/nested functions and so for future improvements. we fix reference issues as well --- source/stdcpp/list.d | 81 ++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 55 deletions(-) diff --git a/source/stdcpp/list.d b/source/stdcpp/list.d index db86da8..5472729 100644 --- a/source/stdcpp/list.d +++ b/source/stdcpp/list.d @@ -94,14 +94,12 @@ extern(C++, class) struct list(Type, Allocator) /// void resize(size_type count); - ~this(); - version(NonClang) { /// - inout(ref) value_type back() inout; + ref inout(value_type) back() inout; /// - inout(ref) value_type front() inout; + ref inout(value_type) front() inout; } version (CppRuntime_Gcc) @@ -132,21 +130,12 @@ extern(C++, class) struct list(Type, Allocator) allocator!Type alloc_instance = allocator!(Type).init; this(n, type_instance, alloc_instance); } - /// - extern(D) void remove(const value_type item) - { - this.remove(item); - } - ref list opAssign(ref const list!Type other); + ref list opAssign(ref const list other); //just const until c++11 allocator_type get_allocator() const; - inout(ref) value_type front() inout; - - inout(ref) value_type back() inout; - pointer begin(); pointer end(); @@ -168,8 +157,6 @@ extern(C++, class) struct list(Type, Allocator) void merge(U)(ref const list!Type other, U comp); - void remove(const ref value_type val); - void reverse(); void sort(); @@ -189,7 +176,7 @@ extern(C++, class) struct list(Type, Allocator) this(n, element, alloc_instance); } - this(ref const list!Type other, ref const allocator!Type); + this(ref const list other, ref const allocator!Type); //list(n) ctor this(size_type __n, ref const allocator!Type); @@ -205,12 +192,7 @@ extern(C++, class) struct list(Type, Allocator) this.resize(n, item); } - extern(D) void remove(const value_type item) - { - this.remove(item); - } - - ref list opAssign(ref const list!Type other); + ref list opAssign(ref const list other); void assign(size_type count, ref const value_type value); @@ -230,13 +212,11 @@ extern(C++, class) struct list(Type, Allocator) void resize(size_type count, ref const value_type val); - void swap(ref const list!Type other) nothrow; + void swap(ref const list other) nothrow; - void merge( ref const list!Type other); - - void merge(U)(ref const list!Type other, U comp); + void merge( ref const list other); - void remove(const ref value_type val); + void merge(U)(ref const list other, U comp); void reverse() nothrow; @@ -266,6 +246,9 @@ extern(C++, class) struct list(Type, Allocator) /// this(size_type n); /// + + ~this(); + extern(D) void resize(size_type n, const value_type item) { this.resize(n, item); @@ -280,13 +263,13 @@ extern(C++, class) struct list(Type, Allocator) /// allocator_type get_allocator() const nothrow; /// - inout(ref) value_type front() inout + ref value_type front() { assert(!empty, "list.front called on empty list"); return base.__end_.next.__as_node.__get_value; } /// - inout(ref) value_type back() inout + ref value_type back() { assert(!empty, "list.back called on empty list"); return base.__end_.prev.__as_node.__get_value; @@ -313,11 +296,11 @@ extern(C++, class) struct list(Type, Allocator) void resize(size_type count, ref const value_type val); - void swap(ref const list!Type other) nothrow; + void swap(ref const list other) nothrow; - void merge( ref const list!Type other); + void merge( ref const list other); - void merge(U)(ref const list!Type other, U comp); + void merge(U)(ref const list other, U comp); void remove(const ref value_type val); @@ -325,11 +308,11 @@ extern(C++, class) struct list(Type, Allocator) void sort(); - void sort(U)(U comp); + // todo: void sort(U)(U comp); - void unique(); + size_type unique(); - void unique(U)(U p); + //todo: size_type unique(U)(U p); private __list_imp!(value_type, allocator!Type) base; } @@ -369,37 +352,25 @@ extern(C++, class) struct list(Type, Allocator) void clear() nothrow; - 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 list!Type other) nothrow; + void swap(ref list other) nothrow; - void merge( ref list!Type other); + void merge( ref list other); - void merge(U)(ref list!Type other, U comp); + // todo: void merge(U)(ref list other, U comp); void reverse() nothrow; void sort(); - void sort(U)(U comp); - - void unique(); - - void unique(U)(U p); + //todo: void sort(U)(U comp); size_type unique(); - size_type unique(U)(U p); + ~this(); + + //todo :size_type unique(U)(U p); private struct node {