properties.hh 1.07 KB
Newer Older
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
#pragma once
#include "primitives.hh"
#include "tmp.hh"

namespace polymesh
{
template <class CollectionT, class FuncT>
struct readonly_property
{
    readonly_property(CollectionT collection, FuncT func) : mCollection(collection), mFunc(func) {}

    using index_t = typename std::decay<CollectionT>::type::index_t;
    using handle_t = typename std::decay<CollectionT>::type::handle_t;
    using input_t = decltype(std::declval<CollectionT>().operator[](index_t()));
    using output_t = typename tmp::decayed_result_type_of<FuncT, input_t>;

    output_t operator[](handle_t h) const { return mFunc(mCollection(h)); }
    output_t operator[](index_t h) const { return mFunc(mCollection(h)); }

    output_t operator()(handle_t h) const { return mFunc(mCollection(h)); }
    output_t operator()(index_t h) const { return mFunc(mCollection(h)); }

    int size() const { return mCollection.size(); }

    template <class Func2T>
Philip Trettner's avatar
Philip Trettner committed
26
    auto view(Func2T&& f) const -> readonly_property<readonly_property<CollectionT, FuncT>, Func2T>;
27
28
29
30
31
32

private:
    CollectionT mCollection;
    FuncT mFunc;
};
}