I want to wrap some c library in modern C++ way.
The library provide a way to reverse serialize objects from binary string. So that its API appears can only forward from begin of the string to the end, where the part have been processed will not be kept, just like stream.
However it works different from standard stream, it will not support "<<" operator to return char, and for loops should not return char also. It needs a iterator which can iterator on it and return objects it generated.
at first, I want to implement code just like below:
class Obj{
c_ptr ptr;
.....
}
class X{
public:
class const_iterator : std::iterator<std::forward_iterator_tag, Obj>{
......
};
class iterator : const_iterator{
.....
};
X::const_iterator cbegin();
X::iterator begin();
X::const_iterator cend();
X::iterator end();
........
}
or merge Obj class into iterator. There are problems in this case.
1.as vector iterator example shows, begin and end() should return index value. But here X is stream, I can only get the begin once, after that access stream would not the the first byte. In iostream the end() seems return a specific char EOF.
I suppose that I can not inherit X from istream? because istream seem is designed for char streams operation with lots of mechanism as overflow, etc, which are not needed for the wrapper.
iterator inherit from const_iterator is suggest by some one to reduce the similar code. But it seems still have a lots of code should be different, mainly focus on it's declaration.
Is there any best practice on this kind of container or iterator implementation in modern C++?