I have been wracking my brain over this for the last day or so. I am trying to figure out how to return an iterator from a class while hiding the type of container the class is using. An example would be I have a class canvas that holds widgets with the same interface and they are stored privately in a std::vector. So....
Simplified code
class Canvas
{
public:
WidgetIterator begin();
WidgetIterator end();
private:
class Impl;
std::unique_ptr<Impl> mImpl;
};
class Canvas::Impl
{
public:
std::vector<widget> mWidget;
//how to return mWidget.begin(), mWidget.end() up as widget iterator??
}
usage:
int main()
{
Canvas canvas();
...Fill canvas with widgets...
//iterate over widgets
for(auto& a_widget : canvas)
{
//do something with / to a_widget. User does not know or care
//that underlying iterator is std::vector<widget>::iterator
}
...Do more stuff....
return 0;
}
Basically, I would like to somehow alias mWidget.begin() and mWidget.end() up through Canvas::begin() and Canvas::end(). The user knows that the iterator is to a widget, they just don't need to know that the iterator is std::vector::iterator. I am trying to both hide my implementations using PIMPL and keep information about how things are stored within the class.
I can't seem to find the right "formula". I have looked at type-erasure and trying to return function pointers through an interface, but I can't seem to think of a way to keep std::vector::iterator out of the header, and everything I have seen so far in looking this up don't seem to fit what I am trying to do. Can someone point me in the right direction? Reading material? Is there a concept I am missing? Oh - and I have seen some use boost int he examples that I couldn't figure out how to make work in my situation. I would like to avoid that because I am trying to reduce outside dependencies.
Thank you very much in advance!!!
Aucun commentaire:
Enregistrer un commentaire