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

WIP changes to cxx api #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
95 changes: 94 additions & 1 deletion iceoryx2-bb/derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
use syn::{parse_macro_input, Data, DeriveInput, Expr, ExprLit, Fields, Lit};

/// Implements the [`iceoryx2_bb_elementary::placement_default::PlacementDefault`] trait when all
/// fields of the struct implement it.
Expand Down Expand Up @@ -99,3 +99,96 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {

TokenStream::from(expanded)
}

#[proc_macro_derive(StaticStringRepresentation, attributes(StaticString))]
pub fn as_static_string_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let static_string_impl = match input.data {
Data::Enum(ref data_enum) => {
let match_arms = data_enum.variants.iter().map(|variant| {
let variant_ident = &variant.ident;

// Get the StaticString attribute if it exists
let static_string = variant
.attrs
.iter()
.find_map(|attr| {
if !attr.path().is_ident("StaticString") {
return None;
}

match attr.meta.require_name_value() {
Ok(meta) => {
if let Expr::Lit(ExprLit {
lit: Lit::Str(lit), ..
}) = &meta.value
{
Some(lit.value())
} else {
None
}
}
_ => None,
}
})
.unwrap_or_else(|| {
// Default to converting variant name to snake_case with spaces
let variant_str = variant_ident.to_string();
variant_str
.chars()
.enumerate()
.map(|(i, c)| {
if i > 0 && c.is_uppercase() {
format!(" {}", c.to_lowercase())
} else {
c.to_lowercase().to_string()
}
})
.collect::<String>()
});

match &variant.fields {
Fields::Unit => {
quote! {
Self::#variant_ident => concat!(#static_string, "\0")
}
}
Fields::Unnamed(_) => {
quote! {
Self::#variant_ident(..) => concat!(#static_string, "\0")
}
}
Fields::Named(_) => {
quote! {
Self::#variant_ident{..} => concat!(#static_string, "\0")
}
}
}
});

quote! {
fn as_static_str(&self) -> &'static str {
match self {
#(#match_arms,)*
}
}
}
}
_ => {
let err =
syn::Error::new_spanned(&input, "AsStaticString can only be derived for enums");
return err.to_compile_error().into();
}
};

let expanded = quote! {
impl #impl_generics AsStaticString for #name #ty_generics #where_clause {
#static_string_impl
}
};

TokenStream::from(expanded)
}
15 changes: 15 additions & 0 deletions iceoryx2-bb/elementary/src/as_static_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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

pub trait AsStaticString {
fn as_static_str(&self) -> &'static str;
}
5 changes: 4 additions & 1 deletion iceoryx2-bb/elementary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#[macro_use]
pub mod enum_gen;

/// A strong type that represents the alignment part of [`std::alloc::Layout`]
mod as_static_string;
pub use as_static_string::*;

pub mod alignment;
pub mod allocator;
/// A strong type that represents the alignment part of [`std::alloc::Layout`]
pub mod bump_allocator;
pub mod lazy_singleton;
pub mod math;
Expand Down
1 change: 1 addition & 0 deletions iceoryx2-ffi/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ target_include_directories(includes-only-cxx

add_library(iceoryx2-cxx-object-lib OBJECT
src/config.cpp
src/error_string.cpp
src/event_id.cpp
src/file_descriptor.cpp
src/header_publish_subscribe.cpp
Expand Down
42 changes: 34 additions & 8 deletions iceoryx2-ffi/cxx/include/iox/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "iox/assertions_addendum.hpp"

#include <cstdint>
#include <type_traits>

namespace iox {
template <typename T>
Expand All @@ -25,27 +26,52 @@ class Slice {
using ConstIterator = const T*;
using ValueType = T;

auto size() const -> uint64_t {
IOX_TODO();
template <typename U>
Slice(U* data, uint64_t len)
: m_data { const_cast<T*>(static_cast<const std::remove_pointer_t<U>*>(data)) }
, m_len { len } {
}

auto len() const -> uint64_t {
return m_len;
}
auto operator[](const uint64_t n) const -> const T& {
IOX_TODO();
IOX_ASSERT(n < m_len, "Index out of bounds");
return *(m_data + n);
}

auto operator[](const uint64_t n) -> T& {
IOX_TODO();
IOX_ASSERT(n < m_len, "Index out of bounds");
return *(m_data + n);
}

auto begin() -> Iterator {
IOX_TODO();
return m_data;
}

auto begin() const -> ConstIterator {
IOX_TODO();
return m_data;
}

auto end() -> Iterator {
IOX_TODO();
return m_data + m_len;
}

auto end() const -> ConstIterator {
IOX_TODO();
return m_data + m_len;
}

auto data() -> T* {
return m_data;
};

auto data() const -> const T* {
return m_data;
};

private:
T* m_data;
uint64_t m_len;
};

template <typename>
Expand Down
3 changes: 3 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/config_creation_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ enum class ConfigCreationError : uint8_t {
/// Parts of the config file could not be deserialized. Indicates some kind of syntax error.
UnableToDeserializeContents
};

auto error_string(const iox2::ConfigCreationError& error) -> const char*;

} // namespace iox2

#endif
4 changes: 4 additions & 0 deletions iceoryx2-ffi/cxx/include/iox2/connection_failure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ enum class ConnectionFailure : uint8_t {
/// Failures when mapping the corresponding data segment
UnableToMapPublishersDataSegment
};


auto error_string(const iox2::ConnectionFailure& error) -> const char*;

} // namespace iox2

#endif
Loading
Loading