Home | History | Annotate | Download | only in cpplinq
      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 
     25             cursor(const inner_cursor& cur, Selector sel) : cur(cur), sel(std::move(sel)) {}
     26 
     27             void forget() { cur.forget(); }
     28             bool empty() const { return cur.empty(); }
     29             void inc() { cur.inc(); }
     30             reference_type get() const { return sel(cur.get()); }
     31 
     32             bool atbegin() const { return cur.atbegin(); }
     33             void dec() { cur.dec(); }
     34 
     35             void skip(std::size_t n) { cur.skip(n); }
     36             std::size_t position() const { return cur.position(); }
     37             std::size_t size() const { return cur.size(); }
     38         private:
     39             inner_cursor    cur;
     40             Selector        sel;
     41         };
     42 
     43         linq_select(const Collection& c, Selector sel) : c(c), sel(sel) {}
     44 
     45         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