microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
include/ortx_cpp_helper.h
100lines · modeblame
cbed8fd5Wenbing Li2 years ago | 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 | | |
| 8 | namespace ort_extensions { | |
| 9 | | |
| 10 | template <typename T> | |
| 11 | class OrtxDeleter { | |
| 12 | public: | |
| 13 | void operator()(T* p) const { | |
| 14 | if (p) { | |
| 15 | OrtxDisposeOnly(p); | |
| 16 | } | |
| 17 | } | |
| 18 | }; | |
| 19 | | |
89f9f2e1Wenbing Li1 years ago | 20 | |
cbed8fd5Wenbing Li2 years ago | 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 | */ | |
| 30 | template <typename T> | |
| 31 | class 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 | */ | |
0d804203Wenbing Li1 years ago | 38 | explicit OrtxObjectPtr(T* ptr=nullptr) : std::unique_ptr<T, OrtxDeleter<T>>(ptr) {} |
cbed8fd5Wenbing Li2 years ago | 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 | | |
0d804203Wenbing Li1 years ago | 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 | | |
cbed8fd5Wenbing Li2 years ago | 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 | | |
89f9f2e1Wenbing Li1 years ago | 77 | struct PointerAssigner { |
| 78 | OrtxObject* obj_{}; | |
| 79 | OrtxObjectPtr<T>& ptr_; | |
| 80 | PointerAssigner(OrtxObjectPtr<T>& ptr) : ptr_(ptr){}; | |
cbed8fd5Wenbing Li2 years ago | 81 | |
89f9f2e1Wenbing Li1 years ago | 82 | ~PointerAssigner() { ptr_.reset(static_cast<T*>(obj_)); }; |
cbed8fd5Wenbing Li2 years ago | 83 | |
89f9f2e1Wenbing Li1 years ago | 84 | operator T**() { return reinterpret_cast<T**>(&obj_); }; |
| 85 | }; | |
cbed8fd5Wenbing Li2 years ago | 86 | |
| 87 | /** | |
89f9f2e1Wenbing Li1 years ago | 88 | * @brief A wrapper function for OrtxObjectPtr that can be used as a function parameter of T**. |
cbed8fd5Wenbing Li2 years ago | 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 | */ | |
89f9f2e1Wenbing Li1 years ago | 94 | PointerAssigner ToBeAssigned() { return PointerAssigner{*this}; } |
| 95 | | |
| 96 | private: | |
| 97 | extError_t err_ = kOrtxOK; /**< The error code associated with the creation of the OrtxObject. */ | |
cbed8fd5Wenbing Li2 years ago | 98 | }; |
| 99 | | |
| 100 | } // namespace ort_extensions |