Skip to content

Commit

Permalink
Merge pull request #12 from Forairaaaaa/better-extension-api-wrapping
Browse files Browse the repository at this point in the history
better extension api wrapping
  • Loading branch information
Forairaaaaa authored Oct 27, 2024
2 parents 2a2ba6b + 8187b01 commit 3c2d6f8
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ AlwaysBreakAfterReturnType: None
# 总是在多行string字面量前换行
AlwaysBreakBeforeMultilineStrings: true
# 总是在template声明后换行
AlwaysBreakTemplateDeclarations: false
AlwaysBreakTemplateDeclarations: true
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: true
# false表示所有形参要么都在同一行,要么都各自一行
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ std::unique_ptr<AbilityManager> _extension_ability_manager;
mc.installApp(std::make_unique<MyApp>());

// 安装扩展
mc.ExtensionManager()->createAbility(std::make_unique<MyWorker>());
mc.ExtensionManager()->createAbility(std::make_unique<MyUI>());
mc.createExtension(std::make_unique<MyWorker>());
mc.createExtension(std::make_unique<MyUI>());

// 更新 Mooncake
for (int i = 0; i < 3; i++) {
Expand Down
1 change: 1 addition & 0 deletions example/ability_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ int main()
// 超出作用域自动释放
printf(">> test over\n");
am.createAbility(std::make_unique<BasicAbilityTest>());
// >> test over
// [basic] on construct
// [basic] on deconstruct
return 0;
Expand Down
4 changes: 2 additions & 2 deletions example/extension_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ int main()

// 安装扩展
printf(">> install extensions\n");
mc.ExtensionManager()->createAbility(std::make_unique<MyWorker>());
mc.ExtensionManager()->createAbility(std::make_unique<MyUI>());
mc.createExtension(std::make_unique<MyWorker>());
mc.createExtension(std::make_unique<MyUI>());
// >> install extensions
// [worker] on construct
// [ui] on construct
Expand Down
29 changes: 26 additions & 3 deletions src/ability_manager/ability_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
#include "ability_manager.h"
#include "ability/ability.h"
#include <iterator>

using namespace mooncake;

Expand All @@ -25,8 +26,8 @@ int AbilityManager::createAbility(std::unique_ptr<AbilityBase> ability)
new_ability_info.state = StateGoCreate;
new_ability_info.ability = std::move(ability);

// 转移所有权
_ability_list.push_back(std::move(new_ability_info));
// 转移所有权,暂存到 new ability list
_new_ability_list.push_back(std::move(new_ability_info));

return new_ability_info.id;
}
Expand All @@ -41,11 +42,25 @@ bool AbilityManager::destroyAbility(int abilityID)
return true;
}
}
for (auto& ability_info : _new_ability_list) {
if (ability_info.id == abilityID) {
// 更新状态
ability_info.state = StateGoDestroy;
return true;
}
}
return false;
}

