From 288d2280fa85225613534ae56a59e35465bbefe2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 19 Apr 2015 14:35:04 +0200 Subject: Refactored cWebPlugin for C++11 style and proper WebTab clearing. --- src/Bindings/WebPlugin.cpp | 110 +++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 38 deletions(-) (limited to 'src/Bindings/WebPlugin.cpp') diff --git a/src/Bindings/WebPlugin.cpp b/src/Bindings/WebPlugin.cpp index 5759b20e7..1eca7de93 100644 --- a/src/Bindings/WebPlugin.cpp +++ b/src/Bindings/WebPlugin.cpp @@ -24,75 +24,82 @@ cWebPlugin::cWebPlugin() cWebPlugin::~cWebPlugin() { + ASSERT(m_Tabs.empty()); // Has ClearTabs() been called? + + // Remove from WebAdmin: cWebAdmin * WebAdmin = cRoot::Get()->GetWebAdmin(); if (WebAdmin != nullptr) { WebAdmin->RemovePlugin(this); } +} + + + - for (TabList::iterator itr = m_Tabs.begin(); itr != m_Tabs.end(); ++itr) + +cWebPlugin::cTabNames cWebPlugin::GetTabNames(void) const +{ + std::list< std::pair> NameList; + for (auto itr = m_Tabs.cbegin(), end = m_Tabs.cend(); itr != end; ++itr) { - delete *itr; + NameList.push_back(std::make_pair((*itr)->m_Title, (*itr)->m_SafeTitle)); } - m_Tabs.clear(); + return NameList; } -std::list > cWebPlugin::GetTabNames(void) +cWebPlugin::cTabPtr cWebPlugin::GetTabBySafeTitle(const AString & a_SafeTitle) const { - std::list< std::pair< AString, AString > > NameList; - for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr) + cCSLock Lock(m_CSTabs); + for (auto itr = m_Tabs.cbegin(), end = m_Tabs.cend(); itr != end; ++itr) { - std::pair< AString, AString > StringPair; - StringPair.first = (*itr)->Title; - StringPair.second = (*itr)->SafeTitle; - NameList.push_back( StringPair); + if ((*itr)->m_SafeTitle == a_SafeTitle) + { + return *itr; + } } - return NameList; + return nullptr; } -std::pair< AString, AString > cWebPlugin::GetTabNameForRequest(const HTTPRequest * a_Request) +std::pair cWebPlugin::GetTabNameForRequest(const HTTPRequest & a_Request) { - std::pair< AString, AString > Names; - AStringVector Split = StringSplit(a_Request->Path, "/"); + AStringVector Split = StringSplit(a_Request.Path, "/"); + if (Split.empty()) + { + return std::make_pair(AString(), AString()); + } - if (Split.size() > 1) + cCSLock Lock(m_CSTabs); + cTabPtr Tab; + if (Split.size() > 2) // If we got the tab name, show that page { - sWebPluginTab * Tab = nullptr; - if (Split.size() > 2) // If we got the tab name, show that page + for (auto itr = m_Tabs.cbegin(), end = m_Tabs.cend(); itr != end; ++itr) { - for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr) + if ((*itr)->m_SafeTitle.compare(Split[2]) == 0) // This is the one! { - if ((*itr)->SafeTitle.compare(Split[2]) == 0) // This is the one! - { - Tab = *itr; - break; - } - } - } - else // Otherwise show the first tab - { - if (GetTabs().size() > 0) - { - Tab = *GetTabs().begin(); + return std::make_pair((*itr)->m_Title, (*itr)->m_SafeTitle); } } + // Tab name not found, display an "empty" page: + return std::make_pair(AString(), AString()); + } - if (Tab != nullptr) - { - Names.first = Tab->Title; - Names.second = Tab->SafeTitle; - } + // Show the first tab: + if (!m_Tabs.empty()) + { + return std::make_pair(m_Tabs.front()->m_SafeTitle, m_Tabs.front()->m_SafeTitle); } - return Names; + // No tabs at all: + return std::make_pair(AString(), AString()); } @@ -101,14 +108,16 @@ std::pair< AString, AString > cWebPlugin::GetTabNameForRequest(const HTTPRequest AString cWebPlugin::SafeString(const AString & a_String) { AString RetVal; - for (unsigned int i = 0; i < a_String.size(); ++i) + auto len = a_String.size(); + RetVal.reserve(len); + for (size_t i = 0; i < len; ++i) { char c = a_String[i]; if (c == ' ') { c = '_'; } - RetVal.push_back( c); + RetVal.push_back(c); } return RetVal; } @@ -116,3 +125,28 @@ AString cWebPlugin::SafeString(const AString & a_String) + +void cWebPlugin::AddNewWebTab(const AString & a_Title, int a_UserData) +{ + auto Tab = std::make_shared(a_Title, a_UserData); + cCSLock Lock(m_CSTabs); + m_Tabs.push_back(Tab); +} + + + + + +void cWebPlugin::ClearTabs(void) +{ + // Remove the webadmin tabs: + cTabPtrs Tabs; + { + cCSLock Lock(m_CSTabs); + std::swap(Tabs, m_Tabs); + } +} + + + + -- cgit v1.2.3