Skip to content

Commit 0490031

Browse files
authored
Merge pull request #702 from elbeno/msg-env
2 parents 458337d + 0858891 commit 0490031

File tree

8 files changed

+204
-64
lines changed

8 files changed

+204
-64
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include(cmake/string_catalog.cmake)
2525
add_versioned_package("gh:boostorg/mp11#boost-1.83.0")
2626
fmt_recipe(11.1.3)
2727
add_versioned_package("gh:intel/cpp-baremetal-concurrency#7c5b26c")
28-
add_versioned_package("gh:intel/cpp-std-extensions#ec95cd6")
28+
add_versioned_package("gh:intel/cpp-std-extensions#7725142")
2929
add_versioned_package("gh:intel/cpp-baremetal-senders-and-receivers#73d95bc")
3030

3131
set(GEN_STR_CATALOG

include/log/env.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ using cib_log_env_t = stdx::env<>;
1818
return stdx::extend_env_t<cib_log_env_t, __VA_ARGS__>{}; \
1919
}()) cib_log_env_t
2020

21+
#define CIB_APPEND_LOG_ENV_DECL(E) \
22+
[[maybe_unused]] typedef decltype([] { \
23+
return stdx::append_env_t<cib_log_env_t, E>{}; \
24+
}()) cib_log_env_t
25+
2126
#define CIB_LOG_ENV(...) \
2227
STDX_PRAGMA(diagnostic push) \
2328
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
2429
CIB_LOG_ENV_DECL(__VA_ARGS__) \
2530
CIB_PRAGMA_SEMI \
2631
STDX_PRAGMA(diagnostic pop)
2732

33+
#define CIB_APPEND_LOG_ENV(E) \
34+
STDX_PRAGMA(diagnostic push) \
35+
STDX_PRAGMA(diagnostic ignored "-Wshadow") \
36+
CIB_APPEND_LOG_ENV_DECL(E) \
37+
CIB_PRAGMA_SEMI \
38+
STDX_PRAGMA(diagnostic pop)
39+
2840
#define CIB_WITH_LOG_ENV(...) \
2941
STDX_PRAGMA(diagnostic push) \
3042
STDX_PRAGMA(diagnostic ignored "-Wshadow") \

