1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 
3 #if !defined(CPPLINQ_LINQ_SELECT_HPP)
4 #define CPPLINQ_LINQ_SELECT_HPP
5 #pragma once
6 
7 #include <cstddef>
8 
9 namespace cpplinq
10 {
11     template <class Collection, class Selector>
12     class linq_select
13     {
14         typedef typename Collection::cursor
15             inner_cursor;
16     public:
17         struct cursor {
18             typedef typename util::result_of<Selector(typename inner_cursor::element_type)>::type
19                 reference_type;
20             typedef typename std::remove_reference<reference_type>::type
21                 element_type;
22             typedef typename inner_cursor::cursor_category
23                 cursor_category;
24 
cursorcpplinq::linq_select::cursor25             cursor(const inner_cursor& cur, Selector sel) : cur(cur), sel(std::move(sel)) {}
26 
forgetcpplinq::linq_select::cursor27             void forget() { cur.forget(); }
emptycpplinq::linq_select::cursor28             bool empty() const { return cur.empty(); }
inccpplinq::linq_select::cursor29             void inc() { cur.inc(); }
getcpplinq::linq_select::cursor30             reference_type get() const { return sel(cur.get()); }
31 
atbegincpplinq::linq_select::cursor32             bool atbegin() const { return cur.atbegin(); }
deccpplinq::linq_select::cursor33             void dec() { cur.dec(); }
34 
skipcpplinq::linq_select::cursor35             void skip(std::size_t n) { cur.skip(n); }
positioncpplinq::linq_select::cursor36             std::size_t position() const { return cur.position(); }
sizecpplinq::linq_select::cursor37             std::size_t size() const { return cur.size(); }
38         private:
39             inner_cursor    cur;
40             Selector        sel;
41         };
42 
linq_select(const Collection & c,Selector sel)43         linq_select(const Collection& c, Selector sel) : c(c), sel(sel) {}
44 
get_cursor() const45         cursor get_cursor() const { return cursor(c.get_cursor(), sel); }
46 
47     private:
48         Collection c;
49         Selector sel;
50     };
51 
52 }
53 
54 #endif // defined(CPPLINQ_LINQ_SELECT_HPP)
55