VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
cpu_profiler_guard.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file cpu_profiler_guard.h
26 * @brief RAII guard that automatically calls CpuProfiler::begin() and CpuProfiler::end().
27 *
28 * @details
29 * @c CpuProfilerGuard is a lightweight RAII wrapper that calls @c CpuProfiler::begin()
30 * in its constructor and @c CpuProfiler::end() in its destructor. This ensures the
31 * active interval is always closed, even if an exception is thrown.
32 *
33 * @par Example
34 * @code
35 * vlink::CpuProfiler profiler;
36 *
37 * void process_frame() {
38 * vlink::CpuProfilerGuard guard(&profiler);
39 * // ... do work ...
40 * } // profiler.end() called here automatically
41 *
42 * double usage = profiler.get();
43 * @endcode
44 *
45 * @note
46 * - Passing @c nullptr as the profiler pointer is safe; both constructor and destructor
47 * check for @c nullptr before calling any method.
48 * - The guard is non-copyable and non-movable; it must be used as a stack-allocated object.
49 * - Use @c CpuProfiler::is_global_enabled() to skip guard construction when profiling
50 * is disabled globally, reducing overhead in hot paths.
51 */
52
53#pragma once
54
55#include "./macros.h"
56
57namespace vlink {
58
59/**
60 * @class CpuProfilerGuard
61 * @brief RAII scope guard that brackets a @c CpuProfiler active interval.
62 *
63 * @details
64 * Calls @c profiler->begin() on construction and @c profiler->end() on destruction.
65 * Handles @c nullptr profiler gracefully (no-op).
66 */
68 public:
69 /**
70 * @brief Constructs the guard and calls @c profiler->begin().
71 *
72 * @param profiler Pointer to the @c CpuProfiler to bracket.
73 * If @c nullptr, both construction and destruction are no-ops.
74 */
75 explicit CpuProfilerGuard(class CpuProfiler* profiler) noexcept;
76
77 /**
78 * @brief Destructor -- calls @c profiler->end() to close the active interval.
79 */
81
82 private:
83 class CpuProfiler* profiler_;
84
86};
87
88} // namespace vlink
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184