|
| 1 | +// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved. |
| 2 | +#pragma once |
| 3 | +#include "AutoFilterDescriptor.h" |
| 4 | +#include "AutoPacket.h" |
| 5 | +#include "AutoPacketFactory.h" |
| 6 | +#include "Autowired.h" |
| 7 | +#include "AutowiringEvents.h" |
| 8 | +#include "CoreRunnable.h" |
| 9 | +#include STL_UNORDERED_MAP |
| 10 | + |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Represents an edge in the graph from a type to an AutoFilter |
| 14 | +/// </summary> |
| 15 | +struct DeliveryEdge |
| 16 | +{ |
| 17 | + // The type info |
| 18 | + const std::type_info* type_info; |
| 19 | + |
| 20 | + // The AutoFilterDescriptor |
| 21 | + AutoFilterDescriptor descriptor; |
| 22 | + |
| 23 | + // Specifies if the argument is an input (type -> descriptor) or output (descriptor -> type) |
| 24 | + bool input; |
| 25 | + |
| 26 | + // For the unordered map/hash comparison |
| 27 | + bool operator==(const DeliveryEdge& rhs) const { |
| 28 | + return |
| 29 | + type_info == rhs.type_info && |
| 30 | + descriptor == rhs.descriptor && |
| 31 | + input == rhs.input; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +/// <summary> |
| 36 | +/// Using the same hash function as the AutoFilterDescriptor |
| 37 | +/// </summary> |
| 38 | +namespace std { |
| 39 | + template<> |
| 40 | + struct hash<DeliveryEdge> |
| 41 | + { |
| 42 | + size_t operator()(const DeliveryEdge& edge) const { |
| 43 | + return (size_t) edge.descriptor.GetAutoFilter()->ptr(); |
| 44 | + } |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +/// <summary> |
| 49 | +/// Graphical visualization of AutoPackets |
| 50 | +/// </summary> |
| 51 | +class AutoPacketGraph: |
| 52 | + public AutowiringEvents, |
| 53 | + public CoreRunnable |
| 54 | +{ |
| 55 | +public: |
| 56 | + AutoPacketGraph(); |
| 57 | + |
| 58 | + typedef std::unordered_map<DeliveryEdge, size_t, std::hash<DeliveryEdge>> t_deliveryEdges; |
| 59 | + |
| 60 | +protected: |
| 61 | + // A mapping of an edge to the number of times it was delivered |
| 62 | + t_deliveryEdges m_deliveryGraph; |
| 63 | + |
| 64 | + // A lock for this type |
| 65 | + mutable std::mutex m_lock; |
| 66 | + |
| 67 | + // Reference to the AutoPacketFactory |
| 68 | + AutoRequired<AutoPacketFactory> m_factory; |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Demangle a type name as well as stripping "auto_in< >" |
| 72 | + /// </summary> |
| 73 | + /// <remarks> |
| 74 | + /// The ">" that encloses the templates will have extra spaces between them, for instance |
| 75 | + /// |
| 76 | + /// auto_in<Class> |
| 77 | + /// auto_in<Class<T> > |
| 78 | + /// auto_in<Class1<Class2, Class3<Class4> > > |
| 79 | + /// |
| 80 | + /// All we care about is matching "^auto_in<(.*)>$" |
| 81 | + /// </remarks> |
| 82 | + std::string DemangleTypeName(const std::type_info* type_info) const; |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Scan all of the objects and add any AutoFilter's from all of the objects in a system. |
| 86 | + /// </summary> |
| 87 | + /// <remarks> |
| 88 | + /// This function will scan all of the objects (and rescan) and only add new edges to our graph. |
| 89 | + /// </remarks> |
| 90 | + void LoadEdges(); |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Record the delivery of a packet and increment the number of times the packet has been delivered |
| 94 | + /// </summary> |
| 95 | + void RecordDelivery(const std::type_info* ti, const AutoFilterDescriptor& descriptor, bool input); |
| 96 | + |
| 97 | + /// AutowiringEvents overrides |
| 98 | + virtual void NewContext(CoreContext&) override {} |
| 99 | + virtual void ExpiredContext(CoreContext&) override {} |
| 100 | + virtual void EventFired(CoreContext&, const std::type_info&) override {} |
| 101 | + virtual void NewObject(CoreContext&, const ObjectTraits&) override; |
| 102 | + |
| 103 | + /// CoreRunnable overrides |
| 104 | + virtual bool OnStart(void) override; |
| 105 | + |
| 106 | +public: |
| 107 | + /// <summary> |
| 108 | + /// Get a copy of the packet via AutoFilter |
| 109 | + /// </summary> |
| 110 | + void AutoFilter(AutoPacket& packet); |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Write the graph to a file in graphviz format |
| 114 | + /// </summary> |
| 115 | + /// <param name="filename"> |
| 116 | + /// The name of the file to write the graph to |
| 117 | + /// </param> |
| 118 | + /// <param name="numPackets"> |
| 119 | + /// Include the number of times the packet was delivered |
| 120 | + /// </param> |
| 121 | + bool WriteGV(const std::string& filename, bool numPackets = false) const; |
| 122 | +}; |
0 commit comments