void AbilityManager::updateAbilities()
{
// 如果有新添加的 ability
if (!_new_ability_list.empty()) {
// 合并到 ability list
std::move(_new_ability_list.begin(), _new_ability_list.end(), std::back_inserter(_ability_list));
_new_ability_list.clear();
}

// 遍历所有 Abilities
for (auto ability_iter = _ability_list.begin(); ability_iter != _ability_list.end();) {
/* ----------------------------------- 状态机 ---------------------------------- */
Expand Down Expand Up @@ -83,7 +98,7 @@ void AbilityManager::updateAbilities()

std::size_t AbilityManager::getAbilityNum()
{
return _ability_list.size();
return _ability_list.size() + _new_ability_list.size();
}

bool AbilityManager::isAbilityExist(int abilityID)
Expand All @@ -98,6 +113,11 @@ bool AbilityManager::isAbilityExist(int abilityID)
AbilityBase* AbilityManager::getAbilityInstance(int abilityID)
{
// 遍历查找对应 ID 的 Ability
for (auto& ability_info : _new_ability_list) {
if (ability_info.id == abilityID) {
return ability_info.ability.get();
}
}
for (auto& ability_info : _ability_list) {
if (ability_info.id == abilityID) {
return ability_info.ability.get();
Expand All @@ -113,6 +133,9 @@ std::vector<AbilityBase*> AbilityManager::getAllAbilityInstance()
for (auto& ability_info : _ability_list) {
ret.push_back(ability_info.ability.get());
}
for (auto& ability_info : _new_ability_list) {
ret.push_back(ability_info.ability.get());
}

return ret;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ability_manager/ability_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AbilityManager {
/* -------------------------------------------------------------------------- */
/* Ability API Wrapping */
/* -------------------------------------------------------------------------- */
// 对外暴露针对性的 Ability 操作接口
// 常用 Ability 类型的操作接口封装

bool showUIAbility(int abilityID);
bool hideUIAbility(int abilityID);
Expand Down Expand Up @@ -112,6 +112,7 @@ class AbilityManager {
};

std::vector<AbilityInfo_t> _ability_list;
std::vector<AbilityInfo_t> _new_ability_list;
int _next_ability_id = 0;
std::vector<int> _available_ability_id_list;

Expand Down
2 changes: 1 addition & 1 deletion src/mooncake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ AppAbility::State_t Mooncake::getAppCurrentState(int appID)
/* Extension Ability Manager */
/* -------------------------------------------------------------------------- */

AbilityManager* Mooncake::ExtensionManager()
AbilityManager* Mooncake::extensionManager()
{
return get_extension_ability_manager();
}
Expand Down
51 changes: 42 additions & 9 deletions src/mooncake.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ability_manager/ability_manager.h"
#include <cstddef>
#include <memory>
#include <utility>
#include <vector>

#define MOONCAKE_VERSION "2.0.0"
Expand Down Expand Up @@ -121,28 +122,60 @@ class Mooncake {
/* -------------------------------------------------------------------------- */
/* Extension Ability Manager */
/* -------------------------------------------------------------------------- */
// Extension Ability 管理器 API 封装。
// 因为 Extension 可以是任意 Ability 类型,所以直接懒加载,暴露管理器实例就算 9 数了
// 扩展管理器 API 封装。
// 可以用来存放管理自己的各种派生 Ability

/**
* @brief 获取 Extension Ability 管理器实例
* @brief 获取扩展管理器
*
* @return AbilityManager*
*/
AbilityManager* ExtensionManager();
AbilityManager* extensionManager();

/**
* @brief 重置 Extension Ability 管理器,销毁其中所有 Ability
* @brief 重置扩展管理器,销毁其中所有 Ability
*
*/
void resetExtensionManager();

/**
* @brief 在扩展管理器中创建 Ability,返回 Ability ID
*
* @param ability
* @return int
*/
int createExtension(std::unique_ptr<AbilityBase> ability)
{
return extensionManager()->createAbility(std::move(ability));
}

/**
* @brief 在扩展管理器中销毁指定 ID 的 Ability
*
* @param abilityID
* @return true
* @return false
*/
bool destroyExtension(int abilityID)
{
return extensionManager()->destroyAbility(abilityID);
}

/**
* @brief 获取扩展管理器中指定 ID Ability 的实例原始指针,以便外部调用 API,最不安全的一集
*
* @tparam T 扩展 Ability 类型
* @param abilityID Ability ID
* @return T
*/
template <typename T>
T* getExtensionInstance(int abilityID)
{
return static_cast<T*>(extensionManager()->getAbilityInstance(abilityID));
}

private:
// App Ability 管理器:提供集中的 App Ability 管理和信息获取
std::unique_ptr<AbilityManager> _app_ability_manager;

// Extension Ability 管理器:提供集中的杂类 Ability 管理,比如一些后台运行的 Worker,或者某些 App Ability
// 要用到的 UI Ability、自定义 Ability 等,都可以创建在这里,由 Mooncake 集中管理
std::unique_ptr<AbilityManager> _extension_ability_manager;

// 懒加载
Expand Down

0 comments on commit 3c2d6f8

Please sign in to comment.