libostd/ostd/initializer_list.hh

57 lines
1.2 KiB
C++
Raw Normal View History

/* Initializer list support for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_INITIALIZER_LIST_HH
#define OSTD_INITIALIZER_LIST_HH
#include <stddef.h>
2015-07-13 21:08:55 +02:00
#include "ostd/range.hh"
2015-07-13 21:08:55 +02:00
#ifndef OSTD_ALLOW_CXXSTD
/* must be in std namespace otherwise the compiler won't know about it */
namespace std {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-03 23:56:01 +02:00
class initializer_list {
2016-06-23 20:18:35 +02:00
T const *p_buf;
2015-07-13 21:07:14 +02:00
ostd::Size p_len;
2016-09-11 21:17:49 +02:00
constexpr initializer_list(T const *v, ostd::Size n) noexcept:
p_buf(v), p_len(n)
{}
2015-06-03 23:56:01 +02:00
public:
2016-09-11 21:17:49 +02:00
constexpr initializer_list() noexcept: p_buf(nullptr), p_len(0) {}
2015-06-03 23:56:01 +02:00
2016-09-11 21:17:49 +02:00
constexpr ostd::Size size() const noexcept { return p_len; }
2015-06-03 23:56:01 +02:00
2016-09-11 21:17:49 +02:00
constexpr T const *begin() const noexcept { return p_buf; }
constexpr T const *end() const noexcept { return p_buf + p_len; }
2015-06-03 23:56:01 +02:00
};
}
#else
#include <initializer_list>
#endif
2015-07-13 21:07:14 +02:00
namespace ostd {
2015-05-28 20:58:05 +02:00
template<typename T>
using InitializerList = std::initializer_list<T>;
2015-06-03 23:56:01 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
2016-09-11 21:17:49 +02:00
PointerRange<T const> iter(std::initializer_list<T> init) noexcept {
2016-06-23 20:18:35 +02:00
return PointerRange<T const>(init.begin(), init.end());
2015-06-03 23:56:01 +02:00
}
2015-06-09 22:18:43 +02:00
template<typename T>
2016-09-11 21:17:49 +02:00
PointerRange<T const> citer(std::initializer_list<T> init) noexcept {
2016-06-23 20:18:35 +02:00
return PointerRange<T const>(init.begin(), init.end());
2015-06-09 22:18:43 +02:00
}
}
2016-02-07 22:17:15 +01:00
#endif