summaryrefslogtreecommitdiff
path: root/src/profile.hh
blob: 7feff0179752e4f1afbb767867871860fbd3b30d (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
#ifndef profile_hh_INCLUDED
#define profile_hh_INCLUDED

#include "option.hh"
#include "clock.hh"

namespace Kakoune
{

template<typename Callback>
class ProfileScope
{
public:
    ProfileScope(const DebugFlags debug_flags, Callback&& callback, bool active = true)
      : m_active{active and debug_flags & DebugFlags::Profile},
        m_start_time(m_active ? Clock::now() : Clock::time_point{}),
        m_callback{std::move(callback)}
    {}

    ProfileScope(const Context& context, Callback&& callback, bool active = true)
      : ProfileScope{context.options()["debug"].get<DebugFlags>(), std::move(callback), active}
    {}

    ~ProfileScope()
    {
        if (m_active)
            m_callback(std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - m_start_time));
    }

private:
    bool m_active;
    Clock::time_point m_start_time;
    Callback m_callback;
};

}

#endif // profile_hh_INCLUDED