Rudiments

staticarrayinlines.h
1 // Copyright (c) 1999-2018 David Muse
2 // See the COPYING file for more information.
3
4 #include <rudiments/private/new.h>
5
6 template< class valuetype, uint64_t length >
7 inline
8 staticarray<valuetype,length>::staticarray() {
9 len=length;
10 data=new valuetype[len];
11 }
12
13 template< class valuetype, uint64_t length >
14 inline
15 staticarray<valuetype,length>::staticarray(
16 const staticarray<valuetype,length> &v) {
17 len=v.len;
18 data=new valuetype[len];
19 for (uint64_t i=0; i<len; i++) {
20
21 // Why not just:
22 // *data[i]=*v.data[i];
23 //
24 // Some compilers get confused and think that
25 // *data[i]=*v.data[i]
26 // means
27 // *((data[i])->operator=(*v.data[i]))
28 // and no carefully placed parentheses help.
29 //
30 // This silliness sorts out the problem.
31 valuetype *a=&(data[i]);
32 valuetype *b=&(v.data[i]);
33 *a=*b;
34 }
35 }
36
37 template< class valuetype, uint64_t length >
38 inline
39 staticarray<valuetype,length> &staticarray<valuetype,length>::operator=(
40 const staticarray<valuetype,length> &v) {
41 if (this!=&v) {
42 len=v.len;
43 data=new valuetype[len];
44 for (uint64_t i=0; i<len; i++) {
45
46 // Why not just:
47 // *data[i]=*v.data[i];
48 //
49 // Some compilers get confused and think that
50 // *data[i]=*v.data[i]
51 // means
52 // *((data[i])->operator=(*v.data[i]))
53 // and no carefully placed parentheses help.
54 //
55 // This silliness sorts out the problem.
56 valuetype *a=&(data[i]);
57 valuetype *b=&(v.data[i]);
58 *a=*b;
59 }
60 }
61 return *this;
62 }
63
64 template< class valuetype, uint64_t length >
65 inline
66 staticarray<valuetype,length>::~staticarray() {
67 delete[] data;
68 }
69
70 template< class valuetype, uint64_t length >
71 inline
72 valuetype &staticarray<valuetype,length>::operator[](uint64_t index) {
73 return data[index];
74 }
75
76 template< class valuetype, uint64_t length >
77 inline
78 uint64_t staticarray<valuetype,length>::getLength() const {
79 return len;
80 }
81
82 template< class valuetype, uint64_t length >
83 inline
84 void staticarray<valuetype,length>::clear() {
85 for (uint64_t i=0; i<len; i++) {
86 ((valuetype *)&data[i])->~valuetype();
87 new(&data[i]) valuetype;
88 }
89 }
staticarray
Definition: staticarray.h:37
staticarray::clear
void clear()
Definition: staticarrayinlines.h:84
staticarray::operator=
staticarray< valuetype, length > & operator=(const staticarray< valuetype, length > &v)
Definition: staticarrayinlines.h:39
staticarray::staticarray
staticarray()
Definition: staticarrayinlines.h:8
staticarray::getLength
uint64_t getLength() const
Definition: staticarrayinlines.h:78
staticarray::operator[]
valuetype & operator[](uint64_t index)
Definition: staticarrayinlines.h:72
staticarray::~staticarray
~staticarray()
Definition: staticarrayinlines.h:66