#ifndef TRACE_CONTEXT_ELEMENT_H #define TRACE_CONTEXT_ELEMENT_H #include #include #define NS_TRACE_CONTEXT_ELEMENT_ENSURE_REGISTERED(x) \ namespace { \ static class thisisaveryverylongclassname \ { \ public: \ thisisaveryverylongclassname () \ { uint32_t uid; uid = x::GetUid ();} \ } g_thisisanotherveryveryverylongname; \ } namespace ns3 { class TraceContextElement { protected: template static uint16_t Register (std::string name); }; } // namespace ns3 namespace ns3 { /** * \brief a registry of TraceContextElement subclasses * \internal */ class ElementRegistry { public: template static uint16_t Register (std::string name); static uint32_t GetSize (uint16_t uid); static void Print (uint16_t uid, uint8_t *instance, std::ostream &os); static void Destroy (uint16_t uid, uint8_t *instance); private: typedef void (*PrintCb) (uint8_t *instance, std::ostream &os); typedef void (*DestroyCb) (uint8_t *instance); struct Info { uint32_t size; std::string uidString; PrintCb print; DestroyCb destroy; }; typedef std::vector InfoVector; static InfoVector *GetInfoVector (void); template static void DoPrint (uint8_t *instance, std::ostream &os); template static void DoDestroy (uint8_t *instance); }; template void ElementRegistry::DoPrint (uint8_t *instance, std::ostream &os) { static T obj; // make sure we are aligned. memcpy ((void*)&obj, instance, sizeof (T)); obj.Print (os); } template void ElementRegistry::DoDestroy (uint8_t *instance) { static T obj; // make sure we are aligned. memcpy ((void*)&obj, instance, sizeof (T)); obj.~T (); } template uint16_t ElementRegistry::Register (std::string name) { InfoVector *vec = GetInfoVector (); uint16_t uid = 1; for (InfoVector::iterator i = vec->begin (); i != vec->end (); i++) { if (i->uidString == name) { return uid; } uid++; } struct Info info; info.size = sizeof (T); info.uidString = name; info.print = &ElementRegistry::DoPrint; info.destroy = &ElementRegistry::DoDestroy; vec->push_back (info); return vec->size (); } template uint16_t TraceContextElement::Register (std::string name) { return ElementRegistry::Register (name); } } // namespace ns3 #endif /* TRACE_CONTEXT_ELEMENT_H */