blob: b4edfc67d9508efbe447238ff39ef7e219fd04b5 [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>
30class tvec2 : public TVecArithmeticOperators<tvec2, T>,
31 public TVecUnaryOperators<tvec2, T>,
32 public TVecComparisonOperators<tvec2, T>,
33 public TVecFunctions<tvec2, T>
34{
35public:
36 enum no_init { NO_INIT };
37 typedef T value_type;
38 typedef T& reference;
39 typedef T const& const_reference;
40 typedef size_t size_type;
41
42 union {
43 struct { T x, y; };
44 struct { T s, t; };
45 struct { T r, g; };
46 };
47
48 enum { SIZE = 2 };
49 inline static size_type size() { return SIZE; }
50
51 // array access
52 inline T const& operator [] (size_t i) const { return (&x)[i]; }
53 inline T& operator [] (size_t i) { return (&x)[i]; }
54
55 // -----------------------------------------------------------------------
56 // we don't provide copy-ctor and operator= on purpose
57 // because we want the compiler generated versions
58
59 // constructors
60
61 // leaves object uninitialized. use with caution.
62 explicit tvec2(no_init) { }
63
64 // default constructor
65 tvec2() : x(0), y(0) { }
66
67 // handles implicit conversion to a tvec4. must not be explicit.
68 template<typename A>
69 tvec2(A v) : x(v), y(v) { }
70
71 template<typename A, typename B>
72 tvec2(A x, B y) : x(x), y(y) { }
73
74 template<typename A>
75 explicit tvec2(const tvec2<A>& v) : x(v.x), y(v.y) { }
76};
77
78// ----------------------------------------------------------------------------------------
79
80typedef tvec2<float> vec2;
81
82// ----------------------------------------------------------------------------------------
83}; // namespace android
84
85#endif /* UI_VEC4_H */