aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/vcpkg_Chrono.h
blob: e4ae121b3682af9ba44b902584f63bd350e5bba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include <chrono>
#include <string>

namespace vcpkg
{
    class ElapsedTime
    {
    public:
        static ElapsedTime createStarted();

        constexpr ElapsedTime() :m_startTick() {}

        template <class TimeUnit>
        TimeUnit elapsed() const
        {
            return std::chrono::duration_cast<TimeUnit>(std::chrono::high_resolution_clock::now() - this->m_startTick);
        }

        std::string toString() const;

    private:
        std::chrono::steady_clock::time_point m_startTick;
    };

    class Stopwatch
    {
    public:
        static Stopwatch createUnstarted();

        static Stopwatch createStarted();

        bool isRunning() const;

        const Stopwatch& start();

        const Stopwatch& stop();

        Stopwatch& reset();

        template <class TimeUnit>
        TimeUnit elapsed() const
        {
            return std::chrono::duration_cast<TimeUnit>(elapsedNanos());
        }

        std::string toString() const;

    private:
        Stopwatch();

        std::chrono::nanoseconds elapsedNanos() const;

        bool m_isRunning;
        std::chrono::nanoseconds m_elapsedNanos;
        std::chrono::steady_clock::time_point m_startTick;
    };
}