From 48dee43f04ec2147d7ceb735df8b5c4e24004d5a Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Apr 2007 10:41:29 +0200 Subject: [PATCH] make sure that TagRegistry member variables are always initialized when needed. --- src/common/tags.cc | 14 +++++++++++--- src/common/tags.h | 27 +++++++++++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/common/tags.cc b/src/common/tags.cc index b508ad074..a94ea5ed0 100644 --- a/src/common/tags.cc +++ b/src/common/tags.cc @@ -23,8 +23,16 @@ namespace ns3 { -bool TagRegistry::m_sorted; -TagRegistry::TagsData TagRegistry::m_registry; +TagRegistry * +TagRegistry::GetInstance (void) +{ + static TagRegistry registry; + return ®istry; +} + +TagRegistry::TagRegistry () + : m_sorted (false) +{} void @@ -189,7 +197,7 @@ Tags::PrettyPrint (std::ostream &os) { for (struct TagData *cur = m_next; cur != 0; cur = cur->m_next) { - TagRegistry::PrettyPrint (cur->m_id, cur->m_data, os); + TagRegistry::GetInstance ()->PrettyPrint (cur->m_id, cur->m_data, os); } } diff --git a/src/common/tags.h b/src/common/tags.h index fadbe1c1f..3c9583c9a 100644 --- a/src/common/tags.h +++ b/src/common/tags.h @@ -119,15 +119,18 @@ class TagRegistry { public: typedef void (*PrettyPrinter) (uint8_t [Tags::SIZE], std::ostream &); typedef void (*Destructor) (uint8_t [Tags::SIZE]); - static void Record (std::string uuid, PrettyPrinter prettyPrinter, Destructor destructor); + void Record (std::string uuid, PrettyPrinter prettyPrinter, Destructor destructor); /** * returns a numeric integer which uniquely identifies the input string. * that integer cannot be zero which is a reserved value. */ - static uint32_t LookupUid (std::string uuid); - static void PrettyPrint (uint32_t uid, uint8_t buf[Tags::SIZE], std::ostream &os); - static void Destruct (uint32_t uid, uint8_t buf[Tags::SIZE]); + uint32_t LookupUid (std::string uuid); + void PrettyPrint (uint32_t uid, uint8_t buf[Tags::SIZE], std::ostream &os); + void Destruct (uint32_t uid, uint8_t buf[Tags::SIZE]); + + static TagRegistry *GetInstance (void); private: + TagRegistry (); struct TagInfoItem { std::string uuid; @@ -137,8 +140,8 @@ private: typedef std::vector TagsData; typedef std::vector::const_iterator TagsDataCI; static bool CompareItem (const struct TagInfoItem &a, const struct TagInfoItem &b); - static bool m_sorted; - static TagsData m_registry; + bool m_sorted; + TagsData m_registry; }; /** * The TypeUid class is used to create a mapping Type --> uid @@ -167,7 +170,8 @@ void TypeUid::Record (std::string uuid) template const uint32_t TypeUid::GetUid (void) { - static const uint32_t uid = TagRegistry::LookupUid (*(GetUuid ())); + static const uint32_t uid = TagRegistry::GetInstance ()-> + LookupUid (*(GetUuid ())); return uid; } @@ -191,7 +195,8 @@ TagRegistration::TagRegistration (std::string uuid, void (*prettyPrinter) (T { NS_ASSERT (sizeof (T) <= Tags::SIZE); m_prettyPrinter = prettyPrinter; - TagRegistry::Record (uuid, &TagRegistration::PrettyPrinterCb, &TagRegistration::DestructorCb); + TagRegistry::GetInstance ()-> + Record (uuid, &TagRegistration::PrettyPrinterCb, &TagRegistration::DestructorCb); TypeUid::Record (uuid); } template @@ -310,14 +315,16 @@ Tags::RemoveAll (void) } if (prev != 0) { - TagRegistry::Destruct (prev->m_id, prev->m_data); + TagRegistry::GetInstance ()-> + Destruct (prev->m_id, prev->m_data); FreeData (prev); } prev = cur; } if (prev != 0) { - TagRegistry::Destruct (prev->m_id, prev->m_data); + TagRegistry::GetInstance ()-> + Destruct (prev->m_id, prev->m_data); FreeData (prev); } m_next = 0;