blob: c31d0e43e38934c3832acf68a5a8234aa0991c14 [file] [log] [blame]
Mathias Agopian595ea772013-08-21 23:10:41 -07001/*
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
26namespace android {
27// -------------------------------------------------------------------------------------
28
29template <typename T>
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070030class tvec2 : public TVecProductOperators<tvec2, T>,
31 public TVecAddOperators<tvec2, T>,
Mathias Agopian595ea772013-08-21 23:10:41 -070032 public TVecUnaryOperators<tvec2, T>,
33 public TVecComparisonOperators<tvec2, T>,
34 public TVecFunctions<tvec2, T>
35{
36public:
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 };
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.
63 explicit tvec2(no_init) { }
64
65 // default constructor
66 tvec2() : x(0), y(0) { }
67
68 // handles implicit conversion to a tvec4. must not be explicit.
69 template<typename A>
70 tvec2(A v) : x(v), y(v) { }
71
72 template<typename A, typename B>
73 tvec2(A x, B y) : x(x), y(y) { }
74
75 template<typename A>
76 explicit tvec2(const tvec2<A>& v) : x(v.x), y(v.y) { }
Mathias Agopian1d4d8f92013-09-01 21:35:36 -070077
78 template<typename A>
79 tvec2(const Impersonator< tvec2<A> >& v)
80 : x(((const tvec2<A>&)v).x),
81 y(((const tvec2<A>&)v).y) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070082};
83
84// ----------------------------------------------------------------------------------------
85
86typedef tvec2<float> vec2;
87
88// ----------------------------------------------------------------------------------------
89}; // namespace android
90
91#endif /* UI_VEC4_H */