Skip to content

Commit

Permalink
Adding string builder adapters for Milliseconds64 and Microseconds64 (p…
Browse files Browse the repository at this point in the history
…roject-chip#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]>
  • Loading branch information
feasel0 and restyled-commits authored Dec 11, 2024
1 parent 6d9d378 commit c799e5c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
# Only uses <chrono> for zero-cost types.
'src/system/SystemClock.h': {'chrono'},
'src/platform/mbed/MbedEventTimeout.h': {'chrono'},
'src/lib/core/StringBuilderAdapters.h': {'chrono'},

'src/app/app-platform/ContentApp.h': {'list', 'string'},
'src/app/app-platform/ContentAppPlatform.cpp': {'string'},
Expand Down
27 changes: 27 additions & 0 deletions src/lib/core/StringBuilderAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffe
return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format());
}

template <>
StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
pw::span<char> buffer)
{
// Cast to llu because some platforms use lu and others use llu.
return pw::string::Format(buffer, "%llums", static_cast<long long unsigned int>(time.count()));
}

template <>
StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
pw::span<char> buffer)
{
// Cast to llu because some platforms use lu and others use llu.
return pw::string::Format(buffer, "%lluus", static_cast<long long unsigned int>(time.count()));
}

} // namespace pw

#if CHIP_CONFIG_TEST_GOOGLETEST
Expand All @@ -42,5 +58,16 @@ void PrintTo(const CHIP_ERROR & err, std::ostream * os)
}
*os << "CHIP_ERROR:<" << err.Format() << ">";
}

void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os)
{
*os << time.count() << "ms";
}

void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os)
{
*os << time.count() << "us";
}

} // namespace chip
#endif // CHIP_CONFIG_TEST_GOOGLETEST
15 changes: 15 additions & 0 deletions src/lib/core/StringBuilderAdapters.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
/// Expected: .... == CHIP_ERROR(0, "src/setup_payload/tests/TestAdditionalDataPayload.cpp", 234)
/// Actual: CHIP_ERROR:<src/lib/core/TLVReader.cpp:889: Error 0x00000022> == CHIP_NO_ERROR

#include <chrono>

#include <pw_string/string_builder.h>
#include <pw_unit_test/framework.h>

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

// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64
template <>
StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
pw::span<char> buffer);
template <>
StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
pw::span<char> buffer);

} // namespace pw
#if CHIP_CONFIG_TEST_GOOGLETEST

Expand All @@ -69,5 +79,10 @@ namespace chip {
///
/// This enhances the readability and diagnostic information in GoogleTest test logs.
void PrintTo(const CHIP_ERROR & err, std::ostream * os);

// Adapters for chip::System::Clock::Microseconds64 and Milliseconds64
void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os);
void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os);

} // namespace chip
#endif // CHIP_CONFIG_TEST_GOOGLETEST

0 comments on commit c799e5c

Please sign in to comment.