Vc  1.3.0
SIMD Vector Classes for C++
elementreference.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2016 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_ELEMENTREFERENCE_H_
29 #define VC_COMMON_ELEMENTREFERENCE_H_
30 
31 #include "macros.h"
32 
33 namespace Vc_VERSIONED_NAMESPACE
34 {
35 namespace Detail
36 {
37 template <typename U, typename Accessor = U> class ElementReference
38 {
39  using value_type = typename U::value_type;
40  friend U;
41  friend Accessor;
42  Vc_INTRINSIC ElementReference(U &o, int i) noexcept : index(i), obj(o) {}
43 
44  static constexpr bool get_noexcept =
45  noexcept(Accessor::get(std::declval<U &>(), int()));
46  template <typename T> static constexpr bool set_noexcept()
47  {
48  return noexcept(Accessor::set(std::declval<U &>(), int(), std::declval<T>()));
49  }
50 
51 public:
52  Vc_INTRINSIC ElementReference(const ElementReference &) = delete;
53 
54  Vc_INTRINSIC operator value_type() const noexcept(get_noexcept)
55  {
56  return Accessor::get(obj, index);
57  }
58 
59  template <typename T>
60  Vc_INTRINSIC ElementReference &operator=(T &&x) &&
61  noexcept(noexcept(Accessor::set(std::declval<U &>(), int(), std::declval<T>())))
62  {
63  Accessor::set(obj, index, std::forward<T>(x));
64  return *this;
65  }
66 
67 // TODO: improve with operator.()
68 
69 #define Vc_OP_(op_) \
70  template <typename T, typename R = decltype(std::declval<const value_type &>() \
71  op_ std::declval<T>())> \
72  Vc_INTRINSIC ElementReference &operator op_##=(T &&x) && \
73  noexcept(get_noexcept && noexcept(Accessor::set(std::declval<U &>(), int(), \
74  std::declval<R &&>()))) \
75  { \
76  const value_type &lhs = Accessor::get(obj, index); \
77  Accessor::set(obj, index, lhs op_ std::forward<T>(x)); \
78  return *this; \
79  }
80  Vc_ALL_ARITHMETICS(Vc_OP_);
81  Vc_ALL_SHIFTS(Vc_OP_);
82  Vc_ALL_BINARY(Vc_OP_);
83 #undef Vc_OP_
84 
85  template <typename = void>
86  Vc_INTRINSIC ElementReference &operator++() &&
87  noexcept(noexcept(std::declval<value_type &>() =
88  Accessor::get(std::declval<U &>(), int())) &&
89  set_noexcept<decltype(++std::declval<value_type &>())>())
90  {
91  value_type x = Accessor::get(obj, index);
92  Accessor::set(obj, index, ++x);
93  return *this;
94  }
95 
96  template <typename = void>
97  Vc_INTRINSIC value_type operator++(int) &&
98  noexcept(noexcept(std::declval<value_type &>() =
99  Accessor::get(std::declval<U &>(), int())) &&
100  set_noexcept<decltype(std::declval<value_type &>()++)>())
101  {
102  const value_type r = Accessor::get(obj, index);
103  value_type x = r;
104  Accessor::set(obj, index, ++x);
105  return r;
106  }
107 
108  template <typename = void>
109  Vc_INTRINSIC ElementReference &operator--() &&
110  noexcept(noexcept(std::declval<value_type &>() =
111  Accessor::get(std::declval<U &>(), int())) &&
112  set_noexcept<decltype(--std::declval<value_type &>())>())
113  {
114  value_type x = Accessor::get(obj, index);
115  Accessor::set(obj, index, --x);
116  return *this;
117  }
118 
119  template <typename = void>
120  Vc_INTRINSIC value_type operator--(int) &&
121  noexcept(noexcept(std::declval<value_type &>() =
122  Accessor::get(std::declval<U &>(), int())) &&
123  set_noexcept<decltype(std::declval<value_type &>()--)>())
124  {
125  const value_type r = Accessor::get(obj, index);
126  value_type x = r;
127  Accessor::set(obj, index, --x);
128  return r;
129  }
130 
131 private:
132  int index;
133  U &obj;
134 };
135 } // namespace Detail
136 } // namespace Vc
137 
138 #endif // VC_COMMON_ELEMENTREFERENCE_H_
139 
140 // vim: foldmethod=marker