nndeploy C++ API  0.2.0
nndeploy C++ API
edge.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_DAG_EDGE_H_
3 #define _NNDEPLOY_DAG_EDGE_H_
4 
5 #include "nndeploy/base/common.h"
7 #include "nndeploy/base/log.h"
8 #include "nndeploy/base/macro.h"
9 #include "nndeploy/base/object.h"
11 #include "nndeploy/base/param.h"
12 #include "nndeploy/base/status.h"
13 #include "nndeploy/dag/base.h"
14 #include "nndeploy/dag/edge/abstract_edge.h"
15 #include "nndeploy/device/buffer.h"
16 #include "nndeploy/device/device.h"
18 #include "nndeploy/device/tensor.h"
19 
20 namespace nndeploy {
21 namespace dag {
22 
36  public:
42  Edge();
43 
49  Edge(const std::string &name);
50 
57  virtual ~Edge();
58 
64  std::string getName();
65 
80  base::Status setQueueMaxSize(int queue_max_size);
81 
88 
101  int drop_count = 1);
102 
109 
116 
129 
136 
143  bool empty();
144 
154 
163  base::Status set(device::Buffer *buffer, bool is_external = true);
164 
172 
181  const device::BufferDesc &desc);
182 
190 
198 
205 
206 #ifdef ENABLE_NNDEPLOY_OPENCV
214  base::Status set(cv::Mat *cv_mat, bool is_external = true);
215 
222  base::Status set(cv::Mat &cv_mat);
223 
233  cv::Mat *create(int rows, int cols, int type, const cv::Vec3b &value);
234 
241  bool notifyWritten(cv::Mat *cv_mat);
242 
249  cv::Mat *getCvMat(const Node *node);
250 
256  cv::Mat *getGraphOutputCvMat();
257 #endif
258 
266  base::Status set(device::Tensor *tensor, bool is_external = true);
267 
275 
285  std::string tensor_name = "");
286 
294 
302 
309 
317  base::Status set(base::Param *param, bool is_external = true);
318 
326 
335  template <typename T, typename... Args,
336  typename std::enable_if<std::is_base_of<base::Param, T>::value,
337  int>::type = 0>
338  base::Param *create(Args &&...args) {
339  this->setTypeInfo<T>();
340  return abstact_edge_->create<T>(std::forward<Args>(args)...);
341  }
342 
350 
357  base::Param *getParam(const Node *node);
358 
365 
372 
381  template <typename T>
382  base::Status set(T *t, bool is_external = true) {
383  this->setTypeInfo<T>();
384  return abstact_edge_->set<T>(t, is_external);
385  }
386 
394  template <typename T>
396  this->setTypeInfo<T>();
397  return this->set(&t, true);
398  }
399 
400  template <typename T>
401  base::Status set(T *t, std::function<void(T *)> deleter) {
402  this->setTypeInfo<T>();
403  return abstact_edge_->set<T>(t, deleter);
404  }
405 
414  template <typename T, typename... Args>
415  T *create(Args &&...args) {
416  this->setTypeInfo<T>();
417  return abstact_edge_->create<T>(std::forward<Args>(args)...);
418  }
419 
427  template <typename T>
428  bool notifyWritten(T *t) {
429  return abstact_edge_->notifyWritten<T>(t);
430  }
431 
442  template <typename T>
443  T *get(const Node *node) {
444  if (getParallelType() == base::ParallelType::kParallelTypePipeline) {
445  std::unique_lock<std::mutex> lock(type_info_mutex_);
446  type_info_cv_.wait(lock, [this]() { return type_info_ != nullptr; });
447  }
448  if (type_info_ != nullptr && !type_info_->isType<T>()) {
449  // NNDEPLOY_LOGE("typeid(T) is not *type_info_");
450  return nullptr;
451  }
452  return abstact_edge_->get<T>(node);
453  }
454 
464  template <typename T>
466  if (getParallelType() == base::ParallelType::kParallelTypePipeline) {
467  std::unique_lock<std::mutex> lock(type_info_mutex_);
468  type_info_cv_.wait(lock, [this]() { return type_info_ != nullptr; });
469  }
470  if (type_info_ != nullptr && !type_info_->isType<T>()) {
471  // NNDEPLOY_LOGE("typeid(T) is not *type_info_");
472  return nullptr;
473  }
474  return abstact_edge_->getGraphOutput<T>();
475  }
476 
487  template <typename PY_WRAPPER, typename T>
488  base::Status set4py(PY_WRAPPER *wrapper, T *t, bool is_external = true) {
489  this->setTypeInfo<T>();
490  return abstact_edge_->set4py<PY_WRAPPER, T>(wrapper, t, is_external);
491  }
492 
499  int64_t getIndex(const Node *node);
500 
507 
511  void resetIndex();
512 
519  int getPosition(const Node *node);
520 
527 
535 
543 
550  base::Status increaseProducers(std::vector<Node *> &producers);
551 
558  base::Status increaseConsumers(std::vector<Node *> &consumers);
559 
565  std::vector<Node *> getProducers();
566 
572  std::vector<Node *> getConsumers();
573 
580 
587  template <typename T>
589  if (type_info_ == nullptr) {
590  type_info_ = std::make_shared<EdgeTypeInfo>();
591  type_info_->setType<T>();
592  } else {
593  type_info_->setType<T>();
594  }
595  if (getParallelType() == base::ParallelType::kParallelTypePipeline) {
596  type_info_cv_.notify_all();
597  }
598  return base::kStatusCodeOk;
599  }
600 
607  base::Status setTypeInfo(std::shared_ptr<EdgeTypeInfo> type_info);
608 
614  std::shared_ptr<EdgeTypeInfo> getTypeInfo();
615 
621  void setTypeName(const std::string &type_name);
622 
628  std::string getTypeName();
629 
636  template <typename T>
637  bool checkTypeInfo() {
638  EdgeTypeInfo other_type_info;
639  other_type_info.setType<T>();
640  return *type_info_ == other_type_info;
641  }
642 
649  bool checkTypeInfo(std::shared_ptr<EdgeTypeInfo> type_info);
650 
651  private:
652  std::string name_;
653  AbstractEdge *abstact_edge_ =
654  nullptr;
655  std::mutex type_info_mutex_;
656  std::condition_variable
657  type_info_cv_;
658  std::shared_ptr<EdgeTypeInfo> type_info_;
659  int queue_max_size_ = 16;
661  base::QueueOverflowPolicy queue_overflow_policy_ =
663  int queue_drop_count_ =
664  1;
665 };
666 
667 } // namespace dag
668 } // namespace nndeploy
669 
670 #endif /* _NNDEPLOY_DAG_EDGE_V2_H_ */
输入输出类型信息
Definition: base.h:113
Edge class in DAG graph for connecting nodes and transferring data.
Definition: edge.h:35
base::Status set(device::Buffer *buffer, bool is_external=true)
Set Buffer data to Edge.
int64_t getGraphOutputIndex()
Get data index for graph output.
base::Param * create(Args &&...args)
Create Param object of specified type.
Definition: edge.h:338
T * create(Args &&...args)
Create arbitrary type data object (template version)
Definition: edge.h:415
void setTypeName(const std::string &type_name)
Set type name.
virtual ~Edge()
Virtual destructor.
bool notifyWritten(base::Param *param)
Notify that Param data has been written.
base::Status construct()
Construct the Edge instance.
device::Buffer * create(device::Device *device, const device::BufferDesc &desc)
Create Buffer on specified device.
base::QueueOverflowPolicy getQueueOverflowPolicy()
Get the current queue overflow policy.
base::Status set(device::Tensor &tensor)
Set Tensor reference to Edge.
bool notifyWritten(device::Tensor *tensor)
Notify that Tensor data has been written.
bool checkTypeInfo()
Check if type information matches (template version)
Definition: edge.h:637
bool markGraphOutput()
Mark as graph output Edge.
device::Buffer * getBuffer(const Node *node)
Get Buffer data for specified node.
std::string getTypeName()
Get type name.
device::Tensor * getGraphOutputTensor()
Get Tensor data for graph output.
base::Status setQueueMaxSize(int queue_max_size)
Set the maximum queue capacity.
bool notifyWritten(T *t)
Notify that arbitrary type data has been written (template version)
Definition: edge.h:428
base::Status set(base::Param &param)
Set Param reference to Edge.
void * getGraphOutputPtr()
Get raw pointer for graph output.
bool requestTerminate()
Request termination of Edge processing.
void resetIndex()
Reset index counter.
T * get(const Node *node)
Get arbitrary type data for specified node (template version)
Definition: edge.h:443
T * getGraphOutput()
Get arbitrary type data for graph output (template version)
Definition: edge.h:465
device::Tensor * getTensor(const Node *node)
Get Tensor data for specified node.
base::Status set(base::Param *param, bool is_external=true)
Set Param data to Edge.
base::Status increaseConsumers(std::vector< Node * > &consumers)
Add consumer nodes.
base::Param * getParam(const Node *node)
Get Param data for specified node.
base::ParallelType getParallelType()
Get the current parallel type.
std::string getName()
Get the name of the Edge.
std::vector< Node * > getConsumers()
Get all consumer nodes.
base::Status set(device::Tensor *tensor, bool is_external=true)
Set Tensor data to Edge.
Edge(const std::string &name)
Constructor with name.
std::vector< Node * > getProducers()
Get all producer nodes.
base::Status setTypeInfo(std::shared_ptr< EdgeTypeInfo > type_info)
Set type information of Edge.
int64_t getIndex(const Node *node)
Get data index for specified node.
base::Status setParallelType(const base::ParallelType &paralle_type)
Set the parallel type.
bool notifyWritten(device::Buffer *buffer)
Notify that Buffer data has been written.
int getGraphOutputPosition()
Get position of graph output in queue.
bool checkTypeInfo(std::shared_ptr< EdgeTypeInfo > type_info)
Check if type information matches.
base::Status set4py(PY_WRAPPER *wrapper, T *t, bool is_external=true)
Data setting interface dedicated for Python binding.
Definition: edge.h:488
base::Status set(T *t, bool is_external=true)
Set arbitrary type data to Edge (template version)
Definition: edge.h:382
base::Status setTypeInfo()
Set type information of Edge (template version)
Definition: edge.h:588
base::Status increaseProducers(std::vector< Node * > &producers)
Add producer nodes.
device::Buffer * getGraphOutputBuffer()
Get Buffer data for graph output.
int getQueueDropCount()
Get the queue drop count.
std::shared_ptr< EdgeTypeInfo > getTypeInfo()
Get type information of Edge.
device::Tensor * create(device::Device *device, const device::TensorDesc &desc, std::string tensor_name="")
Create Tensor on specified device.
base::Status set(T &t)
Set arbitrary type data reference to Edge (template version)
Definition: edge.h:395
int getPosition(const Node *node)
Get position of specified node in queue.
Edge()
Default constructor.
base::Status set(device::Buffer &buffer)
Set Buffer reference to Edge.
base::Status setQueueOverflowPolicy(base::QueueOverflowPolicy policy, int drop_count=1)
Set the queue overflow policy.
base::EdgeUpdateFlag update(const Node *node)
Update Edge status for specified node.
base::Param * getGraphOutputParam()
Get Param data for graph output.
base::Status set(T *t, std::function< void(T *)> deleter)
Definition: edge.h:401
bool empty()
Check if the Edge is empty.
int getQueueMaxSize()
Get the maximum queue capacity.
Node base class.
Definition: node.h:171
设备抽象基类
Definition: device.h:155
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kStatusCodeOk
Definition: status.h:13
@ kQueueOverflowPolicyNodeBackpressure
Definition: common.h:202
@ kParallelTypePipeline
Definition: common.h:357