libcmo21/LibCmo/VTUtils.hpp

174 lines
5.0 KiB
C++
Raw Normal View History

2023-02-06 15:58:16 +08:00
#pragma once
2023-02-11 15:29:51 +08:00
/*
2023-02-06 15:58:16 +08:00
// https://stackoverflow.com/questions/2164827/explicitly-exporting-shared-library-functions-in-linux
// generate import export macro
#if defined(_MSC_VER)
2023-02-11 15:29:51 +08:00
// Microsoft
2023-02-06 15:58:16 +08:00
#define LIBCMO_RAW_EXPORT __declspec(dllexport)
#define LIBCMO_RAW_IMPORT __declspec(dllimport)
#elif defined(__GNUC__)
// GCC
#define LIBCMO_RAW_EXPORT __attribute__((visibility("default")))
#define LIBCMO_RAW_IMPORT
#elif defined(__clang__)
// GCC
#define LIBCMO_RAW_EXPORT __attribute__((visibility("default")))
#define LIBCMO_RAW_IMPORT
#else
// do nothing and hope for the best?
#define LIBCMO_RAW_EXPORT
#define LIBCMO_RAW_IMPORT
#pragma warning Unknown dynamic link import/export semantics.
#endif
// choosee proper style
#if defined(LIBCMO_EXPORTING)
#define LIBCMO_NAKED_EXPORT LIBCMO_RAW_EXPORT
#else
#define LIBCMO_NAKED_EXPORT LIBCMO_RAW_IMPORT
#endif
// some work for cpp
#if defined(__cplusplus)
#define LIBCMO_EXPORT extern "C" LIBCMO_NAKED_EXPORT
#else
#define LIBCMO_EXPORT LIBCMO_NAKED_EXPORT
#endif
2023-02-11 15:29:51 +08:00
*/
#if defined(_WIN32)
#define LIBCMO_OS_WIN32
2023-08-23 16:04:58 +08:00
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
2023-02-11 15:29:51 +08:00
#endif
2023-02-25 22:58:28 +08:00
#include <cstdio>
2023-02-26 21:48:03 +08:00
#include <type_traits>
#include <cinttypes>
#include <cstdint>
#include <initializer_list>
2023-08-25 17:35:45 +08:00
#include <string>
2023-02-25 22:58:28 +08:00
2023-08-23 16:04:58 +08:00
#pragma region Batch Ctor operator= Operations
2023-02-26 21:48:03 +08:00
2023-08-23 16:04:58 +08:00
#define LIBCMO_DISABLE_COPY_MOVE(CLSNAME) \
CLSNAME(const CLSNAME&) = delete; \
CLSNAME(CLSNAME&&) = delete; \
CLSNAME& operator=(const CLSNAME&) = delete; \
CLSNAME& operator=(CLSNAME&&) = delete;
2023-02-28 14:04:38 +08:00
2023-08-25 17:35:45 +08:00
#define LIBCMO_DEFAULT_COPY_MOVE(CLSNAME) \
CLSNAME(const CLSNAME&) = default; \
CLSNAME(CLSNAME&&) = default; \
CLSNAME& operator=(const CLSNAME&) = default; \
CLSNAME& operator=(CLSNAME&&) = default;
2023-08-22 15:30:26 +08:00
#pragma endregion
2023-02-26 21:48:03 +08:00
2023-08-22 15:30:26 +08:00
namespace LibCmo {
2023-02-26 21:48:03 +08:00
2023-08-25 21:57:22 +08:00
[[noreturn]] void LibPanic(int line, const char* file, const char* errmsg);
#define LIBPANIC(msg) LibCmo::LibPanic(__LINE__, __FILE__, msg);
2023-08-25 17:35:45 +08:00
namespace TypeHelper {
/**
2023-08-26 20:34:51 +08:00
* @brief MKString is a compatible type.
2023-08-25 17:35:45 +08:00
* In some original Virtools case, we need CKSTRING to hold string.
* But CKSTRING is just a pointer and it is very cause memory leak.
* So I invent this. It like CKSTRING but will free memory automatically.
* And it is safe in CKSTRING copying.
* It operate like std::string. Have a function called c_str() to create usable CKSTRING.
* We call it as MKString. (memory-safe CKSTRING.) :pu: =.=
*/
class MKString {
public:
MKString() : m_HasStr(false), m_Str() {}
~MKString() {}
MKString(const char* cstr) : m_HasStr(cstr != nullptr), m_Str(m_HasStr ? cstr : "") {}
MKString& operator=(const char* cstr) {
m_HasStr = cstr != nullptr;
m_Str = m_HasStr ? cstr : "";
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
MKString(const std::string& cstr) : m_HasStr(true), m_Str(cstr) {}
MKString& operator=(const std::string& cstr) {
m_HasStr = true;
m_Str = cstr;
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
MKString(const MKString& rhs) : m_HasStr(rhs.m_HasStr), m_Str(rhs.m_Str) {}
MKString(MKString&& rhs) noexcept : m_HasStr(rhs.m_HasStr), m_Str(std::move(rhs.m_Str)) {
rhs.m_HasStr = false;
}
MKString& operator=(const MKString& rhs) {
m_HasStr = rhs.m_HasStr;
m_Str = rhs.m_Str;
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
MKString& operator=(MKString&& rhs) noexcept {
m_HasStr = rhs.m_HasStr;
m_Str = std::move(rhs.m_Str);
rhs.m_HasStr = false;
2023-08-25 21:57:22 +08:00
return *this;
2023-08-25 17:35:45 +08:00
}
const char* c_str() const {
return m_HasStr ? m_Str.c_str() : nullptr;
}
2023-08-28 17:04:28 +08:00
const std::string& string() const {
return m_Str;
}
2023-08-28 14:18:58 +08:00
const size_t size() const {
return m_HasStr ? m_Str.size() : 0u;
}
2023-08-25 17:35:45 +08:00
private:
bool m_HasStr;
std::string m_Str;
};
}
namespace EnumsHelper {
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline TEnum Merge(std::initializer_list<TEnum> il) {
std::underlying_type_t<TEnum> result = 0;
for (auto it = il.begin(); it != il.end(); ++it) {
result |= static_cast<std::underlying_type_t<TEnum>>(*it);
}
return static_cast<TEnum>(result);
}
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline TEnum Inv(TEnum e) {
return static_cast<TEnum>(~(static_cast<std::underlying_type_t<TEnum>>(e)));
}
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
2023-08-25 21:57:22 +08:00
inline void Rm(TEnum& e1, TEnum e2) {
2023-08-25 17:35:45 +08:00
e1 = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(e1) & static_cast<std::underlying_type_t<TEnum>>(Inv(e2)));
}
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
2023-08-25 21:57:22 +08:00
inline void Add(TEnum& e1, TEnum e2) {
2023-08-25 17:35:45 +08:00
e1 = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(e1) | static_cast<std::underlying_type_t<TEnum>>(e2));
}
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline bool Has(TEnum e, TEnum probe) {
return static_cast<bool>(static_cast<std::underlying_type_t<TEnum>>(e) & static_cast<std::underlying_type_t<TEnum>>(probe));
}
}
2023-08-23 16:04:58 +08:00
2023-08-28 21:21:40 +08:00
//namespace StreamHelper {
2023-02-11 15:29:51 +08:00
2023-08-28 21:21:40 +08:00
// void CopyStream(const void* src, FILE* dest, size_t len);
// void CopyStream(FILE* src, void* dest, size_t len);
2023-02-11 15:29:51 +08:00
2023-08-28 21:21:40 +08:00
//}
2023-02-11 15:29:51 +08:00
}