Switch DataViewerCollection to use vector instead of map.

This commit is contained in:
Sid Dahiya 2019-08-27 14:13:13 -07:00
Родитель 83f5c3d2a9
Коммит 66c6f553ea
3 изменённых файлов: 63 добавлений и 28 удалений

Просмотреть файл

@ -1,4 +1,5 @@
#include "DataViewerCollection.hpp"
#include <algorithm>
#include <mutex>
namespace ARIASDK_NS_BEGIN {
@ -14,7 +15,7 @@ namespace ARIASDK_NS_BEGIN {
for(const auto& viewer : m_dataViewerCollection)
{
// Task 3568800: Integrate ThreadPool to IDataViewerCollection
viewer.second->RecieveData(packetData);
viewer->RecieveData(packetData);
}
};
@ -26,12 +27,14 @@ namespace ARIASDK_NS_BEGIN {
}
std::lock_guard<std::mutex> lock(m_dataViewerMapLock);
if (m_dataViewerCollection.find(dataViewer->GetName()) != m_dataViewerCollection.end())
auto lookupResult = IsViewerInCollection(dataViewer);
if (lookupResult != nullptr)
{
throw std::invalid_argument(std::string { "Viewer: '%s' is already registered", dataViewer->GetName() });
}
m_dataViewerCollection.emplace(dataViewer->GetName(), dataViewer);
m_dataViewerCollection.push_back(dataViewer);
}
void DataViewerCollection::UnregisterViewer(const char* viewerName)
@ -42,12 +45,17 @@ namespace ARIASDK_NS_BEGIN {
}
std::lock_guard<std::mutex> lock(m_dataViewerMapLock);
if (m_dataViewerCollection.find(viewerName) == m_dataViewerCollection.end())
auto toErase = std::find_if(m_dataViewerCollection.begin(), m_dataViewerCollection.end(), [&viewerName](std::shared_ptr<IDataViewer> viewer)
{
return viewer->GetName() == viewerName;
});
if (toErase == m_dataViewerCollection.end())
{
throw std::invalid_argument(std::string { "Viewer: '%s' is not currently registered", viewerName });
}
m_dataViewerCollection.erase(viewerName);
m_dataViewerCollection.erase(toErase);
}
void DataViewerCollection::UnregisterAllViewers()
@ -64,7 +72,7 @@ namespace ARIASDK_NS_BEGIN {
}
std::lock_guard<std::mutex> lock(m_dataViewerMapLock);
return m_dataViewerCollection.find(viewerName) != m_dataViewerCollection.end();
return IsViewerInCollection(viewerName) != nullptr;
}
bool DataViewerCollection::IsViewerEnabled() const noexcept
@ -72,4 +80,19 @@ namespace ARIASDK_NS_BEGIN {
std::lock_guard<std::mutex> lock(m_dataViewerMapLock);
return m_dataViewerCollection.empty() == false;
}
const std::shared_ptr<IDataViewer> DataViewerCollection::IsViewerInCollection(const char* viewerName) const noexcept
{
auto lookup = std::find_if(m_dataViewerCollection.begin(), m_dataViewerCollection.end(), [&viewerName](std::shared_ptr<IDataViewer> viewer)
{
return viewer->GetName() == viewerName;
});
return lookup != m_dataViewerCollection.end() ? *lookup : nullptr;
}
const std::shared_ptr<IDataViewer> DataViewerCollection::IsViewerInCollection(const std::shared_ptr<IDataViewer>& viewer) const noexcept
{
return IsViewerInCollection(viewer->GetName());
}
} ARIASDK_NS_END

Просмотреть файл

@ -3,11 +3,11 @@
#define DATAVIEWERCOLLECTIONIMPL_HPP
#include "public/IDataViewerCollection.hpp"
#include "pal/DebugTrace.hpp"
#include "pal/PAL.hpp"
#include "public/ctmacros.hpp"
#include <mutex>
#include <map>
#include <vector>
namespace ARIASDK_NS_BEGIN {
@ -32,7 +32,9 @@ namespace ARIASDK_NS_BEGIN {
mutable std::mutex m_dataViewerMapLock;
protected:
std::map<const char*, std::shared_ptr<IDataViewer>> m_dataViewerCollection;
std::vector<std::shared_ptr<IDataViewer>> m_dataViewerCollection;
const std::shared_ptr<IDataViewer> IsViewerInCollection(const char* viewerName) const noexcept;
const std::shared_ptr<IDataViewer> IsViewerInCollection(const std::shared_ptr<IDataViewer>& viewer) const noexcept;
};
} ARIASDK_NS_END

Просмотреть файл

@ -37,13 +37,15 @@ public:
using DataViewerCollection::UnregisterViewer;
using DataViewerCollection::UnregisterAllViewers;
using DataViewerCollection::IsViewerEnabled;
using DataViewerCollection::IsViewerInCollection;
std::map<const char*, std::shared_ptr<IDataViewer>>& GetCollection()
std::vector<std::shared_ptr<IDataViewer>>& GetCollection()
{
return m_dataViewerCollection;
}
};
TEST(DataViewerCollectionImplTests, RegisterViewer_DataViewerIsNullptr_ThrowsInvalidArgumentException)
{
TestDataViewerCollectionImpl dataViewerCollection { };
@ -62,7 +64,7 @@ TEST(DataViewerCollectionImplTests, RegisterViewer_sharedDataViewerRegistered_sh
std::shared_ptr<IDataViewer> viewer = std::make_shared<MockIDataViewer>("sharedName");
TestDataViewerCollectionImpl dataViewerCollection { };
ASSERT_NO_THROW(dataViewerCollection.RegisterViewer(viewer));
ASSERT_NE(dataViewerCollection.GetCollection().find(viewer->GetName()), dataViewerCollection.GetCollection().end());
ASSERT_TRUE(dataViewerCollection.IsViewerInCollection(viewer->GetName()));
}
TEST(DataViewerCollectionImplTests, RegisterViewer_MultiplesharedDataViewersRegistered_sharedDataViewersRegisteredCorrectly)
@ -79,10 +81,10 @@ TEST(DataViewerCollectionImplTests, RegisterViewer_MultiplesharedDataViewersRegi
ASSERT_NO_THROW(dataViewerCollection.RegisterViewer(viewer4));
ASSERT_EQ(dataViewerCollection.GetCollection().size(), 4);
ASSERT_NE(dataViewerCollection.GetCollection().find(viewer1->GetName()), dataViewerCollection.GetCollection().end());
ASSERT_NE(dataViewerCollection.GetCollection().find(viewer2->GetName()), dataViewerCollection.GetCollection().end());
ASSERT_NE(dataViewerCollection.GetCollection().find(viewer3->GetName()), dataViewerCollection.GetCollection().end());
ASSERT_NE(dataViewerCollection.GetCollection().find(viewer4->GetName()), dataViewerCollection.GetCollection().end());
ASSERT_TRUE(dataViewerCollection.IsViewerInCollection(viewer1->GetName()));
ASSERT_TRUE(dataViewerCollection.IsViewerInCollection(viewer2->GetName()));
ASSERT_TRUE(dataViewerCollection.IsViewerInCollection(viewer3->GetName()));
ASSERT_TRUE(dataViewerCollection.IsViewerInCollection(viewer4->GetName()));
}
TEST(DataViewerCollectionImplTests, RegisterViewer_DuplicateDataViewerRegistered_ThrowsInvalidArgumentException)
@ -110,8 +112,10 @@ TEST(DataViewerCollectionImplTests, UnregisterViewer_ViewerNameIsNotRegistered_T
TEST(DataViewerCollectionImplTests, UnregisterViewer_ViewerNameIsRegistered_UnregistersCorrectly)
{
std::shared_ptr<IDataViewer> viewer = std::make_shared<MockIDataViewer>("sharedName");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer->GetName(), viewer);
dataViewerCollection.GetCollection().push_back(viewer);
ASSERT_NO_THROW(dataViewerCollection.UnregisterViewer(viewer->GetName()));
ASSERT_TRUE(dataViewerCollection.GetCollection().empty());
}
@ -125,8 +129,10 @@ TEST(DataViewerCollectionImplTests, UnregisterAllViewers_NoViewersRegistered_Unr
TEST(DataViewerCollectionImplTests, UnregisterAllViewers_OneViewerRegistered_UnregisterCallSuccessful)
{
std::shared_ptr<IDataViewer> viewer = std::make_shared<MockIDataViewer>("sharedName");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer->GetName(), viewer);
dataViewerCollection.GetCollection().push_back(viewer);
ASSERT_NO_THROW(dataViewerCollection.UnregisterAllViewers());
ASSERT_TRUE(dataViewerCollection.GetCollection().empty());
}
@ -136,10 +142,12 @@ TEST(DataViewerCollectionImplTests, UnregisterAllViewers_ThreeViewersRegistered_
std::shared_ptr<IDataViewer> viewer1 = std::make_shared<MockIDataViewer>("sharedName1");
std::shared_ptr<IDataViewer> viewer2 = std::make_shared<MockIDataViewer>("sharedName2");
std::shared_ptr<IDataViewer> viewer3 = std::make_shared<MockIDataViewer>("sharedName3");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer1->GetName(), viewer1);
dataViewerCollection.GetCollection().emplace(viewer2->GetName(), viewer2);
dataViewerCollection.GetCollection().emplace(viewer3->GetName(), viewer3);
dataViewerCollection.GetCollection().push_back(viewer1);
dataViewerCollection.GetCollection().push_back(viewer2);
dataViewerCollection.GetCollection().push_back(viewer3);
ASSERT_NO_THROW(dataViewerCollection.UnregisterAllViewers());
ASSERT_TRUE(dataViewerCollection.GetCollection().empty());
}
@ -160,7 +168,7 @@ TEST(DataViewerCollectionImplTests, IsViewerEnabled_SingleViewerIsRegistered_Ret
{
std::shared_ptr<IDataViewer> viewer = std::make_shared<MockIDataViewer>("sharedName");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer->GetName(), viewer);
dataViewerCollection.GetCollection().push_back(viewer);
ASSERT_TRUE(dataViewerCollection.IsViewerEnabled(viewer->GetName()));
}
@ -169,10 +177,12 @@ TEST(DataViewerCollectionImplTests, IsViewerEnabled_MultipleViewersRegistered_Re
std::shared_ptr<IDataViewer> viewer1 = std::make_shared<MockIDataViewer>("sharedName1");
std::shared_ptr<IDataViewer> viewer2 = std::make_shared<MockIDataViewer>("sharedName2");
std::shared_ptr<IDataViewer> viewer3 = std::make_shared<MockIDataViewer>("sharedName3");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer1->GetName(), viewer1);
dataViewerCollection.GetCollection().emplace(viewer2->GetName(), viewer2);
dataViewerCollection.GetCollection().emplace(viewer3->GetName(), viewer3);
dataViewerCollection.GetCollection().push_back(viewer1);
dataViewerCollection.GetCollection().push_back(viewer2);
dataViewerCollection.GetCollection().push_back(viewer3);
ASSERT_TRUE(dataViewerCollection.IsViewerEnabled("sharedName3"));
}
@ -186,7 +196,7 @@ TEST(DataViewerCollectionImplTests, IsViewerEnabledNoParam_SingleViewerIsRegiste
{
std::shared_ptr<IDataViewer> viewer = std::make_shared<MockIDataViewer>("sharedName");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer->GetName(), viewer);
dataViewerCollection.GetCollection().push_back(viewer);
ASSERT_TRUE(dataViewerCollection.IsViewerEnabled());
}
@ -196,8 +206,8 @@ TEST(DataViewerCollectionImplTests, IsViewerEnabledNoParam_MultipleViewersRegist
std::shared_ptr<IDataViewer> viewer2 = std::make_shared<MockIDataViewer>("sharedName2");
std::shared_ptr<IDataViewer> viewer3 = std::make_shared<MockIDataViewer>("sharedName3");
TestDataViewerCollectionImpl dataViewerCollection { };
dataViewerCollection.GetCollection().emplace(viewer1->GetName(), viewer1);
dataViewerCollection.GetCollection().emplace(viewer2->GetName(), viewer2);
dataViewerCollection.GetCollection().emplace(viewer3->GetName(), viewer3);
dataViewerCollection.GetCollection().push_back(viewer1);
dataViewerCollection.GetCollection().push_back(viewer2);
dataViewerCollection.GetCollection().push_back(viewer3);
ASSERT_TRUE(dataViewerCollection.IsViewerEnabled());
}