summaryrefslogtreecommitdiff
path: root/include/mbgl/util/std.hpp
blob: e64820de479417c7926f950daa848eb68b857bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef MBGL_UTIL_STD
#define MBGL_UTIL_STD

#include <memory>
#include <type_traits>
#include <utility>

namespace mbgl {
namespace util {

// C++14 backfill based on http://llvm.org/svn/llvm-project/libcxx/trunk/include/memory

namespace detail {
template<class T> struct unique_type { typedef ::std::unique_ptr<T> single; };
template<class T> struct unique_type<T[]> { typedef ::std::unique_ptr<T[]> unknown_bound; };
template<class T, size_t size> struct unique_type<T[size]> { typedef void known_bound; };
}

template<class T, class... Args>
typename detail::unique_type<T>::single make_unique(Args&&... args) {
    return ::std::unique_ptr<T>(new T(::std::forward<Args>(args)...));
}

template<class T>
typename detail::unique_type<T>::unknown_bound make_unique(size_t size) {
    return ::std::unique_ptr<T>(new typename ::std::remove_extent<T>::type[size]());
}

template<class T, class... Args>
typename detail::unique_type<T>::known_bound make_unique(Args&&...) = delete;



template <typename Container, typename ForwardIterator, typename Predicate>
void erase_if(Container &container, ForwardIterator it, const ForwardIterator end, Predicate pred) {
    while (it != end) {
        if (pred(*it)) {
            container.erase(it++);
        } else {
            ++it;
        }
    }
}

template <typename Container, typename Predicate>
void erase_if(Container &container, Predicate pred) {
    erase_if(container, container.begin(), container.end(), pred);
}

}
}

#endif