[fnd] Add List<T> template class.

This commit is contained in:
jakcron 2017-07-06 20:48:46 +10:00
parent f59068088d
commit 1515c56d2f
3 changed files with 95 additions and 0 deletions

91
lib/fnd/List.h Normal file
View file

@ -0,0 +1,91 @@
#pragma once
#include <fnd/types.h>
#include <vector>
namespace fnd
{
template <class T>
class List
{
public:
List() :
mElements()
{
}
List(const T* elements, size_t num) :
mElements(num)
{
initList(elements, num);
}
// assignment operator
const List& operator=(const List& other)
{
mElements.clear();
for (size_t i = 0; i < other.getSize(); i++)
{
mElements.push_back(other[i]);
}
return *this;
}
// comparision operator
bool operator==(const List& other) const
{
if (other.getSize() != this->getSize())
{
return false;
}
for (size_t i = 0; i < this->getSize(); i++)
{
if (getElement(i) != other[i])
{
return false;
}
}
return true;
}
bool operator!=(const List& other) const
{
return !operator==(other);
}
// access operators
const T& getElement(size_t index) const
{
return mElements[index];
}
T& getElement(size_t index)
{
if (index == mElements.size()) mElements.push_back(T());
return mElements[index];
}
const T& operator[](size_t index) const { return getElement(index); }
T& operator[](size_t index) { return getElement(index); }
const T& atBack() const { return getElement(getSize() - 1); }
T& atBack() { return getElement(getSize() - 1); }
// functions
void addElement(const T& element) { mElements.push_back(element); }
size_t getSize() const { return mElements.size(); }
void clear() { mElements.clear(); }
private:
std::vector<T> mElements;
void initList(T* elements, size_t num)
{
mElements.clear();
for (size_t i = 0; i < num; i++)
{
mElements.push_back(elements[i]);
}
}
};
}

View file

@ -118,6 +118,7 @@
<ClInclude Include="elf.h" /> <ClInclude Include="elf.h" />
<ClInclude Include="exception.h" /> <ClInclude Include="exception.h" />
<ClInclude Include="io.h" /> <ClInclude Include="io.h" />
<ClInclude Include="List.h" />
<ClInclude Include="memory_blob.h" /> <ClInclude Include="memory_blob.h" />
<ClInclude Include="string_conv.h" /> <ClInclude Include="string_conv.h" />
<ClInclude Include="types.h" /> <ClInclude Include="types.h" />

View file

@ -33,6 +33,9 @@
<ClInclude Include="io.h"> <ClInclude Include="io.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="List.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="memory_blob.cpp"> <ClCompile Include="memory_blob.cpp">