Sen API
Sen Libraries
Loading...
Searching...
No Matches
callable.h
Go to the documentation of this file.
1// === callable.h ======================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_META_CALLABLE_H
9#define SEN_CORE_META_CALLABLE_H
10
11// sen
12#include "sen/core/base/class_helpers.h" // for SEN_NOCOPY_NOMOVE
14#include "sen/core/base/span.h" // for Span
15#include "sen/core/meta/type.h" // for MemberHash, TransportMode
16
17// std
18#include <string>
19#include <string_view>
20#include <unordered_map>
21#include <vector>
22
23namespace sen
24{
25
28
30struct Arg final
31{
32 SEN_COPY_MOVE(Arg)
33
34public:
35 Arg(std::string theName, std::string theDescription, ConstTypeHandle<> theType)
36 : name(std::move(theName)), description(std::move(theDescription)), type(std::move(theType))
37 {
38 }
39
40 // Comparison operators
41 friend bool operator==(const Arg& lhs, const Arg& rhs) noexcept
42 {
43 if (&lhs == &rhs)
44 {
45 return true;
46 }
47
48 return lhs.name == rhs.name && lhs.description == rhs.description && lhs.type == rhs.type;
49 }
50
51 friend bool operator!=(const Arg& lhs, const Arg& rhs) noexcept { return !(lhs == rhs); }
52
53 ~Arg() = default;
54
55public:
56 std::string name; // NOLINT(misc-non-private-member-variables-in-classes)
57 std::string description; // NOLINT(misc-non-private-member-variables-in-classes)
58 ConstTypeHandle<> type; // NOLINT(misc-non-private-member-variables-in-classes)
59
60public:
61 [[nodiscard]] uint32_t getNameHash() const noexcept;
62
63private:
64 mutable uint32_t nameHash_ = 0;
65};
66
68struct CallableSpec final
69{
70
71 friend bool operator==(const CallableSpec& lhs, const CallableSpec& rhs) noexcept
72 {
73 if (&lhs == &rhs)
74 {
75 return true;
76 }
77
78 return lhs.transportMode == rhs.transportMode && lhs.name == rhs.name && lhs.description == rhs.description &&
79 lhs.args == rhs.args;
80 }
81
82 friend bool operator!=(const CallableSpec& lhs, const CallableSpec& rhs) noexcept { return !(lhs == rhs); }
83
84 std::string name;
85 std::string description;
86 std::vector<Arg> args;
88};
89
92{
93 SEN_NOCOPY_NOMOVE(Callable)
94
95public: // special members
97 explicit Callable(CallableSpec spec);
98 virtual ~Callable() = default;
99
100public:
102 [[nodiscard]] std::string_view getName() const noexcept;
103
105 [[nodiscard]] std::string_view getDescription() const noexcept;
106
108 [[nodiscard]] TransportMode getTransportMode() const noexcept;
109
111 [[nodiscard]] Span<const Arg> getArgs() const noexcept;
112
116 [[nodiscard]] const Arg* getArgFromName(std::string_view name) const;
117
119 [[nodiscard]] size_t getArgIndexFromNameHash(MemberHash nameHash) const;
120
121public: // comparison
122 [[nodiscard]] bool operator==(const Callable& other) const noexcept;
123
124 [[nodiscard]] bool operator!=(const Callable& other) const noexcept;
125
126public:
128 static void checkSpec(const CallableSpec& spec);
129
130protected:
132 [[nodiscard]] const CallableSpec& getCallableSpec() const noexcept;
133
134private:
135 CallableSpec spec_;
136 std::unordered_map<MemberHash, size_t> nameHashToArgIndex_;
137};
138
140
141//----------------------------------------------------------------------------------------------------------------------
142// Inline implementation
143//----------------------------------------------------------------------------------------------------------------------
144
145inline uint32_t Arg::getNameHash() const noexcept
146{
147 if (nameHash_ == 0)
148 {
149 nameHash_ = hashCombine(hashSeed, name);
150 }
151 return nameHash_;
152}
153
154template <>
155struct impl::hash<Arg>
156{
157 inline u32 operator()(const Arg& spec) const noexcept
158 {
159 return hashCombine(hashSeed, spec.name, spec.type->getHash());
160 }
161};
162
163template <>
164struct impl::hash<CallableSpec>
165{
166 u32 operator()(const CallableSpec& spec) const noexcept
167 {
168 auto result = hashCombine(hashSeed, spec.name, spec.transportMode);
169
170 for (const auto& arg: spec.args)
171 {
172 result = hashCombine(result, arg);
173 }
174
175 return result;
176 }
177};
178
179} // namespace sen
180
181#endif // SEN_CORE_META_CALLABLE_H
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
std::string_view getName() const noexcept
Callable name.
Callable(CallableSpec spec)
Copies the spec into a member.
virtual ~Callable()=default
Span< const Arg > getArgs() const noexcept
Gets the arguments.
std::string_view getDescription() const noexcept
Callable description.
TransportMode getTransportMode() const noexcept
The transport mode of this callable.
static void checkSpec(const CallableSpec &spec)
Throws std::exception if the callable spec is invalid.
const CallableSpec & getCallableSpec() const noexcept
The spec of this callable.
size_t getArgIndexFromNameHash(MemberHash nameHash) const
Get the index in the vector of Args given a name hash.
const Arg * getArgFromName(std::string_view name) const
Get the field data given a name. Nullptr means not found.
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
constexpr u32 hashSeed
Initial seed for all hashes.
Definition hash32.h:33
u32 hashCombine(u32 seed, const Types &... args) noexcept
Combines the hash of different values into a single 32-bit hash.
Definition hash32.h:214
TypeHandle< const T > ConstTypeHandle
Definition type.h:319
TransportMode
How to transport information.
Definition type.h:56
@ confirmed
Directed to each receiver, reliable, ordered, with congestion control, relatively heavyweight.
Definition type.h:59
uint32_t u32
Definition numbers.h:25
This file contains functions related to hashing and compression. This is mainly used by Sen internals...
Definition assert.h:17
STL namespace.
Holds the information for an argument in the callable.
Definition callable.h:31
friend bool operator!=(const Arg &lhs, const Arg &rhs) noexcept
Definition callable.h:51
uint32_t getNameHash() const noexcept
Definition callable.h:145
friend bool operator==(const Arg &lhs, const Arg &rhs) noexcept
Definition callable.h:41
~Arg()=default
ConstTypeHandle type
Definition callable.h:58
Arg(std::string theName, std::string theDescription, ConstTypeHandle<> theType)
Definition callable.h:35
std::string description
Definition callable.h:57
std::string name
Definition callable.h:56
Data of a callable.
Definition callable.h:69
friend bool operator!=(const CallableSpec &lhs, const CallableSpec &rhs) noexcept
Definition callable.h:82
std::vector< Arg > args
Definition callable.h:86
TransportMode transportMode
Definition callable.h:87
std::string name
Definition callable.h:84
friend bool operator==(const CallableSpec &lhs, const CallableSpec &rhs) noexcept
Definition callable.h:71
std::string description
Definition callable.h:85
The hash of a member.
Definition type.h:39