chromium/third_party/openscreen/src/platform/base/trivial_clock_traits.h

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PLATFORM_BASE_TRIVIAL_CLOCK_TRAITS_H_
#define PLATFORM_BASE_TRIVIAL_CLOCK_TRAITS_H_

#include <chrono>
#include <ostream>
#include <ratio>
#include <string>

namespace openscreen {

// The Open Screen monotonic clock traits description, providing all the C++14
// requirements of a TrivialClock, for use with STL <chrono>.
class TrivialClockTraits {};

// Convenience type definition, for injecting time sources into classes (e.g.,
// &Clock::now versus something else for testing).
ClockNowFunctionPtr;

// Convenience for serializing to string, e.g. for tracing. Outputs a string of
// the form "123µs".
std::string ToString(const TrivialClockTraits::duration& d);

// Convenience for serializing to string, e.g. for tracing. Outputs a string of
// the form "123µs-ticks".
std::string ToString(const TrivialClockTraits::time_point& tp);

// Explicit namespace for inclusion of custom time-related operator<<
// implementations. These operators may be included in a file for use by adding:
//     using clock_operators::operator<<;
//
// NOTE: in some cases, resolution of these operators may still fail, most
// notably in Google Test/Mock when attempting to serialize to an EXPECT_*
// or ASSERT_* call. In this case, the manual "ToString" functions above must
// be called instead.
namespace clock_operators {

// Logging convenience for durations. Outputs a string of the form "123µs".
std::ostream& operator<<(std::ostream& os,
                         const TrivialClockTraits::duration& d);

// Logging convenience for time points. Outputs a string of the form
// "123µs-ticks".
std::ostream& operator<<(std::ostream& os,
                         const TrivialClockTraits::time_point& tp);

// Logging (and gtest pretty-printing) for several commonly-used chrono types.
std::ostream& operator<<(std::ostream& os, const std::chrono::hours&);
std::ostream& operator<<(std::ostream& os, const std::chrono::minutes&);
std::ostream& operator<<(std::ostream& os, const std::chrono::seconds&);
std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds&);
std::ostream& operator<<(std::ostream& os, const std::chrono::microseconds& d);
// Note: The ostream output operator for std::chrono::microseconds is handled by
// the one for TrivialClockTraits::duration above since they are the same type.

}  // namespace clock_operators

}  // namespace openscreen

#endif  // PLATFORM_BASE_TRIVIAL_CLOCK_TRAITS_H_