(Following up on this discussion : http://www.daniweb.com/software-development/cpp/threads/405285/1732795#post1732795 )
Is it ok to store data in a Visitor class? In the case below, I have a Visitor named PixelWisePatchPairVisitor that internally needs to itself use a visitor. At some point the Image to visit needs to come into the picture. In this case I gave PixelWisePatchPairVisitor a pointer to the Image and then passed the image as a function argument to void VisitAllPixelPairs(TImage* image, TPixelPairVisitor& visitor) const. Is this ok?
#include <iostream>
#include <vector>
template <typename TPixel>
class PixelPairVisitor
{
public:
virtual void Visit(const TPixel& pixel1, const TPixel& pixel2) = 0;
};
template <typename TPixel>
class SumOfDifferencePixelPairVisitor
{
public:
SumOfDifferencePixelPairVisitor() : Sum(0) {}
void Visit(const TPixel& pixel1, const TPixel& pixel2)
{
Sum += pixel1 - pixel2;
}
private:
TPixel Sum;
};
class PatchPair; // Forward declaration
class PatchPairVisitor
{
public:
virtual void Visit(const PatchPair& patchPair) = 0;
};
class Region
{
};
class Patch
{
Region region;
};
class PatchPair
{
public:
template <typename TImage, typename TPixelPairVisitor>
void VisitAllPixelPairs(TImage* image, TPixelPairVisitor& visitor) const
{
for(int i = 0; i < 10; ++i)
{
// These need to be pixels from an image
float sourcePixel = image->GetPixel(i);
float targetPixel = image->GetPixel(i);
visitor.Visit(sourcePixel, targetPixel);
}
}
private:
Patch SourcePatch;
Patch TargetPatch;
};
template <typename TImage, typename TPixelPairVisitor>
class PixelWisePatchPairVisitor : public PatchPairVisitor
{
public:
PixelWisePatchPairVisitor(TImage* image) : InternalImage(image){}
TPixelPairVisitor InternalPixelPairVisitor;
void Visit(const PatchPair& patchPair)
{
patchPair.VisitAllPixelPairs(InternalImage, InternalPixelPairVisitor); // How does 'image' get here?
}
private:
TImage* InternalImage;
};
class PatchPairContainer
{
public:
void VisitAllPatchPairs(PatchPairVisitor& visitor)
{
for(unsigned int i = 0; i < this->PatchPairs.size(); ++i)
{
visitor.Visit(this->PatchPairs[i]);
}
}
private:
std::vector<PatchPair> PatchPairs;
};
template <typename TPixel>
class Image
{
public:
TPixel GetPixel(int i)
{
return 1; // Clearly this would return actual values
}
private:
std::vector<TPixel> Pixels;
};
int main()
{
PatchPairContainer patchPairs;
Image<float>* image = new Image<float>;
PixelWisePatchPairVisitor<Image<float>, SumOfDifferencePixelPairVisitor<float> > visitor(image);
patchPairs.VisitAllPatchPairs(visitor);
return 0;
}
I could have instead made both iterators depend on the image and then none of the function calls would need to be passed the image:
VisitAllPixelPairs(TPixelPairVisitor& visitor) // as long as 'visitor' has the Image, we're good
Any thoughts on if this is ok to do?