0

(C++98, gcc4.4.7) Let's say I have a database-connected container that does not provide iterators over itself, but only an indexed getter and size(), ex:

template<typename T>
class DbVector
{
public:
  const T& getValue(const size_t) const;
  size_t size() const;
};

I'd like to be able to use it in range-based algorithms. This sounds like a common problem. Does Boost provide a solution for this?

I've looked a bit into Boost.Iterator and Boost.Range, but with no success. I'm not even sure how my google query should be formulated to find anything useful.

I thought maybe there is a library, that would allow something like:

typedef Iterable<DbVector<int>,
                 &DbVector<int>::getValue,
                 &DbVector<int>::size>
        IterableDbVectorInt;

const DbVector<int>& dbVector = getDbVector();
return boost::any_of_equal(IterableDbVectorInt(dbVector), someValue);

I know I could write it myself from scratch, but I'm really trying not to reinvent the wheel.

Any boost-based ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
lucas93
  • 11
  • 1
  • 3
    Just provide `begin` and `end` free functions (with `r` and `c` variants) in the same namespace. I'm sure there's a duplicate, but I haven't found it yet. – François Andrieux Jun 18 '19 at 14:35
  • 3
    I don't know Boost, but it seems that writing own iterator for this class would be relatively easy. You just need to store current index, provide `operator++` which increments the index and provide `operator*` which returns `getValue(currentIndex)` – Yksisarvinen Jun 18 '19 at 14:37
  • Possible duplicate of [How to provide free begin/end functions for your own types](https://stackoverflow.com/questions/7582937/how-to-provide-free-begin-end-functions-for-your-own-types) – François Andrieux Jun 18 '19 at 14:38
  • [std::begin](https://en.cppreference.com/w/cpp/iterator/begin)/[std::end](https://en.cppreference.com/w/cpp/iterator/end) ? – Victor Gubin Jun 18 '19 at 14:50
  • `boost::irange(v.size()) | boost::adaptors::transformed([&v](size_t i) -> const T& { return v.getValue(i); })` maybe? – Daniel Schepler Jun 18 '19 at 20:45
  • Just noticed the restriction to gcc 4.4 / c++98 - you might be able to replace the lambda with `boost::bind(&DbVector::getValue, boost::ref(v), boost::placeholders::_1)` and then hopefully it will still work. – Daniel Schepler Jun 18 '19 at 20:52

0 Answers0