GTIRB  v2.2.0
GrammaTech Intermediate Representation for Binaries: C++ API
Offset.hpp
Go to the documentation of this file.
1 //===- Offset.hpp ------------------------------------------------*- C++-*-===//
2 //
3 // Copyright (C) 2020 GrammaTech, Inc.
4 //
5 // This code is licensed under the MIT license. See the LICENSE file in the
6 // project root for license terms.
7 //
8 // This project is sponsored by the Office of Naval Research, One Liberty
9 // Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
10 // N68335-17-C-0700. The content of the information does not necessarily
11 // reflect the position or policy of the Government and no official
12 // endorsement should be inferred.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef GTIRB_OFFSET_H
16 #define GTIRB_OFFSET_H
17 
18 #include <gtirb/Context.hpp>
19 #include <gtirb/Export.hpp>
20 #include <boost/functional/hash.hpp>
21 #include <cstdint>
22 #include <functional>
23 
24 namespace gtirb {
25 namespace proto {
26 class Offset;
27 }
28 
35 
37  uint64_t Displacement{0};
38 
40  Offset(const UUID& ElemId, uint64_t Disp)
41  : ElementId(ElemId), Displacement(Disp) {}
42 
44  Offset() = default;
45 
47  // Note: boost::uuid is not constexpr.
48  friend bool operator==(const Offset& LHS, const Offset& RHS) noexcept {
49  return LHS.ElementId == RHS.ElementId &&
50  LHS.Displacement == RHS.Displacement;
51  }
52 
54  friend bool operator!=(const Offset& LHS, const Offset& RHS) noexcept {
55  return !operator==(LHS, RHS);
56  }
57 
59  friend constexpr bool operator<(const Offset& LHS,
60  const Offset& RHS) noexcept {
61  return std::tie(LHS.ElementId, LHS.Displacement) <
62  std::tie(RHS.ElementId, RHS.Displacement);
63  }
64 
66  friend constexpr bool operator>(const Offset& LHS,
67  const Offset& RHS) noexcept {
68  return operator<(RHS, LHS);
69  }
70 
72  friend constexpr bool operator<=(const Offset& LHS,
73  const Offset& RHS) noexcept {
74  return !operator<(RHS, LHS);
75  }
76 
78  friend constexpr bool operator>=(const Offset& LHS,
79  const Offset& RHS) noexcept {
80  return !operator<(LHS, RHS);
81  }
82 
83 private:
86  using MessageType = proto::Offset;
87 
93  void toProtobuf(MessageType* Message) const;
94 
102  bool fromProtobuf(Context& C, const MessageType& Message);
104 
105  // Enables serialization.
106  friend bool fromProtobuf(Context&, Offset&, const MessageType&);
107 };
108 
109 } // namespace gtirb
110 
111 namespace std {
112 
114 template <> struct hash<gtirb::Offset> {
115  size_t operator()(const gtirb::Offset& X) const {
116  std::size_t Seed = 0;
117  boost::hash_combine(Seed, X.ElementId);
118  boost::hash_combine(Seed, X.Displacement);
119  return Seed;
120  }
121 };
122 
123 } // namespace std
124 
125 #endif // GTIRB_OFFSET_H
gtirb::UUID
boost::uuids::uuid UUID
Represents a universally unique identifier used to identify Node objects across serialization boundar...
Definition: Context.hpp:36
gtirb::Offset::Displacement
uint64_t Displacement
The displacement from the start of the node, in bytes.
Definition: Offset.hpp:37
gtirb::Context
The context under which GTIRB operations occur.
Definition: Context.hpp:63
Context.hpp
Class gtirb::Context and related operators.
gtirb::operator==
std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, bool > operator==(const ErrorOr< T > &Err, E Code)
Definition: ErrorOr.hpp:277
gtirb::Offset
Describes a location inside a node (byte interval, block, etc).
Definition: Offset.hpp:32
gtirb::Offset::operator<=
constexpr friend bool operator<=(const Offset &LHS, const Offset &RHS) noexcept
Less-than-or-equal operator for Offset.
Definition: Offset.hpp:72
GTIRB_EXPORT_API
#define GTIRB_EXPORT_API
This macro controls the visibility of exported symbols (i.e. classes) in shared libraries....
Definition: Export.hpp:52
gtirb::Offset::operator==
friend bool operator==(const Offset &LHS, const Offset &RHS) noexcept
Equality operator for Offset.
Definition: Offset.hpp:48
gtirb
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
Export.hpp
gtirb::Offset::ElementId
UUID ElementId
The UUID of the node.
Definition: Offset.hpp:34
gtirb::Offset::operator!=
friend bool operator!=(const Offset &LHS, const Offset &RHS) noexcept
Inequality operator for Offset.
Definition: Offset.hpp:54
gtirb::Offset::Offset
Offset(const UUID &ElemId, uint64_t Disp)
Constructor using a ElemId uuid and a Displacement.
Definition: Offset.hpp:40
gtirb::Offset::operator>=
constexpr friend bool operator>=(const Offset &LHS, const Offset &RHS) noexcept
Greater-than-or-equal operator for Offset.
Definition: Offset.hpp:78
gtirb::Offset::operator<
constexpr friend bool operator<(const Offset &LHS, const Offset &RHS) noexcept
Less-than operator for Offset.
Definition: Offset.hpp:59
std
Definition: Addr.hpp:359
std::hash< gtirb::Offset >::operator()
size_t operator()(const gtirb::Offset &X) const
Definition: Offset.hpp:115
gtirb::Offset::operator>
constexpr friend bool operator>(const Offset &LHS, const Offset &RHS) noexcept
Greater-than operator for Offset.
Definition: Offset.hpp:66