microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b661d5f22f396e757eb1de6e1ab28f2a50f0e81b

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

include/ortx_cpp_helper.h

100lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include "ortx_utils.h"
7
8namespace ort_extensions {
9
10template <typename T>
11class OrtxDeleter {
12 public:
13 void operator()(T* p) const {
14 if (p) {
15 OrtxDisposeOnly(p);
16 }
17 }
18};
19
20
21/**
22 * @brief A smart pointer class that manages the lifetime of an OrtxObject.
23 *
24 * This class is derived from std::unique_ptr and provides additional functionality
25 * specific to OrtxObject. It automatically calls the OrtxDeleter to release the
26 * owned object when it goes out of scope.
27 *
28 * @tparam T The type of the object being managed.
29 */
30template <typename T>
31class OrtxObjectPtr : public std::unique_ptr<T, OrtxDeleter<T>> {
32 public:
33 /**
34 * @brief Default constructor.
35 *
36 * Constructs an OrtxObjectPtr with a null pointer.
37 */
38 explicit OrtxObjectPtr(T* ptr=nullptr) : std::unique_ptr<T, OrtxDeleter<T>>(ptr) {}
39
40 /**
41 * @brief Constructor that creates an OrtxObjectPtr from a function call.
42 *
43 * This constructor calls the specified function with the given arguments to
44 * create an OrtxObject. If the function call succeeds, the created object is
45 * owned by the OrtxObjectPtr.
46 *
47 * @tparam TFn The type of the function pointer or function object.
48 * @tparam Args The types of the arguments to be passed to the function.
49 * @param fn The function pointer or function object used to create the OrtxObject.
50 * @param args The arguments to be passed to the function.
51 */
52 template <typename TFn, typename... Args>
53 OrtxObjectPtr(TFn fn, Args&&... args) {
54 OrtxObject* proc = nullptr;
55 err_ = fn(&proc, std::forward<Args>(args)...);
56 if (err_ == kOrtxOK) {
57 this->reset(static_cast<T*>(proc));
58 }
59 }
60
61 template <typename TFn, typename... Args>
62 static OrtxObjectPtr<T> FromCapi(TFn fn, Args&&... args) {
63 OrtxObject* proc = nullptr;
64 extError_t err = fn(&proc, std::forward<Args>(args)...);
65 if (err == kOrtxOK) {
66 return OrtxObjectPtr(static_cast<T*>(proc));
67 }
68 }
69
70 /**
71 * @brief Get the error code associated with the creation of the OrtxObject.
72 *
73 * @return The error code.
74 */
75 extError_t Code() const { return err_; }
76
77 struct PointerAssigner {
78 OrtxObject* obj_{};
79 OrtxObjectPtr<T>& ptr_;
80 PointerAssigner(OrtxObjectPtr<T>& ptr) : ptr_(ptr){};
81
82 ~PointerAssigner() { ptr_.reset(static_cast<T*>(obj_)); };
83
84 operator T**() { return reinterpret_cast<T**>(&obj_); };
85 };
86
87/**
88 * @brief A wrapper function for OrtxObjectPtr that can be used as a function parameter of T**.
89 *
90 * This function creates a PointerAssigner object for the given OrtxObjectPtr. The PointerAssigner
91 * object can be used to assign a pointer value to the OrtxObjectPtr.
92 *
93 */
94 PointerAssigner ToBeAssigned() { return PointerAssigner{*this}; }
95
96 private:
97 extError_t err_ = kOrtxOK; /**< The error code associated with the creation of the OrtxObject. */
98};
99
100} // namespace ort_extensions
101