Skip to content

Commit c799e5c

Browse files
Adding string builder adapters for Milliseconds64 and Microseconds64 (#36794)
* Added string builder adaptesr for chip::System::Clock::Microseconds64 and Milliseconds64 * Restyled by clang-format * Cast the time to llu because some platforms use lu. * Added exception for chrono to check_includes_config.py --------- Co-authored-by: Restyled.io <[email protected]>
1 parent 6d9d378 commit c799e5c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

scripts/tools/check_includes_config.py

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
# Only uses <chrono> for zero-cost types.
120120
'src/system/SystemClock.h': {'chrono'},
121121
'src/platform/mbed/MbedEventTimeout.h': {'chrono'},
122+
'src/lib/core/StringBuilderAdapters.h': {'chrono'},
122123

123124
'src/app/app-platform/ContentApp.h': {'list', 'string'},
124125
'src/app/app-platform/ContentAppPlatform.cpp': {'string'},

src/lib/core/StringBuilderAdapters.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffe
2828
return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format());
2929
}
3030

31+
template <>
32+
StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
33+
pw::span<char> buffer)
34+
{
35+
// Cast to llu because some platforms use lu and others use llu.
36+
return pw::string::Format(buffer, "%llums", static_cast<long long unsigned int>(time.count()));
37+
}
38+
39+
template <>
40+
StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
41+
pw::span<char> buffer)
42+
{
43+
// Cast to llu because some platforms use lu and others use llu.
44+
return pw::string::Format(buffer, "%lluus", static_cast<long long unsigned int>(time.count()));
45+
}
46+
3147
} // namespace pw
3248

3349
#if CHIP_CONFIG_TEST_GOOGLETEST
@@ -42,5 +58,16 @@ void PrintTo(const CHIP_ERROR & err, std::ostream * os)
4258
}
4359
*os << "CHIP_ERROR:<" << err.Format() << ">";
4460
}
61+
62+
void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os)
63+
{
64+
*os << time.count() << "ms";
65+
}
66+
67+
void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os)
68+
{
69+
*os << time.count() << "us";
70+
}
71+
4572
} // namespace chip
4673
#endif // CHIP_CONFIG_TEST_GOOGLETEST

src/lib/core/StringBuilderAdapters.h

+15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
/// Expected: .... == CHIP_ERROR(0, "src/setup_payload/tests/TestAdditionalDataPayload.cpp", 234)
4242
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR
4343

44+
#include <chrono>
45+
4446
#include <pw_string/string_builder.h>
4547
#include <pw_unit_test/framework.h>
4648

@@ -51,6 +53,14 @@ namespace pw {
5153
template <>
5254
StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer);
5355

56+
// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64
57+
template <>
58+
StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
59+
pw::span<char> buffer);
60+
template <>
61+
StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
62+
pw::span<char> buffer);
63+
5464
} // namespace pw
5565
#if CHIP_CONFIG_TEST_GOOGLETEST
5666

@@ -69,5 +79,10 @@ namespace chip {
6979
///
7080
/// This enhances the readability and diagnostic information in GoogleTest test logs.
7181
void PrintTo(const CHIP_ERROR & err, std::ostream * os);
82+
83+
// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64
84+
void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os);
85+
void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os);
86+
7287
} // namespace chip
7388
#endif // CHIP_CONFIG_TEST_GOOGLETEST

0 commit comments

Comments
 (0)