Skip to content

Commit 83ed720

Browse files
committed
Allow for retrieving marker labels from the sensor_array.
1 parent 4f1f707 commit 83ed720

File tree

5 files changed

+179
-7
lines changed

5 files changed

+179
-7
lines changed

power_overwhelming/include/visus/pwrowg/sensor_array.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <copyright file="sensor_array.h" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
2+
// Copyright © 2025 - 2026 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55
// <author>Christoph Müller</author>
@@ -208,6 +208,44 @@ class POWER_OVERWHELMING_API sensor_array final {
208208
return this->marker(timestamp::now(), id);
209209
}
210210

211+
/// <summary>
212+
/// Answer the label of the specified <paramref name="marker" />.
213+
/// </summary>
214+
/// <param name="dst">If not <see langword="nullptr" />, receives the name
215+
/// of the marker provided it exists and fits into <paramref name="cnt" />
216+
/// characters.</param>
217+
/// <param name="cnt">The size of the <paramref name="dst" /> array.</param>
218+
/// <param name="marker">The ID of the marker to retrieve the label for.
219+
/// </param>
220+
/// <returns>The required buffer size to store the marker (including the
221+
/// terminating NUL character), regardless of whether the name has been
222+
/// written. If the <paramref name="marker" /> does not exist, the return
223+
/// value is zero.</returns>
224+
std::size_t marker(_Out_writes_opt_z_(cnt) wchar_t *dst,
225+
_In_ const std::size_t cnt, _In_ const unsigned int marker) const;
226+
227+
/// <summary>
228+
/// Answer the label of the specified <paramref name="marker" />.
229+
/// </summary>
230+
/// <param name="dst">If not <see langword="nullptr" />, receives the name
231+
/// of the marker provided it exists and fits into <paramref name="cnt" />
232+
/// characters.</param>
233+
/// <param name="cnt">The size of the <paramref name="dst" /> array.</param>
234+
/// <param name="marker">The ID of the marker to retrieve the label for.
235+
/// </param>
236+
/// <returns>The required buffer size to store the marker (including the
237+
/// terminating NUL character), regardless of whether the name has been
238+
/// written. If the <paramref name="marker" /> does not exist, the return
239+
/// value is zero.</returns>
240+
std::size_t marker(_Out_writes_opt_z_(cnt) char *dst,
241+
_In_ const std::size_t cnt, _In_ const unsigned int marker) const;
242+
243+
/// <summary>
244+
/// Answer the number of markers configured in the array.
245+
/// </summary>
246+
/// <returns>The number of valid markers.</returns>
247+
std::size_t markers(void) const noexcept;
248+
211249
/// <summary>
212250
/// Starts sampling all sensors in the array.
213251
/// </summary>

