Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created September 30, 2015 17:49
Show Gist options
  • Save krysseltillada/0631b87a141e2ee57f3a to your computer and use it in GitHub Desktop.
Save krysseltillada/0631b87a141e2ee57f3a to your computer and use it in GitHub Desktop.
Storage template
#include <iostream>
#include "Storage.hpp"
int main ()
{
Storage <std::string> str = {"mob.class", "entity.class", "collision.class", "projectile.class"};
Storage <std::string> str1 = str;
ptrStorage <std::string> pstr (str), pstr2 (str1);
std::cout << *pstr << std::endl;
++pstr;
++pstr;
std::cout << *pstr << std::endl;
++pstr;
std::cout << *pstr << std::endl;
if (pstr2 == pstr)
std::cout << "true" << std::endl;
return 0;
}
#ifndef STORAGE_HEADER
#define STORAGE_HEADER
#include <memory>
#include <vector>
template <typename> class ptrStorage;
template <typename> class Storage;
template <typename getType>
bool operator == (const Storage <getType> &, const Storage <getType> &);
template <typename getType>
bool operator != (const Storage <getType> &, const Storage <getType> &);
template <typename getType>
bool operator < (const Storage <getType> &, const Storage <getType> &);
template <typename getType>
bool operator > (const Storage <getType> &, const Storage <getType> &);
template <typename getType>
bool operator >= (const Storage <getType> &, const Storage <getType> &);
template <typename getType>
bool operator <= (const Storage <getType> &, const Storage <getType> &);
template <typename getType> class Storage {
friend class ptrStorage <getType>;
friend bool operator == <getType> (const Storage <getType> &, const Storage <getType> &);
friend bool operator != <getType> (const Storage <getType> &, const Storage <getType> &);
friend bool operator > <getType> (const Storage <getType> &, const Storage <getType> &);
friend bool operator < <getType> (const Storage <getType> &, const Storage <getType> &);
friend bool operator >= <getType> (const Storage <getType> &, const Storage <getType> &);
friend bool operator <= <getType> (const Storage <getType> &, const Storage <getType> &);
public:
typedef typename std::vector <getType>::iterator Iter;
typedef typename std::vector <getType>::const_iterator CIter;
Storage ();
Storage (std::initializer_list <getType>);
~Storage ();
Storage add_back (const getType &);
Iter Start ();
Iter End ();
CIter cStart () const;
CIter cEnd () const;
private:
std::shared_ptr <std::vector <getType> > data;
};
template <typename getType>
bool operator == (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data == *rhs.data;
}
template <typename getType>
bool operator != (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data != *rhs.data;
}
template <typename getType>
bool operator > (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data > *rhs.data;
}
template <typename getType>
bool operator < (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data < *rhs.data;
}
template <typename getType>
bool operator >= (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data >= *rhs.data;
}
template <typename getType>
bool operator <= (const Storage <getType> &lhs, const Storage <getType> &rhs) {
return *lhs.data <= *rhs.data;
}
template <typename getType>
Storage <getType>::Storage () :
data (std::make_shared <std::vector <getType> > ()) { }
template <typename getType>
Storage <getType>::Storage (std::initializer_list <getType> il) :
data (std::make_shared <std::vector <getType> > (il)) { }
template <typename getType>
Storage <getType>::~Storage () { }
template <typename getType>
typename std::vector <getType>::iterator Storage <getType>::Start () {
return data->begin ();
}
template <typename getType>
typename std::vector <getType>::iterator Storage <getType>::End () {
return data->end ();
}
template <typename getType>
typename std::vector <getType>::const_iterator Storage <getType>::cStart () const {
return data->cbegin ();
}
template <typename getType>
typename std::vector <getType>::const_iterator Storage <getType>::cEnd () const {
return data->cend ();
}
/******************************/
/*****************************/
template <typename> class ptrStorage;
template <typename ptrType>
bool operator == (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType>
bool operator != (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType>
bool operator < (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType>
bool operator > (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType>
bool operator >= (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType>
bool operator <= (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
template <typename ptrType> class ptrStorage {
friend bool operator == <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
friend bool operator != <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
friend bool operator > <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
friend bool operator < <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
friend bool operator >= <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
friend bool operator <= <ptrType> (const ptrStorage <ptrType> &, const ptrStorage <ptrType> &);
public:
ptrStorage (Storage <ptrType> &);
ptrStorage () = default;
ptrType operator * ();
ptrStorage &operator ++ ();
ptrStorage &operator -- ();
private:
std::weak_ptr <std::vector <ptrType> > wdata;
size_t indCount;
};
template <typename ptrType>
bool operator == (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock () == *rhs.wdata.lock ();
}
template <typename ptrType>
bool operator != (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock() != *rhs.wdata.lock ();
}
template <typename ptrType>
bool operator > (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock() > *rhs.wdata.lock ();
}
template <typename ptrType>
bool operator < (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock() < *rhs.wdata.lock ();
}
template <typename ptrType>
bool operator >= (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock() >= *rhs.wdata.lock ();
}
template <typename ptrType>
bool operator <= (const ptrStorage <ptrType> &lhs, const ptrStorage <ptrType> &rhs) {
return *lhs.wdata.lock() <= *rhs.wdata.lock ();
}
template <typename ptrType>
ptrStorage <ptrType>::ptrStorage (Storage <ptrType> &getObj) :
wdata (getObj.data), indCount (0) { }
template <typename ptrType>
ptrType ptrStorage <ptrType>::operator * () {
return *(wdata.lock ()->begin () + indCount);
}
template <typename ptrType>
ptrStorage <ptrType> &ptrStorage <ptrType>::operator ++ () {
ptrStorage <ptrType> temp = *this;
++this->indCount;
return temp;
}
template <typename ptrType>
ptrStorage <ptrType> &ptrStorage <ptrType>::operator -- () {
ptrStorage <ptrType> temp = *this;
--this->indCount;
return temp;
}
#endif // STORAGE_HEADER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment