dam4rus commented on code in PR #1504: URL: https://github.com/apache/nifi-minifi-cpp/pull/1504#discussion_r1127599440
########## extensions/python/types/BaseTypes.h: ########## @@ -0,0 +1,246 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <concepts> +#include <utility> + +#include "Python.h" + +namespace org::apache::nifi::minifi::extensions::python { + +template<typename T> +concept convertible_to_object = requires { + static_cast<PyObject*>(std::declval<T>()); +}; + +template<typename T> +concept custom_type = requires { + { T::typeObject() } -> std::same_as<PyTypeObject*>; +}; + +template<typename T> +concept holder_type = requires { + typename T::HeldType; +} && custom_type<T>; + +enum class ReferenceType { + BORROWED, + OWNED, +}; + +template<ReferenceType reference_type> +struct ObjectReference { + ObjectReference() = default; + + explicit ObjectReference(PyObject* object) + : object_(object) { + } + + ~ObjectReference() { + decrementRefCount(); + } + + ObjectReference(const ObjectReference& that) + : object_(that.object_) { + incrementRefCount(); + } + + ObjectReference(ObjectReference&& that) + : object_(that.object_) { + that.object_ = nullptr; + } + + ObjectReference& operator=(const ObjectReference& that) { + if (this == &that) { + return *this; + } + + decrementRefCount(); + object_ = that.object_; + incrementRefCount(); + return *this; + } + + ObjectReference& operator=(ObjectReference&& that) { + if (this == &that) { + return *this; + } + + object_ = that.object_; + that.object_ = nullptr; + return *this; + } + + ObjectReference& operator=(PyObject* object) { + decrementRefCount(); + object_ = object; + return *this; + } Review Comment: I was on the fence about creating operators (and constructors) taking a raw `PyObject*` because ownership isn't clear but did so for convenience. These are basically "wrapper" constructors and the caller needs to make sure they increment the ref count before wrapping them in an `ObjectReference` if the API requires it. They also need to be aware whether the `PyObject*` is owned or borrowed when returned by a Python API (should be the only case when `PyObject*` is used) and wrap it in the proper `ObjectReference` type. It's kinda unsafe so maybe being more explicit about it would be better (like with static factory methods) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
