QuasarAppLib/global.h

137 lines
3.6 KiB
C
Raw Normal View History

2020-05-23 02:13:32 +03:00
/*
* Copyright (C) 2018-2020 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2019-04-02 18:12:01 +03:00
#ifndef GLOBAL_H
#define GLOBAL_H
#include <cstdlib>
#include <type_traits>
2020-05-17 23:21:12 +03:00
#include <string>
2019-04-02 18:12:01 +03:00
template <typename T>
constexpr inline T operator | (T lhs, T rhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(static_cast<int>(lhs) | static_cast<int>(rhs));
}
template <typename T>
constexpr inline T operator & (T lhs, T rhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(static_cast<int>(lhs) & static_cast<int>(rhs));
}
template <typename T>
constexpr inline T operator >> (T lhs, T rhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(static_cast<int>(lhs) >> static_cast<int>(rhs));
}
template <typename T>
constexpr inline T operator << (T lhs, T rhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(static_cast<int>(lhs) << static_cast<int>(rhs));
}
template <typename T>
constexpr inline T operator ~ (T lhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(~static_cast<int>(lhs));
}
template <typename T>
constexpr inline T operator ^ (T lhs, T rhs)
{
static_assert(std::is_enum<T>::value,
"template parameter is not an enum type");
return static_cast<T>(static_cast<int>(lhs) ^ static_cast<int>(rhs));
}
2020-08-25 15:33:38 +03:00
// TO DO need to testing. I am think this is does not work.
//template <class IntegerType>
//constexpr IntegerType static_mul(const char* str, unsigned char depth, unsigned char index = 0) noexcept {
// return (str[index] && (depth > index))? str[index] * static_mul<IntegerType>(str, depth, index + 1) : 1;
//}
//template <class IntegerType>
//constexpr IntegerType static_hash(const char* str, unsigned char index = 0) noexcept {
// return (index && index % sizeof(IntegerType) == 0)?
// static_hash<IntegerType>(str, index + 1) % static_mul<IntegerType>(str, index , index - sizeof(IntegerType)):
// (str[index])?
// static_hash<IntegerType>(str, index + 1):
// static_mul<IntegerType>(str, index, index - (index % sizeof(IntegerType)));
//}
//template<class T>
//uint64_t static_type_hash_64() noexcept {
// return static_hash<uint64_t>(typeid (T).name());
//};
2020-05-22 22:34:06 +03:00
template<class T>
2020-06-30 13:13:08 +03:00
uint32_t static_type_hash_32() noexcept {
2020-05-23 02:12:35 +03:00
return typeid (T).hash_code();
2020-05-22 22:34:06 +03:00
};
template<class T>
2020-06-30 13:13:08 +03:00
uint16_t static_type_hash_16() noexcept {
2020-05-23 02:12:35 +03:00
return typeid (T).hash_code() % 0xFFFF;
2020-05-22 22:34:06 +03:00
};
template<class T>
2020-06-30 13:13:08 +03:00
uint8_t static_type_hash_8() noexcept {
2020-05-23 02:12:35 +03:00
return typeid (T).hash_code() % 0xFF;
2020-05-22 22:34:06 +03:00
};
template<class T>
uint32_t static_type_hash_32(T& object) noexcept {
return typeid (object).hash_code();
};
template<class T>
uint16_t static_type_hash_16(T& object) noexcept {
return typeid (object).hash_code() % 0xFFFF;
};
template<class T>
uint8_t static_type_hash_8(T& object) noexcept {
return typeid (object).hash_code() % 0xFF;
};
2020-05-22 22:36:42 +03:00
#define H_8 static_type_hash_8
#define H_16 static_type_hash_16
#define H_32 static_type_hash_32
2020-05-22 22:34:06 +03:00
2020-08-21 16:03:32 +03:00
#ifdef RELEASE_BUILD
#define debug_assert(condition)
#else
#define debug_assert(condition) assert(condition)
#endif
2019-04-02 18:12:01 +03:00
#endif // GLOBAL_H