summaryrefslogtreecommitdiff
path: root/Client/Source/Utilities
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Source/Utilities')
-rw-r--r--Client/Source/Utilities/Assert.h11
-rw-r--r--Client/Source/Utilities/AutoInvoke.h27
-rw-r--r--Client/Source/Utilities/Exception.h12
-rw-r--r--Client/Source/Utilities/Singleton.h43
-rw-r--r--Client/Source/Utilities/StaticInitiator.h30
-rw-r--r--Client/Source/Utilities/Type.h25
-rw-r--r--Client/Source/Utilities/UIDGenerator.h20
-rw-r--r--Client/Source/Utilities/UtilMacros.h19
8 files changed, 187 insertions, 0 deletions
diff --git a/Client/Source/Utilities/Assert.h b/Client/Source/Utilities/Assert.h
new file mode 100644
index 0000000..526ca32
--- /dev/null
+++ b/Client/Source/Utilities/Assert.h
@@ -0,0 +1,11 @@
+#ifndef ASSERT_H
+#define ASSERT_H
+
+#include <assert.h>
+
+#define Assert(c) assert((c))
+
+#define DebugAssertIf(c) assert(c)
+#define AssertIf(c) assert(c)
+
+#endif \ No newline at end of file
diff --git a/Client/Source/Utilities/AutoInvoke.h b/Client/Source/Utilities/AutoInvoke.h
new file mode 100644
index 0000000..7d9bb83
--- /dev/null
+++ b/Client/Source/Utilities/AutoInvoke.h
@@ -0,0 +1,27 @@
+#pragma once
+
+typedef void(*AutoInvokeAction)();
+
+// RAII auto call
+class AutoInvokerWhenLeave
+{
+public:
+ AutoInvokerWhenLeave(AutoInvokeAction func)
+ {
+ m_Func = func;
+ };
+ ~AutoInvokerWhenLeave()
+ {
+ if (m_Func)
+ {
+ m_Func();
+ }
+ }
+private:
+ AutoInvokeAction m_Func;
+
+};
+
+#define InvokeWhenLeave(func) \
+AutoInvokerWhenLeave auto_invoker = AutoInvokerWhenLeave(func);
+
diff --git a/Client/Source/Utilities/Exception.h b/Client/Source/Utilities/Exception.h
new file mode 100644
index 0000000..3795bc6
--- /dev/null
+++ b/Client/Source/Utilities/Exception.h
@@ -0,0 +1,12 @@
+#pragma once
+#include <exception>
+
+#define CustomException(Name) \
+class Name : public std::exception \
+{ \
+public: \
+ Name(const char* what) \
+ : std::exception(what) \
+ { \
+ } \
+}
diff --git a/Client/Source/Utilities/Singleton.h b/Client/Source/Utilities/Singleton.h
new file mode 100644
index 0000000..f864a42
--- /dev/null
+++ b/Client/Source/Utilities/Singleton.h
@@ -0,0 +1,43 @@
+#ifndef SINGLETON_H
+#define SINGLETON_H
+
+template<class T>
+class Singleton
+{
+public:
+
+ static T* Instance()
+ {
+ if (!instance) instance = new T;
+ return instance;
+ }
+
+ static void Destroy()
+ {
+ delete instance;
+ instance = nullptr;
+ }
+
+protected:
+
+ Singleton()
+ {
+ instance = static_cast<T*>(this);
+ };
+
+ virtual ~Singleton() {};
+
+ static T* instance;
+
+private:
+
+ Singleton(const Singleton& singleton);
+
+ Singleton& operator = (const Singleton& singleton);
+
+};
+
+template<class T>
+T* Singleton<T>::instance = nullptr;
+
+#endif \ No newline at end of file
diff --git a/Client/Source/Utilities/StaticInitiator.h b/Client/Source/Utilities/StaticInitiator.h
new file mode 100644
index 0000000..af264af
--- /dev/null
+++ b/Client/Source/Utilities/StaticInitiator.h
@@ -0,0 +1,30 @@
+#pragma once
+
+//https://stackoverflow.com/questions/1005685/c-static-initialization-order
+//https://stackoverflow.com/questions/211237/static-variables-initialisation-order
+
+// 静态构造函数
+#include "StaticConstructor/include/StaticConstructor.h"
+
+typedef void(*StaticFunc) ();
+
+// 自动初始化静态数据
+class StaticFuncInvoker
+{
+public:
+ // Default Constructor:
+ StaticFuncInvoker(StaticFunc func)
+ {
+ if (func)
+ func();
+ }
+};
+
+// 调用具名的static函数
+#define InvokeStaticFunc(func)\
+ static StaticFuncInvoker staticInvokerOf_##func(func);
+
+// 用来初始化当前cpp里的静态变量,需要注意static变量初始化顺序(Static Initialization Order)
+#define InitializeStaticVariables(lambda)\
+ static StaticFuncInvoker staticInvoker(lambda);
+
diff --git a/Client/Source/Utilities/Type.h b/Client/Source/Utilities/Type.h
new file mode 100644
index 0000000..da6208d
--- /dev/null
+++ b/Client/Source/Utilities/Type.h
@@ -0,0 +1,25 @@
+#ifndef TYPE_H
+#define TYPE_H
+
+#include <cstdlib>
+#include <stdint.h>
+
+typedef int8_t int8;
+typedef uint8_t uint8;
+typedef unsigned char byte;
+typedef char sbyte;
+typedef int16_t int16;
+typedef uint16_t uint16;
+typedef uint16_t UInt16;
+typedef int32_t int32;
+typedef uint32_t uint32;
+typedef uint32_t UInt32;
+typedef int64_t int64;
+typedef uint64_t uint64;
+typedef uint64_t UInt64;
+
+typedef uint32_t uint;
+typedef int32_t sint;
+typedef int32_t SInt32;
+
+#endif \ No newline at end of file
diff --git a/Client/Source/Utilities/UIDGenerator.h b/Client/Source/Utilities/UIDGenerator.h
new file mode 100644
index 0000000..81918f0
--- /dev/null
+++ b/Client/Source/Utilities/UIDGenerator.h
@@ -0,0 +1,20 @@
+#ifndef UID_GENERATOR_H
+#define UID_GENERATOR_H
+
+class UIDGenerator
+{
+public:
+ UIDGenerator(int from = 0)
+ : m_Index(from)
+ {
+ };
+ ~UIDGenerator(){};
+
+ int GenUID(){return m_Index++;};
+
+private:
+ int m_Index;
+
+};
+
+#endif \ No newline at end of file
diff --git a/Client/Source/Utilities/UtilMacros.h b/Client/Source/Utilities/UtilMacros.h
new file mode 100644
index 0000000..29bb7b0
--- /dev/null
+++ b/Client/Source/Utilities/UtilMacros.h
@@ -0,0 +1,19 @@
+#ifndef UTIL_MACROS_H
+#define UTIL_MACROS_H
+
+#define GET_SET(TYPE,PROP,VAR) \
+ inline void Set##PROP (TYPE val) { VAR = val; } \
+ inline TYPE Get##PROP () {return VAR; }
+
+#define GET(TYPE, PROP, VAR) \
+ inline TYPE Get##PROP () {return VAR; }
+
+#define SET(TYPE, PROP, VAR) \
+ inline void Set##PROP (TYPE val) { VAR = val; } \
+
+#define Mask(v) (1 << v)
+
+#define cast(T, v) \
+((T)(v))
+
+#endif \ No newline at end of file