power_overwhelming/src/marker_sensor.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <copyright file="marker_sensor.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
2+
// Copyright © 2025 - 2026 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55
// <author>Christoph Müller</author>
@@ -44,12 +44,12 @@ bool PWROWG_DETAIL_NAMESPACE::marker_sensor::emit(
4444
_In_ const timestamp timestamp,
4545
_In_ const unsigned int id) {
4646
this->_emitting.store(true, std::memory_order_release);
47-
pwrowg_on_exit([this](void) {
47+
pwrowg_on_exit([this](void) {
4848
this->_emitting.store(false, std::memory_order_release);
4949
});
5050

5151
const auto retval = (id >= 0)
52-
&& (id < this->_markers.size())
52+
&& (id < this->size())
5353
&& this->_state;
5454

5555
if (retval) {
@@ -64,6 +64,30 @@ bool PWROWG_DETAIL_NAMESPACE::marker_sensor::emit(
6464
}
6565

6666

67+
/*
68+
* PWROWG_DETAIL_NAMESPACE::marker_sensor::marker
69+
*/
70+
std::size_t PWROWG_DETAIL_NAMESPACE::marker_sensor::marker(
71+
_Out_writes_opt_z_(cnt) wchar_t *dst,
72+
_In_ const std::size_t cnt,
73+
_In_ const unsigned int marker) {
74+
if ((marker < 0) || (marker >= this->size())) {
75+
return 0;
76+
}
77+
78+
const auto retval = this->_markers[marker].size() + 1;
79+
80+
if ((dst != nullptr) && (cnt >= retval)) {
81+
std::copy(this->_markers[marker].begin(),
82+
this->_markers[marker].end(),
83+
dst);
84+
assert(dst[retval - 1] == L'\0');
85+
}
86+
87+
return retval;
88+
}
89+
90+
6791
/*
6892
* PWROWG_DETAIL_NAMESPACE::marker_sensor::sample
6993
*/

power_overwhelming/src/marker_sensor.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <copyright file="marker_sensor.h" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
2+
// Copyright © 2025 - 2026 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55
// <author>Christoph Müller</author>
@@ -137,13 +137,37 @@ class PWROWG_TEST_API marker_sensor final {
137137
/// otherwise.</returns>
138138
bool emit(_In_ const timestamp timestamp, _In_ const unsigned int id);
139139

140+
/// <summary>
141+
/// Answer the label of the specified <paramref name="marker" />.
142+
/// </summary>
143+
/// <param name="dst">If not <see langword="nullptr" />, receives the name
144+
/// of the marker provided it exists and fits into <paramref name="cnt" />
145+
/// characters.</param>
146+
/// <param name="cnt">The size of the <paramref name="dst" /> array.</param>
147+
/// <param name="marker">The ID of the marker to retrieve the label for.
148+
/// </param>
149+
/// <returns>The required buffer size to store the marker (including the
150+
/// terminating NUL character), regardless of whether the name has been
151+
/// written. If the <paramref name="marker" /> does not exist, the return
152+
/// value is zero.</returns>
153+
std::size_t marker(_Out_writes_opt_z_(cnt) wchar_t *dst,
154+
_In_ const std::size_t cnt, _In_ const unsigned int marker);
155+
140156
/// <summary>
141157
/// Starts or stops sampling the sensor.
142158
/// </summary>
143159
/// <param name="enable"><c>true</c> for enabling the sensor,
144160
/// <c>false</c> for disabling it.</param>
145161
void sample(_In_ const bool enable);
146162

163+
/// <summary>
164+
/// Answer the number of markers configured for this sensor.
165+
/// </summary>
166+
/// <returns>The number of markers.</returns>
167+
inline std::size_t size(void) const noexcept {
168+
return this->_markers.size();
169+
}
170+
147171
marker_sensor& operator =(const marker_sensor& rhs) = delete;
148172

149173
private:

power_overwhelming/src/sensor_array.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <copyright file="sensor_array.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
2+
// Copyright © 2025 - 2026 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55
// <author>Christoph Müller</author>
@@ -182,6 +182,64 @@ bool PWROWG_NAMESPACE::sensor_array::marker(_In_ const timestamp timestamp,
182182
}
183183

184184

185+
/*
186+
* PWROWG_NAMESPACE::sensor_array::marker
187+
*/
188+
std::size_t PWROWG_NAMESPACE::sensor_array::marker(
189+
_Out_writes_opt_z_(cnt) wchar_t *dst,
190+
_In_ const std::size_t cnt,
191+
_In_ const unsigned int marker) const {
192+
typedef detail::marker_sensor::list_type type;
193+
typedef detail::tuple_types_t<decltype(this->_impl->sensors)> types;
194+
typedef detail::type_list_index_of<type, types> index;
195+
196+
volatile auto impl = this->_impl; // sic!
197+
auto& list = std::get<index::value>(this->_impl->sensors);
198+
199+
return ((impl != nullptr) && !list.empty())
200+
? list.begin()->marker(dst, cnt, marker)
201+
: 0;
202+
}
203+
204+
205+
/*
206+
* PWROWG_NAMESPACE::sensor_array::marker
207+
*/
208+
std::size_t PWROWG_NAMESPACE::sensor_array::marker(
209+
_Out_writes_opt_z_(cnt) char *dst,
210+
_In_ const std::size_t cnt,
211+
_In_ const unsigned int marker) const {
212+
auto retval = this->marker(static_cast<wchar_t *>(nullptr), 0, marker);
213+
214+
if (retval == 0) {
215+
// If the marker does not exist, we can bail out.
216+
return retval;
217+
}
218+
219+
// Get the marker and measure the required UTF-8 code units.
220+
std::vector<wchar_t> buffer(retval);
221+
this->marker(buffer.data(), buffer.size(), marker);
222+
retval = detail::convert_string(dst, cnt, buffer.data(), retval);
223+
224+
return retval;
225+
}
226+
227+
228+
/*
229+
* PWROWG_NAMESPACE::sensor_array::markers
230+
*/
231+
std::size_t PWROWG_NAMESPACE::sensor_array::markers(void) const noexcept {
232+
typedef detail::marker_sensor::list_type type;
233+
typedef detail::tuple_types_t<decltype(this->_impl->sensors)> types;
234+
typedef detail::type_list_index_of<type, types> index;
235+
236+
volatile auto impl = this->_impl; // sic!
237+
auto& list = std::get<index::value>(this->_impl->sensors);
238+
239+
return ((impl != nullptr) && !list.empty()) ? list.begin()->size() : 0;
240+
}
241+
242+
185243
/*
186244
* PWROWG_NAMESPACE::sensor_array::start
187245
*/

test/marker_test.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// <copyright file="marker_test.cpp" company="Visualisierungsinstitut der Universität Stuttgart">
2-
// Copyright © 2025 Visualisierungsinstitut der Universität Stuttgart.
2+
// Copyright © 2025 - 2026 Visualisierungsinstitut der Universität Stuttgart.
33
// Licensed under the MIT licence. See LICENCE file for details.
44
// </copyright>
55
// <author>Christoph Müller</author>
@@ -47,6 +47,7 @@ TEST_CLASS(marker_test) {
4747
.exclude<usb_pd_configuration>();
4848

4949
auto sensors = sensor_array::for_matches(std::move(config), is_marker_sensor);
50+
Assert::AreEqual(std::size_t(1), sensors.markers(), L"# of markers", LINE_INFO());
5051

5152
std::vector<sensor_description> descs;
5253
descs.resize(sensors.descriptions(nullptr, 0));
@@ -64,6 +65,33 @@ TEST_CLASS(marker_test) {
6465
sensors.stop();
6566
}
6667
}
68+
69+
TEST_METHOD(test_marker_name) {
70+
typedef detail::marker_sensor type;
71+
72+
sensor_array_configuration config;
73+
config.configure<marker_configuration>([](marker_configuration &c) { c += L"Erich"; })
74+
.exclude<hmc8015_configuration>()
75+
.exclude<tinkerforge_configuration>()
76+
.exclude<usb_pd_configuration>();
77+
78+
auto sensors = sensor_array::for_matches(std::move(config), is_marker_sensor);
79+
80+
{
81+
std::vector<wchar_t> name(sensors.marker(static_cast<wchar_t *>(nullptr), 0, 0));
82+
Assert::AreEqual(std::size_t(6), sensors.marker(name.data(), name.size(), 0), L"Buffer length", LINE_INFO());
83+
Assert::AreEqual(L"Erich", name.data(), L"Marker name", LINE_INFO());
84+
}
85+
86+
{
87+
std::vector<char> name(sensors.marker(static_cast<char *>(nullptr), 0, 0));
88+
Assert::AreEqual(std::size_t(6), sensors.marker(name.data(), name.size(), 0), L"Buffer length", LINE_INFO());
89+
Assert::AreEqual("Erich", name.data(), L"Marker name", LINE_INFO());
90+
}
91+
92+
Assert::AreEqual(std::size_t(0), sensors.marker(static_cast<wchar_t *>(nullptr), 0, 42), L"Inexistent marker", LINE_INFO());
93+
Assert::AreEqual(std::size_t(0), sensors.marker(static_cast<char *>(nullptr), 0, 42), L"Inexistent marker", LINE_INFO());
94+
}
6795
};
6896

6997
PWROWG_TEST_NAMESPACE_END

0 commit comments

Comments
 (0)