Action Engine
Loading...
Searching...
No Matches
source_location.h
1/*
2 * Copyright 2025 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ACTIONENGINE_UTIL_SOURCE_LOCATION_H_
18#define ACTIONENGINE_UTIL_SOURCE_LOCATION_H_
19
20#include <cstdint>
21
22namespace act::util {
23
24// Class representing a specific location in the source code of a program.
25// source_location is copyable.
26class source_location {
27 public:
28 // Avoid this constructor; it populates the object with dummy values.
29 constexpr source_location() : line_(0), file_name_(nullptr) {}
30
31 // Wrapper to invoke the private constructor below. This should only be
32 // used by the ACTIONENGINE_LOC macro, hence the name.
33 static constexpr source_location DoNotInvokeDirectly(std::uint_least32_t line,
34 const char* file_name) {
35 return source_location(line, file_name);
36 }
37
38 // The line number of the captured source location.
39 [[nodiscard]] constexpr std::uint_least32_t line() const { return line_; }
40
41 // The file name of the captured source location.
42 [[nodiscard]] constexpr const char* file_name() const { return file_name_; }
43
44 // column() and function_name() are omitted because we don't have a
45 // way to support them.
46
47 private:
48 // Do not invoke this constructor directly. Instead, use the
49 // ACTIONENGINE_LOC macro below.
50 //
51 // file_name must outlive all copies of the source_location
52 // object, so in practice it should be a string literal.
53 constexpr source_location(std::uint_least32_t line, const char* file_name)
54 : line_(line), file_name_(file_name) {}
55
56 std::uint_least32_t line_;
57 const char* file_name_;
58};
59
60} // namespace act::util
61
62// If a function takes a source_location parameter, pass this as the argument.
63#define ACTIONENGINE_LOC \
64 act::util::source_location::DoNotInvokeDirectly(__LINE__, __FILE__)
65
66#endif // ACTIONENGINE_UTIL_SOURCE_LOCATION_H_