#include <iostream>
#include <chrono>
using namespace std;
typedef std::chrono::high_resolution_clock Clock;
class Dimension
{
public:
Dimension(int _X, int _Y)
{
mX = _X;
mY = _Y;
}
private:
int mX;
int mY;
};
template <class T>
class Image
{
public:
void Draw()
{
// Dispatch call to exact type
static_cast<T*>(this)->Draw();
}
Dimension GetDimensionInPixels()
{
// Dispatch call to exact type
static_cast<T*>(this)->GetDimensionInPixels();
}
protected:
int dimensionX;
int dimensionY;
};
class PngImage : public Image<PngImage>
{
public:
void Draw()
{
}
Dimension GetDimensionInPixels()
{
return Dimension(dimensionX, dimensionY);
}
};
class BitmapImage : public Image<BitmapImage>
{
public:
void Draw()
{
}
Dimension GetDimensionInPixels()
{
return Dimension(dimensionX, dimensionY);
}
};
class TiffImage : public Image<TiffImage>
{
public:
void Draw()
{
// Uncomment this to check method dispatch
// cout << "TiffImage::Draw() called" << endl;
}
Dimension GetDimensionInPixels()
{
return Dimension(dimensionX, dimensionY);
}
};
int main() {
Image<TiffImage>* pImage = new TiffImage;
auto then = Clock::now();
// Call Draw 1000 times to make sure perf is visible
for (int i = 0; i < 1000; ++i)
pImage->Draw();
auto now = Clock::now();
cout << "Time taken: "
<< std::chrono::duration_cast<std::chrono::nanoseconds>(now - then).count()
<< " nanoseconds" << std::endl;
return 0;
}