libostd/ostd/initializer_list.hh

54 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 {
2015-06-04 23:57:06 +02:00
const T *p_buf;
2015-07-13 21:07:14 +02:00
ostd::Size p_len;
2015-07-13 21:07:14 +02:00
constexpr initializer_list(const T *v, ostd::Size n): p_buf(v), p_len(n) {}
2015-06-03 23:56:01 +02:00
public:
2015-06-28 21:39:27 +02:00
constexpr initializer_list(): p_buf(nullptr), p_len(0) {}
2015-06-03 23:56:01 +02:00
2015-07-13 21:07:14 +02:00
constexpr ostd::Size size() const { return p_len; }
2015-06-03 23:56:01 +02:00
2015-06-28 21:39:27 +02:00
constexpr const T *begin() const { return p_buf; }
constexpr const T *end() const { 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
2015-06-04 23:57:06 +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>
PointerRange<const T> iter(std::initializer_list<T> init) {
return PointerRange<const T>(init.begin(), init.end());
2015-06-03 23:56:01 +02:00
}
2015-06-09 22:18:43 +02:00
template<typename T>
PointerRange<const T> citer(std::initializer_list<T> init) {
return PointerRange<const T>(init.begin(), init.end());
2015-06-09 22:18:43 +02:00
}
}
2016-02-07 22:17:15 +01:00
#endif