Current, uncompileable, work in progress
authorTJ <hacker@iam.tj>
Sun, 12 Apr 2015 20:54:59 +0000 (21:54 +0100)
committerTJ <hacker@iam.tj>
Sun, 12 Apr 2015 20:54:59 +0000 (21:54 +0100)
GenericStorageContainerTemplateImpl.hpp
GenericStorageContainerTest.cpp

index 4bc09f9..6bc98dd 100644 (file)
@@ -21,6 +21,7 @@
 #define _GENERIC_STORAGE_CONTAINER_TEMPLATE_IMPL_HPP_
 
 #include <iterator>
+#include <memory>
 
 namespace tj
 {
@@ -49,11 +50,9 @@ 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:
@@ -73,6 +72,7 @@ namespace tj
      * Iterator to call common methods to perform store, retrieve, and search operations
      */
 
+    public:
     template <typename S>
     class StorageClass
     {
@@ -81,6 +81,7 @@ namespace tj
       public:
       StorageClass() {};
       StorageClass(S& data) : __data(data) {};
+      virtual ~StorageClass() {};
 
       /* API available to application
        *
@@ -93,10 +94,10 @@ namespace tj
         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
@@ -133,7 +134,7 @@ namespace tj
     /* Specialised storage class implementing a 2-way Linked List
      */
     template <typename SLLD>
-    class StorageClass_DoubleLinkedList : StorageClass<SLLD> 
+    class StorageClass_LinkedListDouble : StorageClass<SLLD> 
     {
       // TODO: implement
     };
@@ -153,11 +154,18 @@ namespace tj
      * 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
@@ -264,6 +272,10 @@ namespace tj
 
   }; // 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
index bae3589..86d2667 100644 (file)
@@ -1,13 +1,54 @@
 #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;
 }