1 /*
2  * Copyright 2013 The Android Open Source Project
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 #ifndef UI_VEC2_H
18 #define UI_VEC2_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #define TVEC_IMPLEMENTATION
24 #include <ui/TVecHelpers.h>
25 
26 namespace android {
27 // -------------------------------------------------------------------------------------
28 
29 template <typename T>
30 class tvec2 :   public TVecProductOperators<tvec2, T>,
31                 public TVecAddOperators<tvec2, T>,
32                 public TVecUnaryOperators<tvec2, T>,
33                 public TVecComparisonOperators<tvec2, T>,
34                 public TVecFunctions<tvec2, T>
35 {
36 public:
37     enum no_init { NO_INIT };
38     typedef T value_type;
39     typedef T& reference;
40     typedef T const& const_reference;
41     typedef size_t size_type;
42 
43     union {
44         struct { T x, y; };
45         struct { T s, t; };
46         struct { T r, g; };
47     };
48 
49     enum { SIZE = 2 };
size()50     inline static size_type size() { return SIZE; }
51 
52     // array access
53     inline T const& operator [] (size_t i) const { return (&x)[i]; }
54     inline T&       operator [] (size_t i)       { return (&x)[i]; }
55 
56     // -----------------------------------------------------------------------
57     // we don't provide copy-ctor and operator= on purpose
58     // because we want the compiler generated versions
59 
60     // constructors
61 
62     // leaves object uninitialized. use with caution.
tvec2(no_init)63     explicit tvec2(no_init) { }
64 
65     // default constructor
tvec2()66     tvec2() : x(0), y(0) { }
67 
68     // handles implicit conversion to a tvec4. must not be explicit.
69     template<typename A>
tvec2(A v)70     tvec2(A v) : x(v), y(v) { }
71 
72     template<typename A, typename B>
tvec2(A x,B y)73     tvec2(A x, B y) : x(x), y(y) { }
74 
75     template<typename A>
tvec2(const tvec2<A> & v)76     explicit tvec2(const tvec2<A>& v) : x(v.x), y(v.y) { }
77 
78     template<typename A>
tvec2(const Impersonator<tvec2<A>> & v)79     tvec2(const Impersonator< tvec2<A> >& v)
80         : x(((const tvec2<A>&)v).x),
81           y(((const tvec2<A>&)v).y) { }
82 };
83 
84 // ----------------------------------------------------------------------------------------
85 
86 typedef tvec2<float> vec2;
87 
88 // ----------------------------------------------------------------------------------------
89 }; // namespace android
90 
91 #endif /* UI_VEC4_H */
92