1/*
2 *  Copyright 2008-2016 NVIDIA Corporation
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17#include <thrust/iterator/transform_output_iterator.h>
18#include <thrust/iterator/iterator_adaptor.h>
19
20namespace thrust
21{
22
23template <typename OutputIterator, typename UnaryFunction>
24  class transform_output_iterator;
25
26namespace detail
27{
28
29// Proxy reference that uses Unary Functiont o transform the rhs of assigment
30// operator before writing the result to OutputIterator
31template <typename UnaryFunction, typename OutputIterator>
32  class transform_output_iterator_proxy
33{
34  public:
35    __host__ __device__
36    transform_output_iterator_proxy(const OutputIterator& out, UnaryFunction fun) : out(out), fun(fun)
37    {
38    }
39
40    __thrust_exec_check_disable__
41    template <typename T>
42    __host__ __device__
43    transform_output_iterator_proxy operator=(const T& x)
44    {
45      *out = fun(x);
46      return *this;
47    }
48
49  private:
50    OutputIterator out;
51    UnaryFunction fun;
52};
53
54// Compute the iterator_adaptor instantiation to be used for transform_output_iterator
55template <typename UnaryFunction, typename OutputIterator>
56struct transform_output_iterator_base
57{
58    typedef thrust::iterator_adaptor
59    <
60        transform_output_iterator<UnaryFunction, OutputIterator>
61      , OutputIterator
62      , thrust::use_default
63      , thrust::use_default
64      , thrust::use_default
65      , transform_output_iterator_proxy<UnaryFunction, OutputIterator>
66    > type;
67};
68
69// Register trasnform_output_iterator_proxy with 'is_proxy_reference' from
70// type_traits to enable its use with algorithms.
71template <class OutputIterator, class UnaryFunction>
72struct is_proxy_reference<
73    transform_output_iterator_proxy<OutputIterator, UnaryFunction> >
74    : public thrust::detail::true_type {};
75
76} // end detail
77} // end thrust
78
79