#define _GENERIC_STORAGE_CONTAINER_TEMPLATE_IMPL_HPP_
#include <iterator>
+#include <memory>
namespace tj
{
* correct type of iterator easier to read and write in application code.
*
*/
- template <typename T, int storage_class>
+ template <typename T, class STORAGE_CLASS>
class GenericStorageContainerTemplateImpl
{
-
-
/* XXX: Have to use different template typename value (T, S, etc.) although the types are
* the same else the compiler cannot determine the correct value to use.
* Metaprogramming does not have the concept of scope or namespace:
* Iterator to call common methods to perform store, retrieve, and search operations
*/
+ public:
template <typename S>
class StorageClass
{
public:
StorageClass() {};
StorageClass(S& data) : __data(data) {};
+ virtual ~StorageClass() {};
/* API available to application
*
return __data;
};
- S& get_first() = 0;
- S& get_previous() = 0;
- S& get_next() = 0;
- S& get_last() = 0;
+ virtual S& get_first() = 0;
+ virtual S& get_previous() = 0;
+ virtual S& get_next() = 0;
+ virtual S& get_last() = 0;
/*
* @param data the item (of type S) to be stored
/* Specialised storage class implementing a 2-way Linked List
*/
template <typename SLLD>
- class StorageClass_DoubleLinkedList : StorageClass<SLLD>
+ class StorageClass_LinkedListDouble : StorageClass<SLLD>
{
// TODO: implement
};
* so use the (abstract) base class to keep the Collection's private
* reference to the internal storage class
*/
- StorageClass<T> storage;
+ STORAGE_CLASS __storage;
public:
+ GenericStorageContainerTemplateImpl() : __storage(nullptr) {};
+ GenericStorageContainerTemplateImpl(T& data)
+ {
+
+ __storage = new STORAGE_CLASS(data);
+ };
+
/* constant and non-constant custom Standard Template Library (STL) iterators
* avoiding writing 2 separate class definitions that only differ on their
* constant vs non-constant type specifiers. This makes extensive use of
}; // end of GenericStorageContainerTemplateImpl
+ // handy short-cuts for unweildy names
+ template<typename T, class SC> using GSCTI = GenericStorageContainerTemplateImpl< T, SC>;
+ template<typename T> using SC_DA = tj::GenericStorageContainerTemplateImpl::StorageClass_DynamicArray<T>;
+
}; // end of namespace
#endif
#include "GenericStorageContainerTemplateImpl.hpp"
+#include <iostream>
using namespace tj;
+using namespace std;
int
main (int argc, char **argv, char **env)
{
+ cout << "Generic Storage Template (with Iterator) Test" << endl
+ << "Copyright 2014 TJ <hacker@iam.tj>" << endl
+ << "Licensed on terms of the GNU General Public License version 3.0" << endl;
+
int* test = new int(320596); // initial test value
- GenericStorageContainerTemplateImpl<int, Storage::dynamic_array> my_collection(test);
+ GSCTI<int, SC_DA<int>> my_collection_of_ints(*test);
+
+ unsigned menu_choice;
+
+ do
+ {
+ cout << endl << "Menu" << endl
+ << "1. Add node" << endl
+ << "2. Remove node" << endl
+ << "3. List nodes using forward Iterator" << endl
+ << "4. List nodes using reverse Interator" << endl
+ << "0. Exit" << endl;
+
+ cin >> menu_choice;
+
+ switch (menu_choice)
+ {
+ case 0: // exit
+ break;
+ case 1: // Add node
+ test = new int(76543);
+ my_collection_of_ints.add_node(test);
+ break;
+ case 2: // Remove node
+ break;
+ case 3: // List using forward Iterator
+ cout << "size = " << my_collection_of_ints.get_size();
+ for (int i : my_collection_of_ints) // XXX: C++11 'range for' automagically uses the iterator
+ {
+ cout << "Node = " << i << endl;
+ }
+ break;
+ case 4: // List using reverse Iterator
+ break;
+ }
+ } while (menu_choice > 0);
return 0;
}