Last active
October 13, 2021 03:10
-
-
Save Journeyman1337/58db1ddd237459829a492c5d9fc89714 to your computer and use it in GitHub Desktop.
Vector class using c++20 require keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2021 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#pragma once | |
#include <type_traits> | |
#include <utility> | |
#include <algorithm> | |
#include <array> | |
template<size_t SIZE, typename VALUE_T> | |
requires std::is_arithmetic_v<VALUE_T> && (SIZE > 0) | |
struct Vec | |
{ | |
typedef VALUE_T value_type; | |
const size_t size = SIZE; | |
std::array<VALUE_T, SIZE> data; | |
constexpr Vec() = default; | |
constexpr Vec(const VALUE_T value) | |
{ | |
data.fill(value); | |
} | |
template <typename... Ts> | |
requires std::conjunction_v<std::is_same<VALUE_T, Ts>...> | |
Vec(Ts... values) | |
{ | |
data = { values... }; | |
} | |
VALUE_T& operator[](size_t index) | |
{ | |
if (index >= SIZE) { | |
/// TODO throw | |
} | |
return data[index]; | |
} | |
template<size_t INDEX> | |
requires (INDEX < SIZE) | |
VALUE_T& component() | |
{ | |
return data[INDEX]; | |
} | |
VALUE_T& x() | |
{ | |
return data[0]; | |
} | |
VALUE_T& y() requires(SIZE > 1) | |
{ | |
return data[1]; | |
} | |
VALUE_T& z() requires(SIZE > 2) | |
{ | |
return data[2]; | |
} | |
VALUE_T& w() requires(SIZE > 3) | |
{ | |
return data[3]; | |
} | |
VALUE_T& r() | |
{ | |
return data[0]; | |
} | |
VALUE_T& g() requires(SIZE > 1) | |
{ | |
return data[1]; | |
} | |
VALUE_T& b() requires(SIZE > 2) | |
{ | |
return data[2]; | |
} | |
VALUE_T& a() requires(SIZE > 3) | |
{ | |
return data[3]; | |
} | |
VALUE_T& s() | |
{ | |
return data[0]; | |
} | |
VALUE_T& t() requires(SIZE > 1) | |
{ | |
return data[1]; | |
} | |
VALUE_T& p() requires(SIZE > 2) | |
{ | |
return data[2]; | |
} | |
VALUE_T& q() requires(SIZE > 3) | |
{ | |
return data[3]; | |
} | |
}; | |
#include <iostream> | |
int main() | |
{ | |
Vec<4, float> vec = Vec<4, float>(0.4f, 4.0f, 1.2f, 3.3f); | |
std::cout << vec.b(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment