Skip to content

Commit

Permalink
feat: 增加CMakeLists构建,添加命名空间
Browse files Browse the repository at this point in the history
  • Loading branch information
KondeU committed Nov 25, 2022
1 parent e2496f4 commit 9b834b8
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
build/

# Visual Studio 2015/2017 cache/options directory
.vs/
Expand Down
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.14)
project(GlobalSingleton)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

file(GLOB_RECURSE SRC SingletonManager/*.cpp)
add_library(sgtmgr SHARED ${SRC})

install(TARGETS sgtmgr
ARCHIVE DESTINATION library # .lib/.a
LIBRARY DESTINATION runtime # .dll/.so
RUNTIME DESTINATION runtime # .exe/.out
)
install(DIRECTORY Common/ DESTINATION include)
install(FILES LICENSE.txt DESTINATION .)
install(FILES README.txt DESTINATION .)
14 changes: 13 additions & 1 deletion Common/DllWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <string>
#include <functional>
#include <unordered_map>
#include "NonCopyable.hpp"

#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#define LIB_LIBRARY HMODULE
#define LIB_PROCESS FARPROC
Expand All @@ -28,7 +30,10 @@
#define LIB_PREN "lib"
#endif

class DllWrapper final {
namespace au {
namespace common {

class DllWrapper final : public NonCopyable {
public:
DllWrapper()
{
Expand Down Expand Up @@ -82,7 +87,11 @@ class DllWrapper final {
}

template <typename Func, typename ...Args>
#if (__cplusplus > 201703L) || (_MSVC_LANG > 201703L)
typename std::invoke_result_t<std::function<Func>, Args...>
#else
typename std::result_of<std::function<Func>(Args...)>::type
#endif
ExecuteFunction(const std::string& func, Args&& ...args)
{
auto fn = GetFunction<Func>(func);
Expand All @@ -97,3 +106,6 @@ class DllWrapper final {
LIB_LIBRARY library;
std::unordered_map<std::string, LIB_PROCESS> cache;
};

}
}
6 changes: 6 additions & 0 deletions Common/GlobalSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "GlobalSingletonInstance.hpp"

namespace au {
namespace common {

template <typename T>
class GlobalSingleton : public Singleton<GlobalSingletonInstance<T>> {
public:
Expand All @@ -14,3 +17,6 @@ class GlobalSingleton : public Singleton<GlobalSingletonInstance<T>> {
GlobalSingleton() = default;
~GlobalSingleton() = default;
};

}
}
6 changes: 6 additions & 0 deletions Common/GlobalSingletonContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "Singleton.hpp"
#include "DllWrapper.hpp"

namespace au {
namespace common {

class GlobalSingletonContext final : public Singleton<GlobalSingletonContext> {
public:
// singleton manager dynamic library: sgtmgr.dll or (lib)sgtmgr.so
Expand Down Expand Up @@ -48,3 +51,6 @@ class GlobalSingletonContext final : public Singleton<GlobalSingletonContext> {
private:
DllWrapper manager;
};

}
}
6 changes: 6 additions & 0 deletions Common/GlobalSingletonInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "GlobalSingletonContext.hpp"

namespace au {
namespace common {

template <typename T>
class GlobalSingletonInstance final {
public:
Expand All @@ -24,3 +27,6 @@ class GlobalSingletonInstance final {
private:
T* instance;
};

}
}
6 changes: 6 additions & 0 deletions Common/NonCopyable.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

namespace au {
namespace common {

class NonCopyable {
protected:
NonCopyable() = default;
Expand All @@ -9,3 +12,6 @@ class NonCopyable {
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};

}
}
6 changes: 6 additions & 0 deletions Common/Singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "NonCopyable.hpp"

namespace au {
namespace common {

template <typename T>
class Singleton : public NonCopyable {
public:
Expand All @@ -15,3 +18,6 @@ class Singleton : public NonCopyable {
Singleton() = default;
~Singleton() = default;
};

}
}
2 changes: 1 addition & 1 deletion ExeDllSingleton/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char* argv[])
{
//GlobalTestExe::GetReference().Test(); // 单例由exe加载

DllWrapper dll1, dll2;
au::common::DllWrapper dll1, dll2;
dll1.Load("TestDll1"); // 单例由dll加载
dll2.Load("TestDll2");
dll1.ExecuteFunction<void()>("TestDll1Run");
Expand Down
2 changes: 1 addition & 1 deletion ExeDllSingleton/TestExe.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <iostream>
#include "../Common/GlobalSingleton.hpp"

class GlobalTestExe : public GlobalSingleton<GlobalTestExe> {
class GlobalTestExe : public au::common::GlobalSingleton<GlobalTestExe> {
public:
GlobalTestExe()
{
Expand Down
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Windows使用注意事项:
没有保证多线程间安全,因此在单例实例化/动态库首次获取单例时都是线程不安全的。
若在SingletonManager.cpp的Count/Obtain/Release函数中使用std::mutex加锁能够实现
单例获取的安全,但实例化过程(创建过程)仍是线程不安全的。

C++版本要求:C++14 以上(MSVC v140 (VS2015) 以上,如果使用Visual Studio)。

0 comments on commit 9b834b8

Please sign in to comment.