Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Updated the STL branch #2211

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ COPY=\
$(IMPDIR)\core\stdc\wchar_.d \
$(IMPDIR)\core\stdc\wctype.d \
\
$(IMPDIR)\core\stdcpp\typeinfo.d \
$(IMPDIR)\core\stdcpp\allocator.d \
$(IMPDIR)\core\stdcpp\array.d \
$(IMPDIR)\core\stdcpp\exception.d \
$(IMPDIR)\core\stdcpp\string.d \
$(IMPDIR)\core\stdcpp\tuple.d \
$(IMPDIR)\core\stdcpp\typeinfo.d \
$(IMPDIR)\core\stdcpp\utility.d \
$(IMPDIR)\core\stdcpp\vector.d \
\
$(IMPDIR)\core\sys\darwin\execinfo.d \
$(IMPDIR)\core\sys\darwin\pthread.d \
Expand Down
6 changes: 6 additions & 0 deletions mak/DOCS
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ DOCS=\
$(DOCDIR)\core_stdc_wchar_.html \
$(DOCDIR)\core_stdc_wctype.html \
\
$(DOCDIR)\core_stdcpp_allocator.html \
$(DOCDIR)\core_stdcpp_array.html \
$(DOCDIR)\core_stdcpp_exception.html \
$(DOCDIR)\core_stdcpp_string.html \
$(DOCDIR)\core_stdcpp_tuple.html \
$(DOCDIR)\core_stdcpp_typeinfo.html \
$(DOCDIR)\core_stdcpp_utility.html \
$(DOCDIR)\core_stdcpp_vector.html \
\
$(DOCDIR)\core_sync_barrier.html \
$(DOCDIR)\core_sync_condition.html \
Expand Down
9 changes: 9 additions & 0 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ SRCS=\
src\core\stdc\time.d \
src\core\stdc\wchar_.d \
\
src\core\stdcpp\allocator.d \
src\core\stdcpp\array.d \
src\core\stdcpp\exception.d \
src\core\stdcpp\string.d \
src\core\stdcpp\tuple.d \
src\core\stdcpp\typeinfo.d \
src\core\stdcpp\utility.d \
src\core\stdcpp\vector.d \
\
src\core\sync\barrier.d \
src\core\sync\condition.d \
src\core\sync\config.d \
Expand Down
75 changes: 75 additions & 0 deletions src/core/stdcpp/allocator.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* D header file for interaction with C++ std::allocator.
*
* Copyright: Copyright Guillaume Chatelet 2014 - 2015.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Guillaume Chatelet
* Manu Evans
* Source: $(DRUNTIMESRC core/stdcpp/allocator.d)
*/

module core.stdcpp.allocator;

alias allocator = std.allocator;

extern(C++, std):

/**
* Allocators are classes that define memory models to be used by some parts of
* the C++ Standard Library, and most specifically, by STL containers.
*/
extern(C++, class) struct allocator(T)
{
static assert(!is(T == const), "The C++ Standard forbids containers of const elements because allocator!(const T) is ill-formed.");

alias value_type = T;

alias pointer = value_type*;
alias const_pointer = const value_type*;

alias reference = ref value_type;
alias const_reference = ref const(value_type);

alias size_type = size_t;
alias difference_type = ptrdiff_t;

extern(D) size_t max_size() const nothrow @safe @nogc { return size_t.max / T.sizeof; }

// these need to be defined locally to work on local types...
extern(D) void construct(Ty, Args...)(Ty* ptr, auto ref Args args)
{
// placement new...
assert(false, "TODO: can't use emplace, cus it's in phobos...");
}

extern(D) void destroy(Ty)(Ty* ptr)
{
import object : destroy;
ptr.destroy(); // TODO: use `destruct` instead of destroy, which should exist in the future...
}


// platform specific detail
version(CRuntime_Microsoft)
{
extern(D) void deallocate(pointer ptr, size_type count) nothrow @safe @nogc { _Deallocate(ptr, count, T.sizeof); }
extern(D) pointer allocate(size_type count) nothrow @trusted @nogc { return cast(pointer)_Allocate(count, T.sizeof); }
extern(D) pointer allocate(size_type count, const(void)*) nothrow @safe @nogc { return allocate(count); }
}
else
{
void deallocate(pointer ptr, size_type count) nothrow @trusted @nogc;
pointer allocate(size_type count) nothrow @trusted @nogc;
pointer allocate(size_type count, const(void)*) nothrow @trusted @nogc;
}
}


