microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
nodeps

Branches

Tags

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

Clone

HTTPS

Download ZIP

include/ortx_cpp_helper.h

100lines · modeblame

cbed8fd5Wenbing Li2 years ago1// 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 {
12public:
13void operator()(T* p) const {
14if (p) {
15OrtxDisposeOnly(p);
16}
17}
18};
19
89f9f2e1Wenbing Li1 years ago20
cbed8fd5Wenbing Li2 years ago21/**
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>> {
32public:
33/**
34* @brief Default constructor.
35*
36* Constructs an OrtxObjectPtr with a null pointer.
37*/
0d804203Wenbing Li1 years ago38explicit OrtxObjectPtr(T* ptr=nullptr) : std::unique_ptr<T, OrtxDeleter<T>>(ptr) {}
cbed8fd5Wenbing Li2 years ago39
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*/
52template <typename TFn, typename... Args>
53OrtxObjectPtr(TFn fn, Args&&... args) {
54OrtxObject* proc = nullptr;
55err_ = fn(&proc, std::forward<Args>(args)...);
56if (err_ == kOrtxOK) {
57this->reset(static_cast<T*>(proc));
58}
59}
60
0d804203Wenbing Li1 years ago61template <typename TFn, typename... Args>
62static OrtxObjectPtr<T> FromCapi(TFn fn, Args&&... args) {
63OrtxObject* proc = nullptr;
64extError_t err = fn(&proc, std::forward<Args>(args)...);
65if (err == kOrtxOK) {
66return OrtxObjectPtr(static_cast<T*>(proc));
67}
68}
69
cbed8fd5Wenbing Li2 years ago70/**
71* @brief Get the error code associated with the creation of the OrtxObject.
72*
73* @return The error code.
74*/
75extError_t Code() const { return err_; }
76
89f9f2e1Wenbing Li1 years ago77struct PointerAssigner {
78OrtxObject* obj_{};
79OrtxObjectPtr<T>& ptr_;
80PointerAssigner(OrtxObjectPtr<T>& ptr) : ptr_(ptr){};
cbed8fd5Wenbing Li2 years ago81
89f9f2e1Wenbing Li1 years ago82~PointerAssigner() { ptr_.reset(static_cast<T*>(obj_)); };
cbed8fd5Wenbing Li2 years ago83
89f9f2e1Wenbing Li1 years ago84operator T**() { return reinterpret_cast<T**>(&obj_); };
85};
cbed8fd5Wenbing Li2 years ago86
87/**
89f9f2e1Wenbing Li1 years ago88* @brief A wrapper function for OrtxObjectPtr that can be used as a function parameter of T**.
cbed8fd5Wenbing Li2 years ago89*
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*/
89f9f2e1Wenbing Li1 years ago94PointerAssigner ToBeAssigned() { return PointerAssigner{*this}; }
95
96private:
97extError_t err_ = kOrtxOK; /**< The error code associated with the creation of the OrtxObject. */
cbed8fd5Wenbing Li2 years ago98};
99
100} // namespace ort_extensions