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