# PanelCollection Context And Emplace **命名空间**: `XCEngine::Editor` **类型**: `method group` **源文件**: `editor/src/panels/PanelCollection.h` ## 签名 ```cpp IEditorContext* GetContext() const; void SetContext(IEditorContext* context); template TPanel& Emplace(Args&&... args); ``` ## 作用 管理 `PanelCollection` 持有的共享 `IEditorContext`,并把新面板构造进容器。 ## 当前实现行为 ### `GetContext()` - 返回当前缓存的上下文指针。 ### `SetContext(context)` - 直接把 `m_context` 更新为传入值。 - 当 `context == nullptr` 时只更新内部字段,不会继续向面板补发。 - 当 `context` 有效时,会遍历当前所有面板: - 仅对尚未持有上下文的面板调用 `panel->SetContext(m_context)` ### `Emplace(...)` - 要求 `TPanel` 必须继承自 [Panel](../Panel/Panel.md)。 - 内部通过 `std::make_unique(...)` 构造新面板。 - 若当前 collection 已持有上下文,会在入容器前先把上下文注入该面板。 - 最终把面板放进 `std::vector>`,并返回具体类型引用。 ## 设计含义 - `PanelCollection` 当前没有复杂 DI 容器,`IEditorContext*` 就是共享服务入口。 - `Emplace(...)` 保证了“先建对象,再按需补上下文,再登记到容器”的顺序。 ## 相关文档 - [PanelCollection](PanelCollection.md) - [Attach / Detach / Clear](Attach-Detach-And-Clear.md) - [Update / DispatchEvent / Render](Update-DispatchEvent-And-Render.md)