GTIRB
v2.2.0
GrammaTech Intermediate Representation for Binaries: C++ API
|
Go to the documentation of this file.
16 #ifndef GTIRB_AUXDATACONTAINER_H
17 #define GTIRB_AUXDATACONTAINER_H
21 #include <boost/iterator/transform_iterator.hpp>
22 #include <boost/range/iterator_range.hpp>
23 #include <type_traits>
36 template <
typename MessageType>
struct message_has_aux_data_container {
37 static constexpr
bool value =
false;
39 template <>
struct message_has_aux_data_container<proto::IR> {
40 static constexpr
bool value =
true;
42 template <>
struct message_has_aux_data_container<proto::Module> {
43 static constexpr
bool value =
true;
47 template <
typename MessageType>
48 inline constexpr
bool message_has_aux_data_container_v =
49 message_has_aux_data_container<MessageType>::value;
57 using AuxDataSet = std::map<std::string, std::unique_ptr<gtirb::AuxData>>;
65 registerAuxDataTypeInternal(Schema::Name,
66 std::make_unique<AuxDataTypeImpl<Schema>>());
75 template <
typename Schema>
void addAuxData(
typename Schema::Type&& X) {
77 assert(checkAuxDataRegistration(
78 Schema::Name, AuxDataImpl<Schema>::staticGetApiTypeId()) &&
79 "Attempting to add AuxData with unregistered or incorrect type.");
80 this->AuxDatas[Schema::Name] =
81 std::make_unique<AuxDataImpl<Schema>>(std::move(X));
92 template <
typename Schema>
typename Schema::Type*
getAuxData() {
93 return const_cast<typename Schema::Type*
>(
105 template <
typename Schema>
const typename Schema::Type*
getAuxData()
const {
106 auto Found = this->AuxDatas.find(Schema::Name);
108 if (Found == this->AuxDatas.end())
111 AuxData& AD = *(Found->second);
114 if (AD.getApiTypeId() == AuxData::UNREGISTERED_API_TYPE_ID) {
127 assert(checkAuxDataRegistration(
128 Schema::Name, AuxDataImpl<Schema>::staticGetApiTypeId()) &&
129 "Attempting to retrieve AuxData with an unregistered type.");
134 if (AD.getApiTypeId() != AuxDataImpl<Schema>::staticGetApiTypeId()) {
135 assert(
false &&
"Attempting to retrieve AuxData with incorrect type.");
140 auto& ADI =
static_cast<AuxDataImpl<Schema>&
>(AD);
153 assert(checkAuxDataRegistration(
154 Schema::Name, AuxDataImpl<Schema>::staticGetApiTypeId()) &&
155 "Attempting to remove AuxData with an unregistered type.");
156 return this->AuxDatas.erase(Schema::Name) > 0;
168 return this->AuxDatas.erase(Name) > 0;
194 const std::string& ProtobufType;
197 const std::string& PT)
198 : Key(K), RawBytes(RB), ProtobufType(PT) {}
202 struct AccessRawData {
203 auto operator()(
const AuxDataSet::value_type& P)
const {
204 return AuxDataRaw(P.first, P.second->rawData().RawBytes,
205 P.second->rawData().ProtobufType);
219 boost::transform_iterator<AccessRawData, AuxDataSet::const_iterator>;
267 class = std::enable_if_t<message_has_aux_data_container_v<MessageType>>>
268 void toProtobuf(MessageType* Message)
const {
269 containerToProtobuf(this->AuxDatas, Message->mutable_aux_data());
279 class = std::enable_if_t<message_has_aux_data_container_v<MessageType>>>
280 void fromProtobuf(
const MessageType& Message) {
281 this->AuxDatas.clear();
282 for (
const auto& M : Message.aux_data()) {
283 std::unique_ptr<AuxData> Val;
284 std::string Key = M.first;
287 if (
const auto* ADT = lookupAuxDataType(Key)) {
288 Val = ADT->fromProtobuf(M.second);
295 Val = std::make_unique<AuxData>();
296 AuxData::fromProtobuf(*Val, M.second);
298 this->AuxDatas.insert(std::make_pair(Key, std::move(Val)));
304 AuxDataContainer(Context& C, Kind knd);
305 AuxDataContainer(Context& C, Kind knd,
const UUID& U);
311 virtual ~AuxDataType() =
default;
312 virtual std::unique_ptr<AuxData>
313 fromProtobuf(
const proto::AuxData& Message)
const = 0;
314 virtual std::size_t getApiTypeId()
const = 0;
317 template <
typename Schema>
struct AuxDataTypeImpl :
public AuxDataType {
318 std::unique_ptr<AuxData>
319 fromProtobuf(
const proto::AuxData& Message)
const override {
320 return AuxDataImpl<Schema>::fromProtobuf(Message);
323 std::size_t getApiTypeId()
const override {
324 return AuxDataImpl<Schema>::staticGetApiTypeId();
328 static void registerAuxDataTypeInternal(
const char* Name,
329 std::unique_ptr<AuxDataType> ADT);
330 static bool checkAuxDataRegistration(
const char* Name, std::size_t Id);
331 static const AuxDataType* lookupAuxDataType(
const std::string& Name);
335 #endif // GTIRB_AUXDATACONTAINER_H
const_aux_data_range aux_data() const
Return a constant range of the auxiliary data (AuxData).
Definition: AuxDataContainer.hpp:234
const_aux_data_iterator aux_data_begin() const
Return a constant iterator to the first AuxData.
Definition: AuxDataContainer.hpp:168
boost::iterator_range< const_aux_data_iterator > const_aux_data_range
Definition: AuxDataContainer.hpp:220
friend struct AuxDataTypeMap
Definition: AuxDataContainer.hpp:332
Represents the base of the Node class hierarchy.
Definition: Node.hpp:39
bool getAuxDataEmpty() const
Check: Is the number of AuxData objects in this IR zero?
Definition: AuxDataContainer.hpp:249
boost::uuids::uuid UUID
Represents a universally unique identifier used to identify Node objects across serialization boundar...
Definition: Context.hpp:36
size_t getAuxDataSize() const
Get the total number of AuxData objects in this IR.
Definition: AuxDataContainer.hpp:242
An interface for accessing the serialized form of an AuxData instance.
Definition: AuxDataContainer.hpp:183
const_aux_data_iterator aux_data_end() const
Return a constant iterator to the element following the last AuxData.
Definition: AuxDataContainer.hpp:229
AuxDataRaw(const std::string &K, const std::string &RB, const std::string &PT)
Definition: AuxDataContainer.hpp:196
std::map< std::string, std::unique_ptr< gtirb::AuxData > > AuxDataSet
Definition: AuxDataContainer.hpp:2
void addAuxData(typename Schema::Type &&X)
Add a new AuxData, transferring ownership.
Definition: AuxDataContainer.hpp:75
const std::string & RawBytes
The raw bytes of the serialized form of the AuxData.
Definition: AuxDataContainer.hpp:189
#define GTIRB_EXPORT_API
This macro controls the visibility of exported symbols (i.e. classes) in shared libraries....
Definition: Export.hpp:52
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
boost::transform_iterator< AccessRawData, AuxDataSet::const_iterator > const_aux_data_iterator
An iterator type for traversing the AuxData in this container.
Definition: AuxDataContainer.hpp:219
const Schema::Type * getAuxData() const
Get a reference to the underlying type stored in the AuxData by name.
Definition: AuxDataContainer.hpp:105
const_aux_data_iterator aux_data_begin() const
Return a constant iterator to the first AuxData.
Definition: AuxDataContainer.hpp:223
const_aux_data_iterator aux_data_end() const
Return a constant iterator to the element following the last AuxData.
Definition: AuxDataContainer.hpp:174
void clearAuxData()
Clear all AuxData from the IR.
Definition: AuxDataContainer.hpp:255
Contains the AuxData Tables and serves as a base class.
Definition: AuxDataContainer.hpp:56
bool removeAuxData()
Remove an AuxData by schema.
Definition: AuxDataContainer.hpp:152
Schema::Type * getAuxData()
Get a reference to the underlying type stored in the AuxData by name.
Definition: AuxDataContainer.hpp:92
const std::string & Key
The string name of the AuxData field.
Definition: AuxDataContainer.hpp:186
An interface for accessing the serialized form of an AuxData instance.
Definition: AuxDataContainer.hpp:128
Types and operations for auxiliary data.
bool removeAuxData(std::string Name)
Remove an AuxData by name.
Definition: AuxDataContainer.hpp:167
static void registerAuxDataType()
Register a type to be used with AuxData of the given name.
Definition: AuxDataContainer.hpp:64
boost::transform_iterator< AccessRawData, AuxDataSet::const_iterator > const_aux_data_iterator
An iterator type for traversing the AuxData in this container.
Definition: AuxDataContainer.hpp:164