refactor: update project

- add documentation CMake build script. re-organise document layout for future changes.
- move LIBCMO_EXPORT to BMap and rename it to BMAP_EXPORT because only BMap need to use this macro.
- fully refactor VTEncoding to make it more like Python
	- Now language name is platform independent.
	- Hide implementation detail as possible as I can.
	- Language mapping are still work in progress.
- add code gen for new added universal encoding feature to generate language name mapping in Windows and Iconv respectively.
- remove old code of CMake build script.
- update VTUtils for new requirement.
	- remove useless functions.
	- create LibCmo specific custom exception classes.
This commit is contained in:
2024-08-16 22:07:23 +08:00
parent afa06339b2
commit f870d4dde3
20 changed files with 1013 additions and 726 deletions

View File

@ -1,97 +1,69 @@
#pragma once
// Check LibCmo build type first.
#if !(defined(LIBCMO_BUILD_DEBUG) ^ defined(LIBCMO_BUILD_RELEASE))
#error "You must define ONE of LIBCMO_BUILD_DEBUG and LIBCMO_BUILD_RELEASE to indicate build type!"
#endif
// https://stackoverflow.com/questions/2164827/explicitly-exporting-shared-library-functions-in-linux
// generate import export macro.
// these macro is not used by LibCmo because LibCmo is static library
// these macro may used by other project such as BMap.
#if defined(_MSC_VER)
// Microsoft
#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
// Include YYCC helper library
#include <YYCCommonplace.hpp>
// 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
#if defined(_WIN32)
#define LIBCMO_OS_WIN32
// disable annoy win32 macro
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#endif
#include <cstdio>
#include <type_traits>
#include <cinttypes>
#include <cstdint>
#include <initializer_list>
// Header for this namespace implementation
#include <string>
#pragma region Batch Ctor operator= Operations
#define LIBCMO_DISABLE_COPY_MOVE(CLSNAME) \
CLSNAME(const CLSNAME&) = delete; \
CLSNAME(CLSNAME&&) = delete; \
CLSNAME& operator=(const CLSNAME&) = delete; \
CLSNAME& operator=(CLSNAME&&) = delete;
#define LIBCMO_DEFAULT_COPY_MOVE(CLSNAME) \
CLSNAME(const CLSNAME&) = default; \
CLSNAME(CLSNAME&&) = default; \
CLSNAME& operator=(const CLSNAME&) = default; \
CLSNAME& operator=(CLSNAME&&) = default;
#pragma endregion
#define LIBCMO_UNUSED [[maybe_unused]]
#include <type_traits>
#include <initializer_list>
namespace LibCmo {
[[noreturn]] void LibPanic(int line, const char* file, const char* errmsg);
#define LIBCMO_PANIC(msg) LibCmo::LibPanic(__LINE__, __FILE__, msg);
#pragma region LibCmo Exceptions
void LibOrderDebugger();
#if defined(LIBCMO_BUILD_DEBUG)
/**
This macro only available in Debug mode.
It will order debugger stop.
This macro frequently used when program entering some rarely area.
For example, in CKStateChunk::ReadObjectID, if code run into the calling of Skip, it mean that this file is pretty old and debugger should notice it.
* @brief The exception raised when library entering unreachable scope.
* @details This exception usually used in \c switch syntax.
* It means that program enter the scope which it definitely can not enter.
*/
#define LIBCMO_ORDER_DEBUGGER LibOrderDebugger();
#else
// define a blank one
#define LIBCMO_ORDER_DEBUGGER
#endif
class UnreachableException : public std::exception {
public:
UnreachableException(const char* msg) : message(msg ? msg : "") {}
UnreachableException(const UnreachableException& rhs) : message(rhs.message) {}
virtual ~UnreachableException() {}
[[nodiscard]] virtual const char* what() const override { return message.c_str(); }
private:
std::string message;
};
/**
* @brief The exception raised when library entering logic error.
* @details In theory, this exception can be found by reading code.
* It usually caused by programmer use functions in a wrong way.
* For example, pass invalid argument to function and etc.
*/
class LogicException : public std::exception {
public:
LogicException(const char* msg) : message(msg ? msg : "") {}
LogicException(const LogicException& rhs) : message(rhs.message) {}
virtual ~LogicException() {}
[[nodiscard]] virtual const char* what() const override { return message.c_str(); }
private:
std::string message;
};
/**
* @brief The exception raised when library entering runtime error.
* @details In theory, this exception can not be found by reading code.
* It may caused by user input or anything else.
*/
class RuntimeException : public std::exception {
public:
RuntimeException(const char* msg) : message(msg ? msg : "") {}
RuntimeException(const RuntimeException& rhs) : message(rhs.message) {}
virtual ~RuntimeException() {}
[[nodiscard]] virtual const char* what() const override { return message.c_str(); }
private:
std::string message;
};
#pragma endregion
namespace EnumsHelper {
@ -100,9 +72,10 @@ namespace LibCmo {
*/
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;
using ut = std::underlying_type_t<TEnum>;
ut result = 0;
for (auto it = il.begin(); it != il.end(); ++it) {
result |= static_cast<std::underlying_type_t<TEnum>>(*it);
result |= static_cast<ut>(*it);
}
return static_cast<TEnum>(result);
}
@ -112,7 +85,8 @@ namespace LibCmo {
*/
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)));
using ut = std::underlying_type_t<TEnum>;
return static_cast<TEnum>(~(static_cast<ut>(e)));
}
/**
@ -120,7 +94,8 @@ namespace LibCmo {
*/
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline void Rm(TEnum& e1, TEnum e2) {
e1 = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(e1) & static_cast<std::underlying_type_t<TEnum>>(Inv(e2)));
using ut = std::underlying_type_t<TEnum>;
e1 = static_cast<TEnum>(static_cast<ut>(e1) & static_cast<ut>(Inv(e2)));
}
/**
@ -128,7 +103,8 @@ namespace LibCmo {
*/
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline void Mask(TEnum& e1, TEnum e2) {
e1 = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(e1) & static_cast<std::underlying_type_t<TEnum>>(e2));
using ut = std::underlying_type_t<TEnum>;
e1 = static_cast<TEnum>(static_cast<ut>(e1) & static_cast<ut>(e2));
}
/**
@ -136,7 +112,8 @@ namespace LibCmo {
*/
template<typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
inline void Add(TEnum& e1, TEnum e2) {
e1 = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(e1) | static_cast<std::underlying_type_t<TEnum>>(e2));
using ut = std::underlying_type_t<TEnum>;
e1 = static_cast<TEnum>(static_cast<ut>(e1) | static_cast<ut>(e2));
}
/**
@ -144,7 +121,8 @@ namespace LibCmo {
*/
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));
using ut = std::underlying_type_t<TEnum>;
return static_cast<bool>(static_cast<ut>(e) & static_cast<ut>(probe));
}
}