include/msg/callback.hpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ struct callback {
2929

3030
template <typename... Args>
3131
[[nodiscard]] auto handle(auto const &data, Args &&...args) const -> bool {
32+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
3233
if (msg::call_with_message<Msg>(matcher, data)) {
33-
CIB_INFO("Incoming message matched [{}], because [{}], executing "
34-
"callback",
35-
stdx::ct_string_to_type<Name, sc::string_constant>(),
36-
matcher.describe());
34+
CIB_APPEND_LOG_ENV(typename Msg::env_t);
35+
CIB_LOG("Incoming message matched [{}], because [{}], executing "
36+
"callback",
37+
stdx::ct_string_to_type<Name, sc::string_constant>(),
38+
matcher.describe());
3739
msg::call_with_message<Msg>(callable, data,
3840
std::forward<Args>(args)...);
3941
return true;
@@ -42,14 +44,19 @@ struct callback {
4244
}
4345

4446
auto log_mismatch(auto const &data) const -> void {
45-
CIB_INFO(" {} - F:({})",
46-
stdx::ct_string_to_type<Name, sc::string_constant>(),
47-
msg::call_with_message<Msg>(
48-
[&]<typename T>(T &&t) -> decltype(matcher.describe_match(
49-
std::forward<T>(t))) {
50-
return matcher.describe_match(std::forward<T>(t));
51-
},
52-
data));
47+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
48+
{
49+
CIB_APPEND_LOG_ENV(typename Msg::env_t);
50+
CIB_LOG(
51+
" {} - F:({})",
52+
stdx::ct_string_to_type<Name, sc::string_constant>(),
53+
msg::call_with_message<Msg>(
54+
[&]<typename T>(T &&t) -> decltype(matcher.describe_match(
55+
std::forward<T>(t))) {
56+
return matcher.describe_match(std::forward<T>(t));
57+
},
58+
data));
59+
}
5360
}
5461

5562
using msg_t = Msg;

include/msg/detail/indexed_builder_common.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,13 @@ struct indexed_builder_base {
163163
}
164164

165165
using msg_t = typename CB::msg_t;
166+
CIB_LOG_ENV(logging::get_level, logging::level::INFO);
166167
if (msg::call_with_message<msg_t>(cb.matcher, data)) {
167-
CIB_INFO(
168-
"Incoming message matched [{}], because [{}] (collapsed to "
169-
"[{}]), executing callback",
170-
stdx::ct_string_to_type<cb.name, sc::string_constant>(),
171-
orig_cb.matcher.describe(), cb.matcher.describe());
168+
CIB_APPEND_LOG_ENV(typename msg_t::env_t);
169+
CIB_LOG("Incoming message matched [{}], because [{}] (collapsed to "
170+
"[{}]), executing callback",
171+
stdx::ct_string_to_type<cb.name, sc::string_constant>(),
172+
orig_cb.matcher.describe(), cb.matcher.describe());
172173
msg::call_with_message<msg_t>(cb.callable, data, args...);
173174
return true;
174175
}

include/msg/message.hpp

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <stdx/compiler.hpp>
1111
#include <stdx/ct_string.hpp>
12-
#include <stdx/cx_vector.hpp>
12+
#include <stdx/env.hpp>
1313
#include <stdx/iterator.hpp>
1414
#include <stdx/ranges.hpp>
1515
#include <stdx/span.hpp>
@@ -197,7 +197,7 @@ template <typename... Fields> struct storage_size {
197197

198198
template <typename F> using name_for = typename F::name_t;
199199

200-
template <stdx::ct_string Name, typename... Fields> class message_access {
200+
template <stdx::ct_string Name, typename... Fields> class msg_access {
201201
using FieldsTuple =
202202
decltype(stdx::make_indexed_tuple<name_for>(Fields{}...));
203203

@@ -295,10 +295,11 @@ concept storage_like = stdx::range<T> and requires {
295295
typename T::value_type;
296296
};
297297

298-
template <stdx::ct_string Name, typename... Fields> struct message;
298+
template <stdx::ct_string Name, typename Env, typename... Fields>
299+
struct message;
299300

300-
template <stdx::ct_string Name> struct msg_q {
301-
template <typename... Fields> using fn = message<Name, Fields...>;
301+
template <stdx::ct_string Name, typename Env> struct msg_q {
302+
template <typename... Fields> using fn = message<Name, Env, Fields...>;
302303
};
303304

304305
template <typename F1, typename F2>
@@ -312,9 +313,9 @@ using unique_by_name = boost::mp11::mp_unique_if<
312313
boost::mp11::mp_sort<boost::mp11::mp_list<Fields...>, field_sort_fn>,
313314
name_equal_fn>;
314315

315-
template <stdx::ct_string Name, typename... Fields>
316+
template <stdx::ct_string Name, typename Env, typename... Fields>
316317
using message_without_unique_field_names =
317-
boost::mp11::mp_apply_q<detail::msg_q<Name>,
318+
boost::mp11::mp_apply_q<detail::msg_q<Name, Env>,
318319
detail::unique_by_name<Fields...>>;
319320

320321
template <stdx::ct_string Name, typename... Fields>
@@ -323,12 +324,45 @@ struct message_with_unique_field_names {
323324
name_for, boost::mp11::mp_list<Fields...>>>::value,
324325
"Message contains fields with duplicate names");
325326

326-
using type = message_without_unique_field_names<Name, Fields...>;
327+
using type =
328+
message_without_unique_field_names<Name, stdx::env<>, Fields...>;
329+
};
330+
331+
template <stdx::ct_string Name, stdx::envlike Env, typename... Fields>
332+
struct message_with_unique_field_names<Name, Env, Fields...> {
333+
static_assert(boost::mp11::mp_is_set<boost::mp11::mp_transform<
334+
name_for, boost::mp11::mp_list<Fields...>>>::value,
335+
"Message contains fields with duplicate names");
336+
337+
using type = message_without_unique_field_names<Name, Env, Fields...>;
327338
};
328339

329-
template <stdx::ct_string Name, typename... Fields> struct message {
340+
template <stdx::ct_string Name, typename Access, typename T> struct msg_base {
341+
constexpr static auto name = Name;
342+
343+
constexpr auto as_derived() const -> T const & {
344+
return static_cast<T const &>(*this);
345+
}
346+
constexpr auto as_derived() -> T & { return static_cast<T &>(*this); }
347+
348+
[[nodiscard]] constexpr auto get(auto f) const {
349+
return Access::get(as_derived().data(), f);
350+
}
351+
constexpr auto set(auto... fs) -> void {
352+
Access::set(as_derived().data(), fs...);
353+
}
354+
constexpr auto set() -> void {}
355+
356+
[[nodiscard]] constexpr auto describe() const {
357+
return Access::describe(as_derived().data());
358+
}
359+
};
360+
361+
template <stdx::ct_string Name, typename Env, typename... Fields>
362+
struct message {
330363
using fields_t = stdx::type_list<Fields...>;
331-
using access_t = detail::message_access<Name, Fields...>;
364+
using env_t = Env;
365+
using access_t = msg_access<Name, Fields...>;
332366
using default_storage_t = typename access_t::default_storage_t;
333367
using default_span_t = typename access_t::default_span_t;
334368
using default_const_span_t = typename access_t::default_const_span_t;
@@ -342,32 +376,13 @@ template <stdx::ct_string Name, typename... Fields> struct message {
342376

343377
template <auto N, typename Unit = bit_unit>
344378
using shifted_by =
345-
message<Name, typename Fields::template shifted_by<N, Unit>...>;
379+
message<Name, Env, typename Fields::template shifted_by<N, Unit>...>;
346380

347381
template <typename S>
348382
constexpr static auto fits_inside =
349383
(... and Fields::template fits_inside<S>());
350384

351-
template <typename T> struct base {
352-
constexpr static auto name = Name;
353-
354-
constexpr auto as_derived() const -> T const & {
355-
return static_cast<T const &>(*this);
356-
}
357-
constexpr auto as_derived() -> T & { return static_cast<T &>(*this); }
358-
359-
[[nodiscard]] constexpr auto get(auto f) const {
360-
return access_t::get(as_derived().data(), f);
361-
}
362-
constexpr auto set(auto... fs) -> void {
363-
access_t::set(as_derived().data(), fs...);
364-
}
365-
constexpr auto set() -> void {}
366-
367-
[[nodiscard]] constexpr auto describe() const {
368-
return access_t::describe(as_derived().data());
369-
}
370-
};
385+
template <typename T> using base = msg_base<Name, access_t, T>;
371386

372387
template <typename> struct owner_t;
373388

@@ -583,13 +598,17 @@ template <stdx::ct_string Name, typename... Fields> struct message {
583598
// name (see unique_by_name)
584599
template <stdx::ct_string NewName, typename... NewFields>
585600
using extension =
586-
message_without_unique_field_names<NewName, NewFields..., Fields...>;
601+
message_without_unique_field_names<NewName, Env, NewFields...,
602+
Fields...>;
603+
604+
template <stdx::envlike E>
605+
using with_env = message<Name, stdx::append_env_t<Env, E>, Fields...>;
587606
};
588607
} // namespace detail
589608

590-
template <stdx::ct_string Name, typename... Fields>
609+
template <stdx::ct_string Name, typename... Ts>
591610
using message =
592-
typename detail::message_with_unique_field_names<Name, Fields...>::type;
611+
typename detail::message_with_unique_field_names<Name, Ts...>::type;
593612

594613
template <typename T> using owning = typename T::template owner_t<>;
595614
template <typename T> using mutable_view = typename T::mutable_view_t;
@@ -681,15 +700,16 @@ using shifted_msgs =
681700
msg_offsets<AlignTo, Msgs...>,
682701
stdx::type_list<Msgs..., msg::message<"end">>>;
683702

684-
template <stdx::ct_string Name> struct combiner {
685-
template <typename... Fields> using fn = msg::message<Name, Fields...>;
703+
template <stdx::ct_string Name, stdx::envlike Env> struct combiner {
704+
template <typename... Fields> using fn = msg::message<Name, Env, Fields...>;
686705
};
687706

688707
template <stdx::ct_string Name> struct combine_q {
689708
template <typename... Msgs>
690709
requires(sizeof...(Msgs) > 0)
691710
using fn = boost::mp11::mp_apply_q<
692-
combiner<Name>, boost::mp11::mp_append<typename Msgs::fields_t...>>;
711+
combiner<Name, stdx::append_env_t<typename Msgs::env_t...>>,
712+
boost::mp11::mp_append<typename Msgs::fields_t...>>;
693713
};
694714
} // namespace detail
695715

@@ -709,32 +729,37 @@ using is_locatable = std::bool_constant<requires { F::sort_key; }>;
709729
template <typename F1, typename F2>
710730
using field_size_sort_fn = std::bool_constant < F2::bitsize<F1::bitsize>;
711731

712-
template <stdx::ct_string Name, typename... Fields> struct field_locator {
732+
template <stdx::ct_string Name, typename... Fields>
733+
struct field_locator : field_locator<Name, stdx::env<>, Fields...> {};
734+
735+
template <stdx::ct_string Name, stdx::envlike Env, typename... Fields>
736+
struct field_locator<Name, Env, Fields...> {
713737
using fields = boost::mp11::mp_partition<boost::mp11::mp_list<Fields...>,
714738
is_locatable>;
715739
using located_fields = boost::mp11::mp_first<fields>;
716740
using unlocated_fields = boost::mp11::mp_second<fields>;
717741

718-
using located_msg = boost::mp11::mp_apply_q<combiner<Name>, located_fields>;
742+
using located_msg =
743+
boost::mp11::mp_apply_q<combiner<Name, stdx::env<>>, located_fields>;
719744

720745
using auto_fields =
721746
boost::mp11::mp_sort<unlocated_fields, field_size_sort_fn>;
722747

723748
template <typename F>
724749
using as_singleton_message =
725-
msg::message<Name, typename F::default_located>;
750+
msg::message<Name, Env, typename F::default_located>;
726751
using auto_msgs =
727752
boost::mp11::mp_transform<as_singleton_message, auto_fields>;
728753

729754
using all_msgs = boost::mp11::mp_push_front<auto_msgs, located_msg>;
730755

731756
template <typename... Msgs>
732757
using pack = msg::pack<Name, std::uint8_t, Msgs...>;
733-
using msg_type = boost::mp11::mp_apply<pack, all_msgs>;
758+
using msg_type =
759+
typename boost::mp11::mp_apply<pack, all_msgs>::template with_env<Env>;
734760
};
735761
} // namespace detail
736762

737-
template <stdx::ct_string Name, typename... Fields>
738-
using relaxed_message =
739-
typename detail::field_locator<Name, Fields...>::msg_type;
763+
template <stdx::ct_string Name, typename... Ts>
764+
using relaxed_message = typename detail::field_locator<Name, Ts...>::msg_type;
740765
} // namespace msg

test/msg/callback.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ TEST_CASE("callback handles message (typed)", "[callback]") {
152152
CHECK(dispatched);
153153
}
154154

155-
TEST_CASE("callback logs match", "[callback]") {
155+
TEST_CASE("callback logs match as INFO", "[callback]") {
156156
auto callback = msg::callback<"cb", msg_defn>(
157157
id_match, [](msg::const_view<msg_defn>) { dispatched = true; });
158158
auto const msg_match = std::array{0x8000ba11u, 0x0042d00du};
@@ -162,6 +162,22 @@ TEST_CASE("callback logs match", "[callback]") {
162162
CAPTURE(log_buffer);
163163
CHECK(log_buffer.find("matched [cb], because [id == 0x80]") !=
164164
std::string::npos);
165+
CHECK(log_buffer.find("INFO") != std::string::npos);
166+
}
167+
168+
TEST_CASE("callback logs match using message environment", "[callback]") {
169+
using trace_msg_defn = msg_defn::with_env<
170+
stdx::make_env_t<logging::get_level, logging::level::TRACE>>;
171+
auto callback = msg::callback<"cb", trace_msg_defn>(
172+
id_match, [](msg::const_view<trace_msg_defn>) { dispatched = true; });
173+
auto const msg_match = std::array{0x8000ba11u, 0x0042d00du};
174+
175+
log_buffer.clear();
176+
CHECK(callback.handle(msg_match));
177+
CAPTURE(log_buffer);
178+
CHECK(log_buffer.find("matched [cb], because [id == 0x80]") !=
179+
std::string::npos);
180+
CHECK(log_buffer.find("TRACE") != std::string::npos);
165181
}
166182

167183
TEST_CASE("callback with convenience matcher", "[callback]") {

0 commit comments

Comments
 (0)