rexylib/include/rexy/cx/utility.hpp
2020-05-07 11:54:56 -07:00

82 lines
2.3 KiB
C++

/**
This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_CX_UTILITY_HPP
#define REXY_CX_UTILITY_HPP
#include <utility> //forward, move
#include <type_traits>
namespace rexy::cx{
namespace{
template<class T>
constexpr void swap(T& l, T& r)
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
{
T tmp = std::move(l);
l = std::move(r);
r = std::move(tmp);
}
template<class T, class U = T>
constexpr T exchange(T& l, U&& r)
noexcept(std::is_nothrow_assignable<T,U&&>::value &&
std::is_nothrow_move_assignable<T>::value)
{
T old = std::move(l);
l = std::forward<U>(r);
return old;
}
template<class T>
constexpr const T& min(const T& l, const T& r)noexcept{
return l < r ? l : r;
}
template<class T, class Compare>
constexpr const T& min(const T& l, const T& r, Compare cmp)
noexcept(std::is_nothrow_invocable<Compare,const T&,const T&>::value)
{
return cmp(l, r) ? l : r;
}
template<class T>
constexpr const T& max(const T& l, const T& r)noexcept{
return l > r ? l : r;
}
template<class T, class Compare>
constexpr const T& max(const T& l, const T& r, Compare cmp)
noexcept(std::is_nothrow_invocable<Compare,const T&,const T&>::value)
{
return cmp(l, r) ? l : r;
}
constexpr size_t strlen(const char* c)noexcept{
size_t i = 0;
for(;c[i];++i);
return i;
}
constexpr int strcmp(const char* l, const char* r)noexcept{
using uchar = unsigned char;
for(;*l == *r && *l;++l, ++r);
return (static_cast<uchar>(*l)) - (static_cast<uchar>(*r));
}
}
}
#endif