Debian Codegraph @

libphobos/libdruntime/core/stdcpp/array.d

raw
1 /**
2 * D header file for interaction with C++ std::array.
3 *
4 * Copyright: Copyright (c) 2018 D Language Foundation
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Manu Evans
9 * Source: $(DRUNTIMESRC core/stdcpp/array.d)
10 */
11
12 module core.stdcpp.array;
13
14 import core.stdcpp.xutility : StdNamespace;
15
16 extern(C++, (StdNamespace)):
17
18 /**
19 * D language counterpart to C++ std::array.
20 *
21 * C++ reference: $(LINK2 https://en.cppreference.com/w/cpp/container/array)
22 */
23 extern(C++, class) struct array(T, size_t N)
24 {
25 extern(D):
26 pragma(inline, true):
27
28 ///
29 alias size_type = size_t;
30 ///
31 alias difference_type = ptrdiff_t;
32 ///
33 alias value_type = T;
34 ///
35 alias pointer = T*;
36 ///
37 alias const_pointer = const(T)*;
38
39 ///
40 alias as_array this;
41
42 /// Variadic constructor
43 this(T[N] args ...) { this[] = args[]; }
44
45 ///
46 void fill()(auto ref const(T) value) { this[] = value; }
47
48 pure nothrow @nogc:
49 ///
50 size_type size() const @safe { return N; }
51 ///
52 alias length = size;
53 ///
54 alias opDollar = length;
55 ///
56 size_type max_size() const @safe { return N; }
57 ///
58 bool empty() const @safe { return N == 0; }
59
60 ///
61 ref inout(T) front() inout @safe { static if (N > 0) { return this[0]; } else { return as_array()[][0]; /* HACK: force OOB */ } }
62 ///
63 ref inout(T) back() inout @safe { static if (N > 0) { return this[N-1]; } else { return as_array()[][0]; /* HACK: force OOB */ } }
64
65 version (CppRuntime_Microsoft)
66 {
67 ///
68 inout(T)* data() inout @safe { return &_Elems[0]; }
69 ///
70 ref inout(T)[N] as_array() inout @safe { return _Elems[0 .. N]; }
71 ///
72 ref inout(T) at(size_type i) inout @safe { return _Elems[0 .. N][i]; }
73
74 private:
75 T[N ? N : 1] _Elems;
76 }
77 else version (CppRuntime_GNU)
78 {
79 ///
80 inout(T)* data() inout @safe { static if (N > 0) { return &_M_elems[0]; } else { return null; } }
81 ///
82 ref inout(T)[N] as_array() inout @trusted { return data()[0 .. N]; }
83 ///
84 ref inout(T) at(size_type i) inout @trusted { return data()[0 .. N][i]; }
85
86 private:
87 static if (N > 0)
88 {
89 T[N] _M_elems;
90 }
91 else
92 {
93 struct _Placeholder {}
94 _Placeholder _M_placeholder;
95 }
96 }
97 else version (CppRuntime_LLVM)
98 {
99 ///
100 inout(T)* data() inout @trusted { static if (N > 0) { return &__elems_[0]; } else { return cast(inout(T)*)__elems_.ptr; } }
101 ///
102 ref inout(T)[N] as_array() inout @trusted { return data()[0 .. N]; }
103 ///
104 ref inout(T) at(size_type i) inout @trusted { return data()[0 .. N][i]; }
105
106 private:
107 static if (N > 0)
108 {
109 T[N] __elems_;
110 }
111 else
112 {
113 struct _ArrayInStructT { T[1] __data_; }
114 align(_ArrayInStructT.alignof)
115 byte[_ArrayInStructT.sizeof] __elems_ = void;
116 }
117 }
118 else
119 {
120 static assert(false, "C++ runtime not supported");
121 }
122 }