4 Star 3 Fork 3

梁欢 / Modular

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Modular

我在实际工作中发现很多类和模块是非常独立和相似的:它们一般都需要有初始化和反初始化的过程。一般做法是在程序初始化的位置对这些类和模块进行集中配置。比如程序中有ModuleA、ModuleE、ModuleF这三个模块,而且这三个模块之间有依赖顺序ModuleE<ModuleF<ModuleA,这时候的初始化过程就是:
ModuleE.Init();
ModuleF.Init();
ModuleA.Init();
如果模块很多,这种初始化过程就显得非常乏味。那么我们又没有更好的办法解决这个问题呢?这就是该项目给出的答案!

Modular能够自动收集C++项目中那些独立的模块,并提供统一的初始化入口。还是上面的那个问题,如果用Modular来解决你只需要做以下几件事情:

###在模块ModuleE的实现文件中加入当前模块的注册信息: #include "SelfModule.h" #include "TypeSelect.h" #include "Modular.h"

/**
* 定义当前模块
*/
static DefModule _Module("ModuleE"
#ifdef Mod_Config
	, "ModuleF;"
#else
	,""
#endif
	, []$(Initializer)(){
		printf("ModuleE::Initializer()\n");
	}
	, []$(Uninitializer)(){
	printf("ModuleE::Uninitializer()\n");
	});

###在模块ModuleF的实现文件中加入当前模块的注册信息: #include "SelfModule.h" #include "TypeSelect.h" #include "Modular.h"

/**
 * 定义当前模块
 */
static DefModule _Module("ModuleF"
#ifdef Mod_Config
	, "ModuleA;"
#else
	,""
#endif
	, []$(Initializer)(){
		printf("ModuleF::Initializer()\n");
	}
	, []$(Uninitializer)(){
		printf("ModuleF::Uninitializer()\n");
	});

###在模块ModuleA的实现文件中加入当前模块的注册信息: #include "SelfModule.h" #include "TypeSelect.h" #include "Modular.h"

/**
 * 定义当前模块
 */
static DefModule _Module("ModuleA"
	,""
	, []$(Initializer)(){
		printf("ModuleA::Initializer()\n");
	}
	, []$(Uninitializer)(){
		printf("ModuleA::Uninitializer()\n");
	});

###当你对每一个独立的模块进行注册后,就可以在应用初始化的位置使用一句话完成注册过程:

SelfModuleList::GetInstance()->Inits();

SelfModuleList::GetInstance()->Uninits();
The MIT License (MIT) Copyright (c) 2015 梁欢 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

以优雅的方式收集软件系统中那些独立且需初始化的类、配置等模块。 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/lvan100/Modular.git
git@gitee.com:lvan100/Modular.git
lvan100
Modular
Modular
master

搜索帮助