// platform detail
version(CRuntime_Microsoft)
{
void* _Allocate(size_t _Count, size_t _Sz, bool _Try_aligned_allocation = true) nothrow @trusted @nogc;
void _Deallocate(void* _Ptr, size_t _Count, size_t _Sz) nothrow @trusted @nogc;
}
91 changes: 91 additions & 0 deletions src/core/stdcpp/array.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* D header file for interaction with C++ std::array.
*
* Copyright: Manu Evans 2014 - 2018.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Manu Evans
* Source: $(DRUNTIMESRC core/stdcpp/array.d)
*/

module core.stdcpp.array;

alias array = std.array;

extern(C++, std):

extern(C++, class) struct array(T, size_t N)
{
alias size_type = size_t;
alias difference_type = ptrdiff_t;
alias value_type = T;
alias reference = ref T;
alias const_reference = ref const(T);
alias pointer = T*;
alias const_pointer = const(T)*;

alias as_array this;

extern(D) size_type size() const nothrow @safe @nogc { return N; }
extern(D) size_type max_size() const nothrow @safe @nogc { return N; }
extern(D) bool empty() const nothrow @safe @nogc { return N == 0; }

// Element access
extern(D) reference front() @safe @nogc { static if (N > 0) { return as_array()[0]; } else { return as_array()[][0]; } }
extern(D) const_reference front() const @safe @nogc { static if (N > 0) { return as_array()[0]; } else { return as_array()[][0]; } }
extern(D) reference back() @safe @nogc { static if (N > 0) { return as_array()[N-1]; } else { return as_array()[][0]; } }
extern(D) const_reference back() const @safe @nogc { static if (N > 0) { return as_array()[N-1]; } else { return as_array()[][0]; } }

extern(D) void fill(ref const(T) value) @safe @nogc { foreach (ref T v; as_array()) v = value; }

// D helpers
extern(D) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert(pos == 0, "std::vector is one-dimensional"); return N; }

version(CRuntime_Microsoft)
{
// perf will be greatly improved by inlining the primitive access functions
extern(D) T* data() nothrow @safe @nogc { return &_Elems[0]; }
extern(D) const(T)* data() const nothrow @safe @nogc { return &_Elems[0]; }

extern(D) ref T at(size_type i) nothrow @trusted @nogc { static if (N > 0) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); return _Elems[0]; } }
extern(D) ref const(T) at(size_type i) const nothrow @trusted @nogc { static if (N > 0) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); return _Elems[0]; } }

extern(D) ref inout(T)[N] as_array() const inout @safe @nogc { return _Elems[0 .. N]; }

private:
import core.stdcpp.utility : _Xout_of_range;

T[N ? N : 1] _Elems;

void _Xran() const @safe @nogc { _Xout_of_range("invalid array<T, N> subscript"); }
}
else version(CRuntime_Glibc)
{
import core.exception : RangeError;

// perf will be greatly improved by inlining the primitive access functions
extern(D) T* data() nothrow @safe @nogc { static if (N > 0) { return &_M_elems[0]; } else { return null; } }
extern(D) const(T)* data() const nothrow @safe @nogc { static if (N > 0) { return &_M_elems[0]; } else { return null; } }

extern(D) ref T at(size_type i) @trusted { if (i >= N) throw new RangeError("Index out of range"); return _M_elems.ptr[i]; }
extern(D) ref const(T) at(size_type i) const @trusted { if (i >= N) throw new RangeError("Index out of range"); return _M_elems.ptr[i]; }

extern(D) ref inout(T)[N] as_array() inout nothrow @safe @nogc { return _M_elems[0 .. N]; }

private:
static if (N > 0)
{
T[N] _M_elems;
}
else
{
struct _Placeholder {}
_Placeholder _M_placeholder;
}
}
else
{
static assert(false, "C++ runtime not supported");
}
}
151 changes: 76 additions & 75 deletions src/core/stdcpp/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,96 @@

