Skip to content

Instantly share code, notes, and snippets.

@gKodes
Created October 7, 2012 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gKodes/3847688 to your computer and use it in GitHub Desktop.
Save gKodes/3847688 to your computer and use it in GitHub Desktop.
cpp inheritance generic iterators
/*
* Copyright 2012 Kamalakar Gadireddy @ gkodes.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* itr.cc
* history
* Prototype (07.10)
*
* Ref:
* http://stackoverflow.com/questions/2191572/iterator-for-custom-container-with-derived-classes?rq=1
* http://stackoverflow.com/questions/2191724/using-iterators-to-hide-internal-container-and-achieve-generic-operation-over-a
* http://stackoverflow.com/questions/3960948/fast-and-flexible-iterator-for-abstract-class?rq=1
* http://stackoverflow.com/questions/9239489/c-using-different-iterator-types-in-subclasses-without-breaking-the-inherita
*/
#include <iterator>
#include <typeinfo>
#include <iostream>
#include <string>
class BaseItr
{
public:
class iterator : public std::iterator<std::input_iterator_tag, int>
{
public:
iterator() : _in(NULL) {}
inline iterator(const iterator& org) : _in(org._in) {}
inline iterator& operator=(const iterator& other) { _in = other._in; return *this; }
virtual inline int operator * () { return _in->operator*(); }
virtual inline iterator& operator++() { (*_in)++; return *this; }
virtual inline iterator& operator++(int unused) { (*_in)++; return *this; }
virtual inline bool operator==(const iterator& other)
{
return *(*_in) == *(*(other._in));
}
virtual inline bool operator!=(const iterator& other)
{
return *(*_in) != *(*(other._in));
}
// would use shared pointer insted of this
//~iterator() { if(_in) { delete _in; } }
static inline iterator New(iterator *in) { return iterator(in); }
private:
iterator(iterator *in) : _in(in) {}
iterator *_in;
};
virtual iterator begin() = 0;
virtual iterator end() = 0;
};
class Itr : public BaseItr
{
private:
class iterator : public BaseItr::iterator
{
public:
iterator(int val) : _val(val), BaseItr::iterator() {}
int operator * () { return _val; }
inline iterator& operator++() { ++_val; return *this; }
inline iterator& operator++(int unused) { _val++; return *this; }
private:
int _val;
};
BaseItr::iterator _begin;
BaseItr::iterator _end;
public:
inline Itr(int start, int end)
{
_begin = BaseItr::iterator::New(new iterator(start));
_end = BaseItr::iterator::New(new iterator(end));
}
BaseItr::iterator begin() { return _begin; }
BaseItr::iterator end() { return _end; }
};
int main ()
{
BaseItr *base = new Itr(10, 100);
BaseItr::iterator itr = base->begin();
BaseItr::iterator end = base->end();
//itr++;
//std::cout << *(base->end()) << std::endl;
std::copy( itr, end,
std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
//delete base;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment