baidu / babylon

High-Performance C++ Fundamental Library
Apache License 2.0
489 stars 61 forks source link

add_vertex和ApplicationContext结合使用 #42

Closed yefengdanqing closed 3 months ago

yefengdanqing commented 3 months ago

你好,我想 在add_vertex的时候通过工厂或者其他方式添加,看到有支持ApplicationContext,这两个怎么结合使用呢??

yefengdanqing commented 3 months ago

{ auto& v = builder.add_vertex([]{ return std::make_unique(); }); v.named_depend("x").to("A"); v.named_depend("y").to("B"); v.named_emit("z").to("C"); }

oathdruid commented 3 months ago

内部版本确实是集成的ApplicationContext功能的,最近刚把ApplicationContext本身开源出来,对应的功能现在应该也具备挪出来的条件了;我看看怎么弄下,内部的使用是类似这样的

GraphBuilder builder;
...
builder.add_vertex("name_in_context");

BABYLON_REGISTER_COMPONENT(CustomProcessor, "name_in_context", GraphProcessor);
yefengdanqing commented 3 months ago

vertex_builder_ptr = &builder.add_vertex([] { auto request_processor = ApplicationContext::instance().get("注册的名字"); return std::unique_ptr(request_processor); });为啥这样获取了之后,build的时候会有问题?

oathdruid commented 3 months ago

get是获取单例,单例不能放到unique_ptr里面托管 ApplicationContext也有create接口,会使用工厂模式

vertex_builder_ptr = &builder.add_vertex([] {
auto request_processor = ApplicationContext::instance().component_accessor().create<GraphProcessor>("注册的名字").release();
return std::unique_ptr(request_processor);
});