KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
ComponentFacade.hpp
[詳解]
1#pragma once
2
3#include <cassert>
4#include <typeinfo>
6#include <string>
7#include <map>
9#include "ComponentBase.hpp"
10
11using std::string;
12using std::map;
14
15namespace Kisaragi_Lib
16{
18 {
19 private:
21 map<string, ComponentBase*> components;
22 map<string, string>destroyID;
23
24 public:
26 {
27 idAssignment.IDHead("Component_");
28 }
29
34
35 /// <summary>
36 /// コンポーネントを作成する
37 /// </summary>
38 /// <typeparam name="T">コンポーネントの型</typeparam>
39 /// <returns>コンポーネントの参照</returns>
40 template <class T>
42 {
43 //ComponentBaseの派生クラスではないなら
44 //TODO 派生クラスを判定外に
45 //if (!(typeid(ComponentBase).before(typeid(T)) || typeid(ComponentBase) == typeid(T)))
46 //{
47 // assert(false);
48 //}
49
50 string tmpID = idAssignment.GetID();
51
52 //既に存在するIDなら
53 assert(components.count(tmpID) == 0);
54
55 //生成
56 components.emplace(tmpID, new T);
57
58 //ID登録
59 components[tmpID]->ID(tmpID);
60
61 components[tmpID]->Start();
62
63 return components[tmpID];
64 }
65
66 /// <summary>
67 /// Component派生クラスを全て破棄する
68 /// </summary>
70 {
71 for (auto& com : components)
72 {
73 delete com.second;
74 }
75
76 components.clear();
77 }
78
79 /// <summary>
80 /// 破棄オブジェクトの登録
81 /// </summary>
82 /// <param name="_id">破棄オブジェクトのID</param>
83 void AddDestroy(string _id)
84 {
85 //破棄オブジェクトの重複がないようにmapに登録
86 destroyID.emplace(_id, _id);
87 }
88
89 /// <summary>
90 /// 破棄オブジェクトの登録.
91 /// </summary>
92 /// <param name="_obj">破棄オブジェクト</param>
94 {
95 AddDestroy(_com->ID());
96 }
97
98 /// <summary>
99 /// コンポーネントを破棄する.
100 /// </summary>
101 void Destroy()
102 {
103 for (auto& id : destroyID)
104 {
105 //指定されたIDが存在しないならプログラム終了
106 assert(components.count(id.second) == 1);
107 //ID解放
108 idAssignment.OpenID(id.second);
109 //オブジェクト破棄
110 delete components[id.second];
111 //要素の解放
112 components.erase(id.second);
113 }
114 //破棄対象IDをリセット
115 destroyID.clear();
116 }
117
118 };
119
120 ComponentFacade& GetComponentFacade();
121}
Definition ComponentBase.hpp:24
string ID() const
Definition ComponentBase.cpp:15
void AddDestroy(string _id)
破棄オブジェクトの登録
Definition ComponentFacade.hpp:83
ComponentFacade()
Definition ComponentFacade.hpp:25
map< string, ComponentBase * > components
Definition ComponentFacade.hpp:21
void FinalizeComponent()
Component派生クラスを全て破棄する
Definition ComponentFacade.hpp:69
IDAssignment idAssignment
Definition ComponentFacade.hpp:20
ComponentBase * Create()
コンポーネントを作成する
Definition ComponentFacade.hpp:41
~ComponentFacade()
Definition ComponentFacade.hpp:30
map< string, string > destroyID
Definition ComponentFacade.hpp:22
void AddDestroy(ComponentBase *_com)
破棄オブジェクトの登録.
Definition ComponentFacade.hpp:93
void Destroy()
コンポーネントを破棄する.
Definition ComponentFacade.hpp:101
ID割り当てクラス
Definition IDAssignment.h:15
Definition Accessor.hpp:110
ComponentFacade & GetComponentFacade()
Definition ComponentFacade.cpp:5