nndeploy C++ API  0.2.0
nndeploy C++ API
base.h
Go to the documentation of this file.
1 #ifndef _NNDEPLOY_DAG_BASE_H_
2 #define _NNDEPLOY_DAG_BASE_H_
3 
4 #include <cstring>
5 #include <iostream>
6 #include <string_view>
7 #include <typeindex>
8 #include <typeinfo>
9 
10 #include "nndeploy/base/common.h"
12 #include "nndeploy/base/log.h"
13 #include "nndeploy/base/macro.h"
14 #include "nndeploy/base/object.h"
16 #include "nndeploy/base/param.h"
17 #include "nndeploy/base/status.h"
18 #include "nndeploy/base/string.h"
19 #include "nndeploy/device/buffer.h"
20 #include "nndeploy/device/device.h"
22 #include "nndeploy/device/tensor.h"
23 namespace nndeploy {
24 namespace dag {
25 
26 enum class NodeType {
27  kNodeTypeInput = 1, // Input node with no outputs
28  kNodeTypeOutput = 2, // Output node with no inputs
29  kNodeTypeIntermediate = 3, // Intermediate node with both inputs and outputs
30 };
31 
32 enum class IOType {
33  kIOTypeNone = 0, // No type
34 
35  // Basic data types
36  kIOTypeBool = 1, // Boolean type
37  kIOTypeNum = 2, // Integer type
38  kIOTypeString = 3, // String type
39 
40  // File types
41  kIOTypeText = 10, // Text file type
42  kIOTypeJson = 11, // JSON file type
43  kIOTypeXml = 12, // XML file type
44  kIOTypeCsv = 13, // CSV file type
45  kIOTypeYaml = 14, // YAML file type
46  kIOTypeBinary = 15, // Binary file type
47 
48  // Media file types
49  kIOTypeImage = 20, // Image file type
50  kIOTypeVideo = 21, // Video file type
51  kIOTypeAudio = 22, // Audio file type
52  kIOTypeCamera = 23, // Camera type
53  kIOTypeMicrophone = 24, // Microphone type
54 
55  // Model
56  kIOTypeModel = 30, // Model file type
57 
58  // Directory type
59  kIOTypeDir = 31, // Directory type
60 
61  // Special types
62  kIOTypeAny = 100, // Any object type
63 };
64 
65 enum class EdgeTypeFlag {
66  kBuffer = 1,
67  kCvMat = 2,
68  kTensor = 4,
69  kParam = 8,
70  kAny = 1 << 30,
71  kNone = 1 << 31,
72 };
73 
74 template <typename T>
75 std::string typeName() {
76 #if defined(__clang__)
77  constexpr auto prefix = std::string_view("[T = ");
78  constexpr auto suffix = "]";
79  constexpr auto function = std::string_view(__PRETTY_FUNCTION__);
80 #elif defined(__GNUC__)
81  constexpr auto prefix = std::string_view("with T = ");
82  constexpr auto suffix = "]";
83  constexpr auto function = std::string_view(__PRETTY_FUNCTION__);
84 #elif defined(_MSC_VER)
85  constexpr auto prefix = std::string_view("type_name<");
86  constexpr auto suffix = ">(void)";
87  constexpr auto function = std::string_view(__FUNCSIG__);
88 #else
89  return std::type_index(typeid(T)).name();
90 #endif
91 
92  const size_t start = function.find(prefix) + prefix.size();
93  const size_t end = function.find(suffix, start);
94  std::string_view type_view = function.substr(start, end - start);
95 
96  // 查找分号位置,如果存在则只返回分号前的部分
97  std::string type_str(type_view);
98  size_t semicolon_pos = type_str.find(';');
99  if (semicolon_pos != std::string::npos) {
100  return type_str.substr(0, semicolon_pos);
101  }
102 
103  return type_str;
104 }
105 
106 extern NNDEPLOY_CC_API std::string removeNamespace(
107  const std::string& type_name_with_namespace);
108 
114  public:
115  EdgeTypeInfo() : type_(EdgeTypeFlag::kNone), type_name_("") {}
116  ~EdgeTypeInfo() = default;
117 
118  EdgeTypeInfo(const EdgeTypeInfo& other) {
119  type_ = other.type_;
120  type_name_ = other.type_name_;
121  type_ptr_ = other.type_ptr_;
122  type_holder_ = other.type_holder_;
123  edge_name_ = other.edge_name_;
124  }
125 
127  if (this != &other) {
128  type_ = other.type_;
129  type_name_ = other.type_name_;
130  type_ptr_ = other.type_ptr_;
131  type_holder_ = other.type_holder_;
132  edge_name_ = other.edge_name_;
133  }
134  return *this;
135  }
136 
137  bool operator==(const EdgeTypeInfo& other) const {
138  return (type_ == other.type_ && type_name_ == other.type_name_ &&
139  type_ptr_ == other.type_ptr_ && edge_name_ == other.edge_name_);
140  }
141 
142  bool operator!=(const EdgeTypeInfo& other) const { return !(*this == other); }
143 
144  template <typename T>
145  void setType() {
146  typedef typename std::decay<T>::type DT;
147  if constexpr (std::is_same<DT, device::Buffer>::value) {
148  type_ = EdgeTypeFlag::kBuffer;
149  // type_name_ = "Buffer";
150  }
151 #ifdef ENABLE_NNDEPLOY_OPENCV
152  else if constexpr (std::is_same<DT, cv::Mat>::value) {
153  type_ = EdgeTypeFlag::kCvMat;
154  // type_name_ = "numpy.ndarray";
155  }
156 #endif
157  else if constexpr (std::is_same<DT, device::Tensor>::value) {
158  type_ = EdgeTypeFlag::kTensor;
159  // type_name_ = "Tensor";
160  } else if constexpr (std::is_base_of<base::Param, DT>::value) {
161  type_ = EdgeTypeFlag::kParam;
162  // type_name_ = "Param";
163  } else {
164  type_ = EdgeTypeFlag::kAny;
165  // type_name_ = std::string(typeName<DT>());
166  }
167  type_name_ = std::string(typeName<DT>());
168  type_ptr_ = &typeid(DT);
169  type_holder_ = std::make_shared<TypeHolder<DT>>();
170  }
171 
172  EdgeTypeFlag getType() const { return type_; }
173 
174  void setTypeName(const std::string& type_name) {
175  // NNDEPLOY_LOGI("setTypeName: %s\n", type_name.c_str());
176  type_name_ = type_name;
177  }
178  std::string getTypeName() const { return removeNamespace(type_name_); }
179  std::string getTypeNameWithNamespace() const { return type_name_; }
180 
181  std::string getUniqueTypeName() {
182  // basic type
183  std::string base_name = type_name_;
184 
185  // timestamp
186  std::string timestamp = base::getUniqueString();
187 
188  // unique string
189  std::stringstream ss;
190  ss << base_name << "_" << timestamp;
191  return ss.str();
192  }
193 
194  const std::type_info* getTypePtr() const { return type_ptr_; }
195 
196  template <typename T>
197  bool isType() const {
198  return (type_ptr_ != nullptr) && (*type_ptr_ == typeid(T));
199  }
200 
201  template <typename T, typename... Args>
202  T* createType(Args&&... args) {
203  if (!isType<T>()) {
204  NNDEPLOY_LOGE("Type mismatch in createType\n");
205  NNDEPLOY_LOGE(" stored=%s\n", type_ptr_->name());
206  NNDEPLOY_LOGE(" requested=%s\n", typeid(T).name());
207  return nullptr;
208  }
209  return new T(std::forward<Args>(args)...);
210  }
211 
212  template <typename T>
213  bool checkType() const {
214  if (type_ptr_ == nullptr) {
215  NNDEPLOY_LOGE("The type info is empty\n");
216  NNDEPLOY_LOGE(" requested=%s\n", typeid(T).name());
217  return false;
218  }
219  if (*type_ptr_ != typeid(T)) {
220  NNDEPLOY_LOGE("The stored type mismatch\n");
221  NNDEPLOY_LOGE(" stored=%s\n", type_ptr_->name());
222  NNDEPLOY_LOGE(" requested=%s\n", typeid(T).name());
223  return false;
224  }
225  return true;
226  }
227 
228  void setEdgeName(const std::string& edge_name) { edge_name_ = edge_name; }
229  std::string getEdgeName() const { return edge_name_; }
230 
231  public:
232  // Type holder base class
233  struct TypeHolderBase {
234  virtual ~TypeHolderBase() = default;
235  };
236 
237  // Type holder for specific type
238  template <typename T>
240  using Type = T;
241  };
242 
244  std::string type_name_;
245  const std::type_info* type_ptr_{nullptr};
246  std::shared_ptr<TypeHolderBase> type_holder_;
247  std::string edge_name_;
248 };
249 
250 extern NNDEPLOY_CC_API std::string nodeTypeToString(NodeType node_type);
252 stringToNodeType(const std::string& node_type_str);
253 extern NNDEPLOY_CC_API std::string ioTypeToString(IOType io_type);
254 extern NNDEPLOY_CC_API IOType stringToIoType(const std::string& io_type_str);
255 
256 // extern NNDEPLOY_CC_API std::string edgeTypeToString(EdgeTypeFlag edge_type);
257 // extern NNDEPLOY_CC_API EdgeTypeFlag
258 // stringToEdgeType(const std::string& edge_type_str);
259 
261  std::string node_name;
262  bool is_running = false;
263  size_t graph_run_size = 0;
264  size_t run_size = 0;
265  size_t completed_size = 0;
266  float cost_time = -1.0f;
267  float average_time = -1.0f;
268  float init_time = -1.0f;
269 
271  : node_name(""),
272  is_running(false),
273  graph_run_size(0),
274  run_size(0),
275  completed_size(0),
276  cost_time(-1.0f),
277  average_time(-1.0f),
278  init_time(-1.0f) {}
279  RunStatus(const std::string& node_name, bool is_running,
280  size_t graph_run_size, size_t run_size, size_t completed_size,
281  float cost_time, float average_time, float init_time)
282  : node_name(node_name),
283  is_running(is_running),
284  graph_run_size(graph_run_size),
285  run_size(run_size),
286  completed_size(completed_size),
287  cost_time(cost_time),
288  average_time(average_time),
289  init_time(init_time) {}
290  RunStatus(const RunStatus& other)
291  : node_name(other.node_name),
292  is_running(other.is_running),
293  graph_run_size(other.graph_run_size),
294  run_size(other.run_size),
295  completed_size(other.completed_size),
296  cost_time(other.cost_time),
297  average_time(other.average_time),
298  init_time(other.init_time) {}
299  RunStatus& operator=(const RunStatus& other) {
300  if (this != &other) {
301  node_name = other.node_name;
302  is_running = other.is_running;
303  graph_run_size = other.graph_run_size;
304  run_size = other.run_size;
305  completed_size = other.completed_size;
306  cost_time = other.cost_time;
307  average_time = other.average_time;
308  init_time = other.init_time;
309  }
310  return *this;
311  }
312 
313  std::string getStatus() {
314  if (is_running) {
315  return "RUNNING";
316  } else if (run_size > 0 && completed_size > 0 &&
317  run_size == completed_size) {
318  return "DONE";
319  } else if (run_size == 0 && completed_size == 0 &&
320  std::abs(init_time + 1.0f) < 1e-6) {
321  return "INITING";
322  } else if (run_size == 0 && completed_size == 0 &&
323  std::abs(init_time) >= 0.0f) {
324  return "INITED";
325  } else {
326  return "IDLE";
327  }
328  }
329 };
330 
331 } // namespace dag
332 } // namespace nndeploy
333 
334 #endif /* _NNDEPLOY_DAG_TYPE_H_ */
输入输出类型信息
Definition: base.h:113
EdgeTypeInfo(const EdgeTypeInfo &other)
Definition: base.h:118
std::string getEdgeName() const
Definition: base.h:229
std::string type_name_
Definition: base.h:244
T * createType(Args &&... args)
Definition: base.h:202
std::string getTypeName() const
Definition: base.h:178
EdgeTypeInfo & operator=(const EdgeTypeInfo &other)
Definition: base.h:126
EdgeTypeFlag type_
Definition: base.h:243
bool checkType() const
Definition: base.h:213
void setTypeName(const std::string &type_name)
Definition: base.h:174
std::string getTypeNameWithNamespace() const
Definition: base.h:179
std::string getUniqueTypeName()
Definition: base.h:181
bool operator!=(const EdgeTypeInfo &other) const
Definition: base.h:142
const std::type_info * getTypePtr() const
Definition: base.h:194
bool isType() const
Definition: base.h:197
void setEdgeName(const std::string &edge_name)
Definition: base.h:228
EdgeTypeFlag getType() const
Definition: base.h:172
bool operator==(const EdgeTypeInfo &other) const
Definition: base.h:137
std::shared_ptr< TypeHolderBase > type_holder_
Definition: base.h:246
const std::type_info * type_ptr_
Definition: base.h:245
std::string edge_name_
Definition: base.h:247
#define NNDEPLOY_LOGE(fmt,...)
Definition: log.h:59
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
std::string getUniqueString()
EdgeTypeFlag
Definition: base.h:65
std::string removeNamespace(const std::string &type_name_with_namespace)
std::string ioTypeToString(IOType io_type)
std::string typeName()
Definition: base.h:75
NodeType stringToNodeType(const std::string &node_type_str)
IOType stringToIoType(const std::string &io_type_str)
std::string nodeTypeToString(NodeType node_type)
base::Status abs(device::Tensor *input, device::Tensor *output)
std::string getStatus()
Definition: base.h:313
RunStatus(const std::string &node_name, bool is_running, size_t graph_run_size, size_t run_size, size_t completed_size, float cost_time, float average_time, float init_time)
Definition: base.h:279
std::string node_name
Definition: base.h:261
RunStatus(const RunStatus &other)
Definition: base.h:290
RunStatus & operator=(const RunStatus &other)
Definition: base.h:299