Files
OpenVulkano/openVulkanoCpp/Base/Utils.hpp
2020-11-25 21:21:21 +01:00

96 lines
2.2 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <vector>
#include <string>
#include <set>
#include <algorithm>
namespace openVulkanoCpp
{
class Utils
{
public:
static void SetThreadName(const std::string& name);
static inline std::vector<const char*> toCString(const std::vector<std::string>& values)
{
std::vector<const char*> result;
result.reserve(values.size());
for (const auto& string : values) {
result.push_back(string.c_str());
}
return result;
}
static inline std::vector<const char*> toCString(const std::set<std::string>& values)
{
std::vector<const char*> result;
result.reserve(values.size());
for (const auto& string : values) {
result.push_back(string.c_str());
}
return result;
}
template <typename T>
static inline bool Contains(std::vector<T>& vec, const T& element)
{
return (std::find(vec.begin(), vec.end(), element) != vec.end());
}
template <typename T>
static inline void Remove(std::vector<T>& vec, const T& element)
{
vec.erase(std::remove(vec.begin(), vec.end(), element), vec.end());
}
template <typename Enumeration>
static inline auto EnumAsInt(Enumeration const value)
-> typename std::underlying_type<Enumeration>::type
{
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}
static inline constexpr size_t Align(size_t size, size_t alignment)
{
return (size + alignment - 1) & ~(alignment - 1);
}
static inline constexpr size_t AlignPage(size_t size)
{
return Align(size, 4096); //TODO detect system page size instead of relying on hardcoded value
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
static inline constexpr bool IsPow2(T i)
{
return ((i - 1) & i) == 0;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
static inline constexpr T Log2OfPow2(T n)
{
assert(n != 0);
assert(IsPow2(n));
T log = 0;
while(true)
{
n >>= 1;
if (n == 0)
{
break;
}
log++;
}
return log;
}
};
}