Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support take_from_loaned API #416

Merged
merged 17 commits into from
Mar 3, 2025
Merged
25 changes: 15 additions & 10 deletions include/zenoh/api/base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>

#include "../zenohc.hxx"
#include "detail.hxx"

namespace zenoh {

Expand Down Expand Up @@ -72,8 +73,12 @@ class Owned {
Owned& operator=(Owned&& v) {
if (this != &v) {
::z_drop(::z_move(this->_0));
_0 = v._0;
::z_internal_null(&v._0);
if constexpr (detail::is_take_from_loaned_available_v<OwnedType>) {
::z_take_from_loaned(&this->_0, ::z_loan_mut(v._0));
} else {
_0 = v._0;
::z_internal_null(&v._0);
}
}
return *this;
}
Expand All @@ -84,16 +89,16 @@ class Owned {

explicit Owned(OwnedType* pv) {
if (pv != nullptr) {
_0 = *pv;
::z_internal_null(pv);
} else
if constexpr (detail::is_take_from_loaned_available_v<OwnedType>) {
::z_take_from_loaned(&this->_0, ::z_loan_mut(*pv));
} else {
_0 = *pv;
::z_internal_null(pv);
}
} else {
::z_internal_null(&this->_0);
}
}
};

namespace detail {
struct null_object_t {};
inline constexpr null_object_t null_object{};
} // namespace detail

} // namespace zenoh
54 changes: 54 additions & 0 deletions include/zenoh/api/detail.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would probably make sense to rename this file int something else and move it under dedicated detail folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
#pragma once

#include <optional>
#include <type_traits>

#include "../zenohc.hxx"

// namespace zenoh::detail
namespace zenoh::detail {
template <typename T, typename = void>
struct is_loan_available : std::false_type {};

template <typename T>
struct is_loan_available<T, std::void_t<decltype(::z_loan(std::declval<const T&>()))>> : std::true_type {};

template <typename T>
inline constexpr bool is_loan_available_v = is_loan_available<T>::value;

template <typename T, typename = void>
struct is_loan_mut_available : std::false_type {};

template <typename T>
struct is_loan_mut_available<T, std::void_t<decltype(::z_loan_mut(std::declval<T&>()))>> : std::true_type {};

template <typename T>
inline constexpr bool is_loan_mut_available_v = is_loan_mut_available<T>::value;

template <typename T, typename = void>
struct is_take_from_loaned_available : std::false_type {};

template <typename T>
struct is_take_from_loaned_available<
T, std::void_t<decltype(::z_take_from_loaned(
std::declval<T*>(), std::declval<typename ::z_owned_to_loaned_type_t<T>::type*>()))>> : std::true_type {};

template <typename T>
inline constexpr bool is_take_from_loaned_available_v = is_take_from_loaned_available<T>::value;

struct null_object_t {};
inline constexpr null_object_t null_object{};

} // namespace zenoh::detail
22 changes: 1 addition & 21 deletions include/zenoh/api/interop.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,7 @@
#include <type_traits>

#include "base.hxx"

namespace zenoh::detail {
template <typename T, typename = void>
struct is_loan_available : std::false_type {};

template <typename T>
struct is_loan_available<T, std::void_t<decltype(::z_loan(std::declval<const T&>()))>> : std::true_type {};

template <class T>
inline constexpr bool is_loan_available_v = is_loan_available<T>::value;

template <typename T, typename = void>
struct is_loan_mut_available : std::false_type {};

template <typename T>
struct is_loan_mut_available<T, std::void_t<decltype(::z_loan_mut(std::declval<T&>()))>> : std::true_type {};

template <class T>
inline constexpr bool is_loan_mut_available_v = is_loan_mut_available<T>::value;

} // namespace zenoh::detail
#include "detail.hxx"

namespace zenoh::interop {

Expand Down
2 changes: 1 addition & 1 deletion scripts/build_standalone_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ absolute_install_location=$(cd $1; pwd)

bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/zenohc $absolute_install_location

if [ "$BUILD_PICO" == "ON" ]; then
if [ "$BUILD_PICO" == "ON" ] || [ "$BUILD_PICO" == "TRUE" ] || [ "$BUILD_PICO" == "1" ]; then
#build examples requiring zenoh-pico
bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/universal $absolute_install_location
bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/zenohpico $absolute_install_location
Expand Down
4 changes: 2 additions & 2 deletions scripts/install_from_git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if [ "$#" -ge 3 ]; then
USE_SHARED_MEMORY=$3
fi

if [ "$USE_UNSTABLE" == "TRUE" ]; then
if [ "$USE_UNSTABLE" == "TRUE" ] || [ "$USE_UNSTABLE" == "ON" ] || [ "$USE_UNSTABLE" == "1" ]; then
USE_UNSTABLE_PICO="1"
fi

Expand All @@ -36,7 +36,7 @@ mkdir -p $1
absolute_install_location=$(cd $1; pwd)
#build zenoh-c
bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../zenoh-c $absolute_install_location -DZENOHC_BUILD_WITH_UNSTABLE_API=$USE_UNSTABLE -DZENOHC_BUILD_WITH_SHARED_MEMORY=$USE_SHARED_MEMORY
if [ "$BUILD_PICO" == "ON" ]; then
if [ "$BUILD_PICO" == "ON" ] || [ "$BUILD_PICO" == "TRUE" ] || [ "$BUILD_PICO" == "1" ]; then
#build zenoh-pico
bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../zenoh-pico $absolute_install_location -DZ_FEATURE_UNSTABLE_API=$USE_UNSTABLE_PICO
fi
Expand Down
25 changes: 25 additions & 0 deletions tests/universal/details.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#include "zenoh.hxx"

using namespace zenoh;

static_assert(detail::is_take_from_loaned_available_v<::z_owned_session_t> == false);
static_assert(detail::is_take_from_loaned_available_v<::z_owned_hello_t>);
static_assert(detail::is_take_from_loaned_available_v<::z_owned_sample_t>);
static_assert(detail::is_take_from_loaned_available_v<::z_owned_reply_t>);
static_assert(detail::is_take_from_loaned_available_v<::z_owned_query_t>);

int main() { return 0; }
Loading