forked from mbasaglia/Cxx-MiscLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split some libraries from PixelCayman
- Loading branch information
0 parents
commit 6ed44d3
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A C++ library with miscellaneous functionality (header-only) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* \file | ||
* | ||
* \author Mattia Basaglia | ||
* | ||
* \copyright Copyright (C) 2015 Mattia Basaglia | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef MISCLIB_MATH_HPP | ||
#define MISCLIB_MATH_HPP | ||
|
||
#include <utility> | ||
|
||
namespace math { | ||
|
||
/** | ||
* \brief Maximum between two values | ||
*/ | ||
template<class T> | ||
inline constexpr T max(T&& a, T&& b) | ||
{ | ||
return a < b ? b : a; | ||
} | ||
|
||
/** | ||
* \brief Maximum among several values | ||
*/ | ||
template<class T, class...Ts> | ||
inline constexpr T max(T&& a, Ts&&... b) | ||
{ | ||
return max(std::forward<T>(a), max(std::forward<Ts>(b)...)); | ||
} | ||
|
||
/** | ||
* \brief Minimum between two values | ||
*/ | ||
template<class T> | ||
inline constexpr T min(T&& a, T&& b) | ||
{ | ||
return !(a < b) ? b : a; | ||
} | ||
|
||
/** | ||
* \brief Minimum among several values | ||
*/ | ||
template<class T, class...Ts> | ||
inline constexpr T min(T&& a, Ts&&... b) | ||
{ | ||
return min(std::forward<T>(a), min(std::forward<Ts>(b)...)); | ||
} | ||
|
||
/** | ||
* \brief Absolute value | ||
*/ | ||
template<class T> | ||
inline constexpr T abs(T&& x) | ||
{ | ||
return x < 0 ? -x : x; | ||
} | ||
|
||
/** | ||
* \brief Limit \p value to be in [min_value, max_value] | ||
* \pre min_value < max_value | ||
* \post value in [min_value, max_value] | ||
*/ | ||
template<class T> | ||
inline constexpr T bound(T&& min_value, T&& value, T&& max_value) | ||
{ | ||
return max(std::forward<T>(min_value), | ||
min(std::forward<T>(value), std::forward<T>(max_value)) | ||
); | ||
} | ||
|
||
/** | ||
* \brief Normalize a value | ||
* \pre value in [min, max] && min < max | ||
* \post value in [0, 1] | ||
*/ | ||
template<class Real> | ||
inline constexpr Real normalize(Real value, Real min, Real max) | ||
{ | ||
return (value - min) / (max - min); | ||
} | ||
|
||
/** | ||
* \brief Denormalize a value | ||
* \pre value in [0, 1] && min < max | ||
* \post value in [min, max] | ||
*/ | ||
template<class Real> | ||
inline constexpr Real denormalize(Real value, Real min, Real max) | ||
{ | ||
return value * (max - min) + min; | ||
} | ||
|
||
|
||
} // namespace math | ||
#endif // MISCLIB_MATH_HPP |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* \file | ||
* | ||
* \author Mattia Basaglia | ||
* | ||
* \copyright Copyright (C) 2015 Mattia Basaglia | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef MISCLIB_UTIL_HPP | ||
#define MISCLIB_UTIL_HPP | ||
|
||
#include <utility> | ||
#include <tuple> | ||
|
||
namespace util { | ||
|
||
/** | ||
* \brief Utility to get a cleaner syntax when disambiguating function pointers | ||
*/ | ||
template<class ...Args, class C, class R> | ||
auto overload(R (C::*f)(Args...)) -> R (C::*)(Args...) | ||
{ | ||
return f; | ||
} | ||
|
||
/** | ||
* \brief Determine the distance between a data member and its object | ||
*/ | ||
template <typename Class, typename Type> | ||
std::size_t offset_of(const Class* object, Type Class::* member) | ||
{ | ||
return reinterpret_cast<const char *>(&(object->*member)) - | ||
reinterpret_cast<const char *>(object); | ||
} | ||
|
||
/** | ||
* \brief Template to retrieve information about a function signature | ||
* | ||
* Use as FunctionSignature<Ret (Args...)> or FunctionSignature<Pointer> | ||
*/ | ||
template<class T> | ||
struct FunctionSignature; | ||
|
||
template<class Ret, class...Args> | ||
struct FunctionSignature<Ret(Args...)> | ||
{ | ||
using pointer_type = Ret (*) (Args...); | ||
using return_type = Ret; | ||
using arguments_types = std::tuple<Args...>; | ||
}; | ||
|
||
template<class Ret, class...Args> | ||
struct FunctionSignature<Ret(*)(Args...)> | ||
: public FunctionSignature<Ret(Args...)> | ||
{ | ||
}; | ||
|
||
/** | ||
* \brief Clean syntax to get a function pointer type | ||
*/ | ||
template<class T> | ||
using FunctionPointer = typename FunctionSignature<T>::pointer_type; | ||
|
||
} // namespace util | ||
#endif // MISCLIB_UTIL_HPP |