Created
January 29, 2013 23:22
-
-
Save asukharev/4668945 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TCollection.hpp | |
// | |
#ifndef TCOLLECTION_HPP_ | |
#define TCOLLECTION_HPP_ | |
#include <vector> | |
#include <assert.h> | |
template<typename TBase> | |
class TCollection { | |
public: | |
unsigned long Add(TBase& item) { | |
items_.push_back(item); | |
return (items_.size() - 1); | |
} | |
// Function to return the memory address of a specific Item | |
TBase* GetAddress(int itemKey) { | |
return &(items_[itemKey]); | |
} | |
// Remove a specific Item from the collection | |
void Remove(int itemKey) { | |
items_.erase(GetAddress(itemKey)); | |
} | |
// Clear the collection | |
void Clear(void) { | |
items_.clear(); | |
} | |
// Return the number of items in collection | |
int Count(void) { | |
return items_.size(); //One Based | |
} | |
// Operator Returning a reference to TBase | |
TBase& operator [](int itemKey) { | |
assert(itemKey >= 0 && itemKey < Count()); | |
return items_[itemKey]; | |
} | |
// Return items using the supplied function | |
std::vector<TBase> findMatching(std::function<bool(const TBase&)> func) { | |
std::vector<TBase> results; | |
for (auto itr = items_.begin(), end = items_.end(); itr != end; ++itr) { | |
// call the function passed into findMatchingAddresses and see if it matches | |
if (func(*itr)) { | |
results.push_back(*itr); | |
} | |
} | |
return results; | |
} | |
protected: | |
// The Vector container that will hold the collection of Items | |
std::vector<TBase> items_; | |
}; | |
#endif /* TCOLLECTION_HPP_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment