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

Commit

Permalink
Added std::array<>
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed Aug 27, 2018
1 parent db2910e commit 84c614b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ COPY=\
$(IMPDIR)\core\stdc\wchar_.d \
$(IMPDIR)\core\stdc\wctype.d \
\
$(IMPDIR)\core\stdcpp\array.d \
$(IMPDIR)\core\stdcpp\typeinfo.d \
$(IMPDIR)\core\stdcpp\exception.d \
\
Expand Down
1 change: 1 addition & 0 deletions mak/DOCS
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ DOCS=\
$(DOCDIR)\core_stdc_wchar_.html \
$(DOCDIR)\core_stdc_wctype.html \
\
$(DOCDIR)\core_stdcpp_array.html \
$(DOCDIR)\core_stdcpp_exception.html \
$(DOCDIR)\core_stdcpp_typeinfo.html \
\
Expand Down
2 changes: 2 additions & 0 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ SRCS=\
src\core\stdc\time.d \
src\core\stdc\wchar_.d \
\
src\core\stdcpp\array.d \
\
src\core\sync\barrier.d \
src\core\sync\condition.d \
src\core\sync\config.d \
Expand Down
123 changes: 123 additions & 0 deletions src/core/stdcpp/array.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* D header file for interaction with C++ std::array.
*
* Copyright: Copyright (c) 2018 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: Manu Evans
* Source: $(DRUNTIMESRC core/stdcpp/array.d)
*/

module core.stdcpp.array;

alias array = std.array;

extern(C++, std):

/**
* D language counterpart to C++ std::array.
*
* C++ reference: $(LINK2 https://en.cppreference.com/w/cpp/container/array)
*/
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.xutility : _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");
}
}

0 comments on commit 84c614b

Please sign in to comment.