iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
layout.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_BB_LAYOUT_HPP
14#define IOX2_BB_LAYOUT_HPP
15
16#include "iox2/bb/expected.hpp"
17#include <cstdint>
18#include <type_traits>
19
20namespace iox2 {
21namespace bb {
22
24enum class LayoutCreationError : uint8_t {
27};
28
31class Layout {
32 public:
35 template <typename T>
36 static auto from() -> std::enable_if_t<!std::is_same<T, void>::value, Layout>;
37
39 template <typename T>
40 static auto from() -> std::enable_if_t<std::is_same<T, void>::value, Layout>;
41
46 static auto create(uint64_t size, uint64_t align) -> iox2::bb::Expected<Layout, LayoutCreationError>;
47
49 auto size() const -> uint64_t;
50
52 auto alignment() const -> uint64_t;
53
54 private:
55 static auto is_power_of_two(uint64_t value) -> bool;
56
57 static auto round_up_to(uint64_t value, uint64_t multiple) -> uint64_t;
58
59 Layout(uint64_t size, uint64_t align);
60
61 uint64_t m_size;
62 uint64_t m_align;
63};
64
65
66template <typename T>
67inline auto Layout::from() -> std::enable_if_t<!std::is_same<T, void>::value, Layout> {
68 return Layout { sizeof(T), alignof(T) };
69}
70
71template <typename T>
72inline auto Layout::from() -> std::enable_if_t<std::is_same<T, void>::value, Layout> {
73 return Layout { 0, 1 };
74}
75
76inline auto Layout::create(const uint64_t size, const uint64_t align)
78 if (!is_power_of_two(align)) {
80 }
81
82 return Layout(round_up_to(size, align), align);
83}
84
85inline auto Layout::size() const -> uint64_t {
86 return m_size;
87}
88
89inline auto Layout::alignment() const -> uint64_t {
90 return m_align;
91}
92
93inline auto Layout::is_power_of_two(const uint64_t value) -> bool {
94 return (value != 0) && ((value & (value - 1)) == 0);
95}
96
97inline auto Layout::round_up_to(const uint64_t value, const uint64_t multiple) -> uint64_t {
98 return (value % multiple == 0) ? value : multiple * ((value / multiple) + 1);
99}
100
101// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) private ctor, only used internally
102inline Layout::Layout(const uint64_t size, const uint64_t align)
103 : m_size { size }
104 , m_align { align } {
105}
106
107} // namespace bb
108} // namespace iox2
109
110#endif // IOX2_BB_LAYOUT_HPP
static auto from() -> std::enable_if_t<!std::is_same< T, void >::value, Layout >
Definition layout.hpp:67
static auto from() -> std::enable_if_t< std::is_same< T, void >::value, Layout >
Creates a new [Layout] with size == 0 and alignment == 1.
auto alignment() const -> uint64_t
Returns the stored alignment.
Definition layout.hpp:89
auto size() const -> uint64_t
Returns the stored size.
Definition layout.hpp:85
static auto create(uint64_t size, uint64_t align) -> iox2::bb::Expected< Layout, LayoutCreationError >
Definition layout.hpp:76
LayoutCreationError
Defines all errors that can occur while creating a new [Layout].
Definition layout.hpp:24
@ InvalidAlignment
The provided alignment was not a power of two.
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22