module core.stdcpp.exception;

alias exception = std.exception;
alias bad_exception = std.bad_exception;
alias bad_alloc = std.bad_alloc;

extern (C++, std):

version (CRuntime_DigitalMars)
{
import core.stdcpp.typeinfo;

extern (C++, std)
alias void function() unexpected_handler;
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
void unexpected();

alias void function() terminate_handler;
terminate_handler set_terminate(terminate_handler f) nothrow;
void terminate();

bool uncaught_exception();

class exception
{
alias void function() unexpected_handler;
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
void unexpected();

alias void function() terminate_handler;
terminate_handler set_terminate(terminate_handler f) nothrow;
void terminate();

bool uncaught_exception();

class exception
{
this() nothrow { }
this(const exception) nothrow { }
//exception operator=(const exception) nothrow { return this; }
//virtual ~this() nothrow;
void dtor() { }
const(char)* what() const nothrow;
}

class bad_exception : exception
{
this() nothrow { }
this(const bad_exception) nothrow { }
//bad_exception operator=(const bad_exception) nothrow { return this; }
//virtual ~this() nothrow;
override const(char)* what() const nothrow;
}
this() nothrow { }
this(const exception) nothrow { }
//exception operator=(const exception) nothrow { return this; }
//virtual ~this() nothrow;
void dtor() { }
const(char)* what() const nothrow;
}

class bad_exception : exception
{
this() nothrow { }
this(const bad_exception) nothrow { }
//bad_exception operator=(const bad_exception) nothrow { return this; }
//virtual ~this() nothrow;
override const(char)* what() const nothrow;
}
}
else version (CRuntime_Glibc)
{
extern (C++, std)
alias void function() unexpected_handler;
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
void unexpected();

alias void function() terminate_handler;
terminate_handler set_terminate(terminate_handler f) nothrow;
void terminate();

pure bool uncaught_exception();

class exception
{
alias void function() unexpected_handler;
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
void unexpected();

alias void function() terminate_handler;
terminate_handler set_terminate(terminate_handler f) nothrow;
void terminate();

pure bool uncaught_exception();

class exception
{
this();
//virtual ~this();
void dtor1();
void dtor2();
const(char)* what() const;
}

class bad_exception : exception
{
this();
//virtual ~this();
override const(char)* what() const;
}
this();
//virtual ~this();
void dtor1();
void dtor2();
const(char)* what() const;
}

class bad_exception : exception
{
this();
//virtual ~this();
override const(char)* what() const;
}
}
else version (CRuntime_Microsoft)
{
extern (C++, std)
class exception
{
this(const(char)* _Message = "unknown", int x = 1) { _Ptr = _Message; }
this(ref const(exception) _Right) { _Ptr = _Right._Ptr; }
~this() {}

const(char)* what() const { return _Ptr ? _Ptr : "unknown exception"; }

private:
const(char)* _Ptr;
}

class bad_exception : exception
{
this(const(char)* _Message = "bad exception") { super(_Message); }
~this() {}
}

class bad_alloc : exception
{
class exception
{
this();
this(const exception);
//exception operator=(const exception) { return this; }
//virtual ~this();
void dtor() { }
const(char)* what() const;

private:
const(char)* mywhat;
bool dofree;
}

class bad_exception : exception
{
this(const(char)* msg = "bad exception");
//virtual ~this();
}
this() { super("bad allocation", 1); }
~this() {}
}
}
Loading