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

[#264] cxx bindings for attributes #549

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
conflicts when merging.
-->

* C++ bindings for attributes [#264](https://github.com/eclipse-iceoryx/iceoryx2/issues/264)
* Add Event-Multiplexer `WaitSet` [#390](https://github.com/eclipse-iceoryx/iceoryx2/issues/390)
* Add `PeriodicTimer` into POSIX building blocks [#425](https://github.com/eclipse-iceoryx/iceoryx2/issues/425)
* Developer permissions for resources [#460](https://github.com/eclipse-iceoryx/iceoryx2/issues/460)
Expand Down
4 changes: 4 additions & 0 deletions iceoryx2-ffi/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ target_include_directories(includes-only-cxx
# object lib

add_library(iceoryx2-cxx-object-lib OBJECT
src/attribute.cpp
src/attribute_set.cpp
src/attribute_specifier.cpp
src/attribute_verifier.cpp
src/config.cpp
src/event_id.cpp
src/file_descriptor.cpp
Expand Down
32 changes: 23 additions & 9 deletions iceoryx2-ffi/cxx/include/iox2/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,39 @@
#ifndef IOX2_ATTRIBUTE_HPP
#define IOX2_ATTRIBUTE_HPP

#include "iox/assertions_addendum.hpp"
#include "iox/string.hpp"
#include "iox2/internal/iceoryx2.hpp"

#include <string>

namespace iox2 {
/// Represents a single service attribute (key-value) pair that can be defined when the service
/// is being created.
class Attribute {
public:
using Key = iox::string<IOX2_ATTRIBUTE_KEY_LENGTH>;
using Value = iox::string<IOX2_ATTRIBUTE_VALUE_LENGTH>;
};

/// Represents a single view service attribute (key-value) pair that can be defined when the service
/// is being created.
///
/// @attention The parent from which the view was extracted MUST live longer than the
/// [`AttributeView`].
class AttributeView {
public:
/// Acquires the service attribute key
auto key() const -> Attribute::Key;

/// Acquires the service attribute value
auto value() const -> Attribute::Value;

private:
friend class AttributeSetView;
explicit AttributeView(iox2_attribute_h_ref handle);

auto key() const -> Key {
IOX_TODO();
}
auto value() const -> Value {
IOX_TODO();
}
iox2_attribute_h_ref m_handle;
};
} // namespace iox2
//
auto operator<<(std::ostream& stream, const iox2::AttributeView& value) -> std::ostream&;

#endif
40 changes: 29 additions & 11 deletions iceoryx2-ffi/cxx/include/iox2/attribute_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,44 @@
#ifndef IOX2_ATTRIBUTE_SET_HPP
#define IOX2_ATTRIBUTE_SET_HPP

#include "iox/assertions_addendum.hpp"
#include "iox/vector.hpp"
#include "iox/function.hpp"
#include "iox2/attribute.hpp"
#include "iox2/internal/iceoryx2.hpp"

#include <iostream>

namespace iox2 {
class AttributeSet {
/// Represents all service attributes. They can be set when the service is created.
///
/// @attention The parent from which the view was extracted MUST live longer than the
/// [`AttributeSetView`].
class AttributeSetView {
public:
auto get(const Attribute::Key& key) const -> iox::vector<Attribute::Value, IOX2_MAX_VALUES_PER_ATTRIBUTE_KEY> {
IOX_TODO();
}
};
/// Returns the number of [`Attribute`]s stored inside the [`AttributeSet`].
auto len() const -> uint64_t;

/// Returns a [`AttributeView`] at a specific index. The number of indices is returned via
/// [`AttributeSetView::len()`].
auto at(uint64_t index) const -> AttributeView;
Copy link
Member

Choose a reason for hiding this comment

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

Food for thought. For certification we might need to return an Optional<AttributeView> to not have to terminate for out of bounds access.

But we need to review the whole API for this scenarios, so I think it's fine to stay as is for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or we work with begin() and end() and iterators or for_each and for.

Copy link
Member

Choose a reason for hiding this comment

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

Well, that's a different use case. If someone really wants to access element at index N, then it only works by checking the size before using at ... but that's a different philosophical question


/// Returns all values to a specific key
void get_key_values(const Attribute::Key& key,
const iox::function<CallbackProgression(const Attribute::Value&)>& callback) const;

inline auto operator<<(std::ostream& stream, const AttributeSet& value) -> std::ostream& {
stream << "AttributeSet { }";
return stream;
}
private:
template <ServiceType, typename, typename>
friend class PortFactoryPublishSubscribe;
template <ServiceType>
friend class PortFactoryEvent;
friend class AttributeVerifier;
friend class AttributeSpecifier;

explicit AttributeSetView(iox2_attribute_set_h_ref handle);

iox2_attribute_set_h_ref m_handle;
};
} // namespace iox2

auto operator<<(std::ostream& stream, const iox2::AttributeSetView& value) -> std::ostream&;

#endif
34 changes: 27 additions & 7 deletions iceoryx2-ffi/cxx/include/iox2/attribute_specifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,37 @@
#define IOX2_ATTRIBUTE_SPECIFIER_HPP

#include "attribute_set.hpp"
#include "iox/assertions_addendum.hpp"

namespace iox2 {

/// Represents the set of [`Attribute`]s that are defined when the [`Service`]
/// is created.
class AttributeSpecifier {
public:
auto define(const Attribute::Key& key, const Attribute::Value& value) -> AttributeSpecifier& {
IOX_TODO();
}
auto attributes() const -> AttributeSet& {
IOX_TODO();
}
/// Creates a new empty set of [`Attribute`]s
AttributeSpecifier();
AttributeSpecifier(const AttributeSpecifier&) = delete;
AttributeSpecifier(AttributeSpecifier&&) noexcept;
~AttributeSpecifier();

auto operator=(const AttributeSpecifier&) -> AttributeSpecifier& = delete;
auto operator=(AttributeSpecifier&&) noexcept -> AttributeSpecifier&;

/// Defines a value for a specific key. A key is allowed to have multiple values.
auto define(const Attribute::Key& key, const Attribute::Value& value) -> AttributeSpecifier&&;

/// Returns the underlying [`AttributeSetView`]
auto attributes() const -> AttributeSetView;

private:
template <ServiceType>
friend class ServiceBuilderEvent;
template <typename, typename, ServiceType>
friend class ServiceBuilderPublishSubscribe;

void drop();

iox2_attribute_specifier_h m_handle;
};
} // namespace iox2

Expand Down
53 changes: 35 additions & 18 deletions iceoryx2-ffi/cxx/include/iox2/attribute_verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,50 @@
#ifndef IOX2_ATTRIBUTE_VERIFIER_HPP
#define IOX2_ATTRIBUTE_VERIFIER_HPP

#include "iox/assertions_addendum.hpp"
#include "iox/expected.hpp"
#include "iox/vector.hpp"
#include "iox2/attribute.hpp"
#include "iox2/attribute_set.hpp"
#include "iox2/internal/iceoryx2.hpp"

namespace iox2 {
/// Represents the set of [`Attribute`]s that are required when the [`Service`]
/// is opened.
class AttributeVerifier {
public:
AttributeVerifier() = default;
auto require(const Attribute::Key& key, const Attribute::Value& value) -> AttributeVerifier& {
IOX_TODO();
}
auto require_key(const Attribute::Key& key) -> AttributeVerifier& {
IOX_TODO();
}
auto attributes() const -> const AttributeSet& {
IOX_TODO();
}
auto keys() const -> iox::vector<Attribute::Key, IOX2_MAX_ATTRIBUTES_PER_SERVICE> {
IOX_TODO();
}

auto verify_requirements(const AttributeSet& rhs) const -> iox::expected<void, Attribute::Key> {
IOX_TODO();
}
/// Creates a new empty set of [`Attribute`]s
AttributeVerifier();
AttributeVerifier(const AttributeVerifier&) = delete;
AttributeVerifier(AttributeVerifier&&) noexcept;
~AttributeVerifier();

auto operator=(const AttributeVerifier&) -> AttributeVerifier& = delete;
auto operator=(AttributeVerifier&&) noexcept -> AttributeVerifier&;

/// Requires a value for a specific key. A key is allowed to have multiple values.
auto require(const Attribute::Key& key, const Attribute::Value& value) -> AttributeVerifier&&;

/// Requires that a specific key is defined.
auto require_key(const Attribute::Key& key) -> AttributeVerifier&&;

/// Returns the underlying required [`AttributeSet`]
auto attributes() const -> AttributeSetView;

/// Returns the underlying required keys
auto keys() const -> iox::vector<Attribute::Key, IOX2_MAX_ATTRIBUTES_PER_SERVICE>;

/// Verifies if the [`AttributeSet`] contains all required keys and key-value pairs.
auto verify_requirements(const AttributeSetView& rhs) const -> iox::expected<void, Attribute::Key>;

private:
template <ServiceType>
friend class ServiceBuilderEvent;
template <typename, typename, ServiceType>
friend class ServiceBuilderPublishSubscribe;

void drop();

iox2_attribute_verifier_h m_handle;
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved
};
} // namespace iox2

Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-ffi/cxx/include/iox2/port_factory_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PortFactoryEvent {
auto service_id() const -> const ServiceId&;

/// Returns the attributes defined in the [`Service`]
auto attributes() const -> const AttributeSet&;
auto attributes() const -> AttributeSetView;

/// Returns the StaticConfig of the [`Service`].
/// Contains all settings that never change during the lifetime of the service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PortFactoryPublishSubscribe {
auto service_id() const -> const ServiceId&;

/// Returns the attributes defined in the [`Service`]
auto attributes() const -> const AttributeSet&;
auto attributes() const -> AttributeSetView;

/// Returns the StaticConfig of the [`Service`].
/// Contains all settings that never change during the lifetime of the service.
Expand Down Expand Up @@ -132,8 +132,8 @@ inline auto PortFactoryPublishSubscribe<S, Payload, UserHeader>::service_id() co
}

template <ServiceType S, typename Payload, typename UserHeader>
inline auto PortFactoryPublishSubscribe<S, Payload, UserHeader>::attributes() const -> const AttributeSet& {
IOX_TODO();
inline auto PortFactoryPublishSubscribe<S, Payload, UserHeader>::attributes() const -> AttributeSetView {
return AttributeSetView(iox2_port_factory_pub_sub_attributes(&m_handle));
}

template <ServiceType S, typename Payload, typename UserHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,16 @@ inline auto ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::open_or_crea
required_attributes) && -> iox::expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>,
PublishSubscribeOpenOrCreateError> {
set_parameters();
IOX_TODO();

iox2_port_factory_pub_sub_h port_factory_handle {};
auto result = iox2_service_builder_pub_sub_open_or_create_with_attributes(
m_handle, &required_attributes.m_handle, nullptr, &port_factory_handle);

if (result == IOX2_OK) {
return iox::ok(PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle));
}

return iox::err(iox::into<PublishSubscribeOpenOrCreateError>(result));
}

template <typename Payload, typename UserHeader, ServiceType S>
Expand All @@ -245,15 +254,33 @@ inline auto ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::open_with_at
required_attributes) && -> iox::expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>,
PublishSubscribeOpenError> {
set_parameters();
IOX_TODO();

iox2_port_factory_pub_sub_h port_factory_handle {};
auto result = iox2_service_builder_pub_sub_open_with_attributes(
m_handle, &required_attributes.m_handle, nullptr, &port_factory_handle);

if (result == IOX2_OK) {
return iox::ok(PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle));
}

return iox::err(iox::into<PublishSubscribeOpenError>(result));
}

template <typename Payload, typename UserHeader, ServiceType S>
inline auto ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::create_with_attributes(
const AttributeSpecifier& attributes) && -> iox::expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>,
PublishSubscribeCreateError> {
set_parameters();
IOX_TODO();

iox2_port_factory_pub_sub_h port_factory_handle {};
auto result = iox2_service_builder_pub_sub_create_with_attributes(
m_handle, &attributes.m_handle, nullptr, &port_factory_handle);

if (result == IOX2_OK) {
return iox::ok(PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle));
}

return iox::err(iox::into<PublishSubscribeCreateError>(result));
}
} // namespace iox2

Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-ffi/cxx/include/iox2/static_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace iox2 {
class StaticConfig {
public:
/// Returns the attributes of the [`Service`]
auto attributes() const -> const AttributeSet&;
auto attributes() const -> AttributeSetView;

/// Returns the id of the [`Service`]
auto id() const -> const char*;
Expand Down
39 changes: 39 additions & 0 deletions iceoryx2-ffi/cxx/src/attribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#include "iox2/attribute.hpp"

namespace iox2 {
auto AttributeView::key() const -> Attribute::Key {
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) used as an uninitialized buffer
char buffer[Attribute::Key::capacity()];
iox2_attribute_key(m_handle, &buffer[0], Attribute::Key::capacity());
return { iox::TruncateToCapacity, &buffer[0] };
}
elfenpiff marked this conversation as resolved.
Show resolved Hide resolved

auto AttributeView::value() const -> Attribute::Value {
// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays) used as an uninitialized buffer
char buffer[Attribute::Key::capacity()];
iox2_attribute_value(m_handle, &buffer[0], Attribute::Value::capacity());

return { iox::TruncateToCapacity, &buffer[0] };
}

AttributeView::AttributeView(iox2_attribute_h_ref handle)
: m_handle { handle } {
}
} // namespace iox2

auto operator<<(std::ostream& stream, const iox2::AttributeView& value) -> std::ostream& {
stream << "Attribute { key = \"" << value.key().c_str() << "\", value = \"" << value.value().c_str() << "\" }";
return stream;
}
Loading
Loading