Skip to content

Commit

Permalink
NullSources: use only supported SettingsTypes for default_value
Browse files Browse the repository at this point in the history
Since the Sources are instantiated with types not supported by Settings,
we have use the base value type, except for string, which is supported.

This should probably be revisited since while this works, it causes
warnings and unexpected behaviour for the Packet/DataSet/... types since
default constructing them with their base value will not create a one
item instance with that value, but use it to initialize the first field,
in this case an uint64_t timestamp.

Signed-off-by: Alexander Krimm <[email protected]>
  • Loading branch information
wirew0rm committed Dec 13, 2024
1 parent 624a2e0 commit ba5c799
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions blocks/testing/include/gnuradio-4.0/testing/NullSources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ static_assert(gr::BlockLike<NullSource<float>>);

template<typename T>
struct ConstantSource : public gr::Block<ConstantSource<T>> {
using value_t = std::conditional_t<std::is_same_v<T, std::string>, std::string, meta::fundamental_base_value_type_t<T>>; // use base-type for types not supported by settings
using Description = Doc<R""(A source block that emits a constant default value for each output sample.
This block counts the number of samples emitted and optionally halts after reaching a specified maximum.
Commonly used for testing and simulations where consistent output and finite execution are required.)"">;

gr::PortOut<T> out;

Annotated<T, "default value", Visible, Doc<"default value for each sample">> default_value{};
Annotated<value_t, "default value", Visible, Doc<"default value for each sample">> default_value{};
Annotated<gr::Size_t, "max samples", Doc<"count>n_samples_max -> signal DONE (0: infinite)">> n_samples_max = 0U;
Annotated<gr::Size_t, "count", Doc<"sample count (diagnostics only)">> count = 0U;

Expand All @@ -44,19 +45,20 @@ Commonly used for testing and simulations where consistent output and finite exe
if (n_samples_max > 0 && count >= n_samples_max) {
this->requestStop();
}
return default_value;
return T(default_value.value);
}
};

static_assert(gr::BlockLike<ConstantSource<float>>);

template<typename T>
struct SlowSource : public gr::Block<SlowSource<T>> {
using value_t = std::conditional_t<std::is_same_v<T, std::string>, std::string, meta::fundamental_base_value_type_t<T>>; // use base-type for types not supported by settings
using Description = Doc<R""(A source block that emits a constant default value every n miliseconds)"">;

gr::PortOut<T> out;
Annotated<T, "default value", Visible, Doc<"default value for each sample">> default_value{};
Annotated<gr::Size_t, "delay", Doc<"how many milliseconds between each value">> n_delay = 100U;
gr::PortOut<T> out;
Annotated<value_t, "default value", Visible, Doc<"default value for each sample">> default_value{};
Annotated<gr::Size_t, "delay", Doc<"how many milliseconds between each value">> n_delay = 100U;

GR_MAKE_REFLECTABLE(SlowSource, out, n_delay);

Expand All @@ -66,7 +68,7 @@ struct SlowSource : public gr::Block<SlowSource<T>> {
if (!lastEventAt || std::chrono::system_clock::now() - *lastEventAt > std::chrono::milliseconds(n_delay)) {
lastEventAt = std::chrono::system_clock::now();

output[0] = default_value;
output[0] = T(default_value.value);
output.publish(1UZ);
} else {
output.publish(0UZ);
Expand Down Expand Up @@ -100,7 +102,7 @@ Commonly used for testing and simulations where consistent output and finite exe
if (n_samples_max > 0 && count >= n_samples_max) {
this->requestStop();
}
return default_value + T(count);
return T(default_value.value) + T(count);
}
};

Expand Down

0 comments on commit ba5c799

Please sign in to comment.