xref: /aosp_15_r20/external/openscreen/platform/base/location.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PLATFORM_BASE_LOCATION_H_
6 #define PLATFORM_BASE_LOCATION_H_
7 
8 #include <stddef.h>
9 
10 #include <cassert>
11 #include <functional>
12 #include <string>
13 
14 namespace openscreen {
15 
16 // NOTE: lifted from Chromium's base Location implementation, forked to work
17 // with our base library.
18 
19 // Instances of the location class include basic information about a position
20 // in program source, for example the place where an object was constructed.
21 class Location {
22  public:
23   Location();
24   Location(const Location&);
25   Location(Location&&) noexcept;
26 
27   // Initializes the program counter
28   explicit Location(const void* program_counter);
29 
30   Location& operator=(const Location& other);
31   Location& operator=(Location&& other);
32 
33   // Comparator for hash map insertion. The program counter should uniquely
34   // identify a location.
35   bool operator==(const Location& other) const {
36     return program_counter_ == other.program_counter_;
37   }
38 
39   // The address of the code generating this Location object. Should always be
40   // valid except for default initialized Location objects, which will be
41   // nullptr.
program_counter()42   const void* program_counter() const { return program_counter_; }
43 
44   // Converts to the most user-readable form possible. This will return
45   // "pc:<hex address>".
46   std::string ToString() const;
47 
48   static Location CreateFromHere();
49 
50  private:
51   const void* program_counter_ = nullptr;
52 };
53 
54 const void* GetProgramCounter();
55 
56 #define CURRENT_LOCATION ::openscreen::Location::CreateFromHere()
57 
58 }  // namespace openscreen
59 
60 #endif  // PLATFORM_BASE_LOCATION_H_
61