Skip to content

Commit 88341ff

Browse files
committed
Support vector initialization using an initializer list
1 parent bc73b5e commit 88341ff

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoSTL
2-
version=0.1.6
2+
version=0.1.7
33
author=Mike Matera <matera@lifealgorithmic.com>
44
maintainer=Mike Matera <matera@lifealgorithmic.com>
55
sentence=A port of uClibc++ packaged as an Arduino library.

src/initializer_list

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright (C) 2016 Michael Matera
2+
3+
This file is part of the uClibc++ Library.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
19+
*/
20+
21+
22+
#ifndef __STD_HEADER_INITIALIZER_LIST
23+
#define __STD_HEADER_INITIALIZER_LIST
24+
25+
#pragma GCC visibility push(default)
26+
27+
namespace std {
28+
29+
template<class T>
30+
class initializer_list {
31+
32+
private:
33+
const T* array;
34+
size_t len;
35+
36+
// Initialize from a { ... } construct
37+
initializer_list(const T *a, size_t l) {
38+
}
39+
40+
public:
41+
42+
// default constructor
43+
initializer_list() : array(NULL), len(0) {}
44+
45+
size_t size() const {
46+
return len;
47+
}
48+
49+
const T *begin() {
50+
return array;
51+
}
52+
53+
const T *end() {
54+
return array + len;
55+
}
56+
57+
};
58+
59+
}
60+
61+
#endif

src/vector

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <func_exception>
2525
#include <algorithm>
2626
#include <type_traits>
27-
27+
#include <initializer_list>
2828

2929
#ifndef __STD_HEADER_VECTOR
3030
#define __STD_HEADER_VECTOR
@@ -94,6 +94,16 @@ namespace std{
9494
}
9595
}
9696

97+
_UCXXEXPORT vector(initializer_list<T> in, const Allocator & al=Allocator()) :
98+
a(al)
99+
{
100+
data_size = in.size() + __UCLIBCXX_STL_BUFFER_SIZE__;
101+
elements = in.size();
102+
data = a.allocate(data_size);
103+
for(size_type i = 0; i < elements; i++)
104+
a.construct(data+i, *(in.begin()+i));
105+
}
106+
97107
_UCXXEXPORT ~vector(); //Below
98108

99109
_UCXXEXPORT vector<T,Allocator>& operator=(const vector<T,Allocator>& x){

0 commit comments

Comments
 (0)