0

I've a class for my one of programs which is drawing image sequences at different positions on the window. The class has multiple instances but it's the same image sequence which is being drawn at all the positions inside the window. I want to prevent multiple instances of class initializaing multiple image sequences to avoid eating memory, for which I have the image sequence variable as static variable

class Drawer{
private:
static ImageSequence imgSequence;
};

In the .cpp file, I am doing the following to initialize the static var.

#include "Drawer.h"

ImageSequence Drawer::imgSequence = ImageSequence();

However, I have two methods to specify the path to image sequences and preload al frames - and confused about where to put these methods so that each Drawer class instantiation does not preload the frames time and again. How'd this be done in C++?

--EDIT The two methods as asked for: i) loadSequence, ii)preloadAllFrames();

    loadSequence(string prefix, string firstFrame, string lastFrame){
    for(int i=0;i<numFrames;++i){
    //adds and pushes values of all the files in the sequence to a vector
    }
}

preloadAllFrames(){
for(int i=0;i<numFrames;++i){
//create pointers to image textures and store then in a vector so that it doesn't take time to load them for drawing
}
}
user1240679
  • 6,829
  • 17
  • 60
  • 89
  • You say *I have two methods*. Please post the (simpilifed) code of those two methods, so we can better understand what you want. – pts Aug 27 '14 at 21:00
  • Please note that static initialization (e.g. `ImageSequence Drawer::imgSequence = ImageSequence();`) is unsafe, because the order between translation units is undefined. – pts Aug 27 '14 at 21:01
  • @pts: For the most part only if you use one `static` object to initialize another `static` one. Not an answer, but why not use a flyweight pattern here (like e.g. implemented in boost) instead of `static` variables? – Benjamin Bannier Aug 27 '14 at 21:04
  • You say you have two methods to specify the path to the image sequences. What logic is used to choose the method? Is it a compile time switch? Is it a run time switch? If the latter, what criteria are used to choose a method? – R Sahu Aug 27 '14 at 21:09

2 Answers2

0

Have an accompanying boolean value with the image and check if the image has been already loaded when you try to load it. You can also load it when your program is initializing only once instead of attempting to load it every frame.

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
0

Just have a static pointer instead of instance and initialize in a static method:

class Drawer{
private:
    static std::unique_ptr<ImageSequence> imgSequence;
public:
    static void initializeMethod1() 
    {
        if( imgSequence ) return; // or throw exception
        imgSequence.reset( new ImageSequence( ... ) );
        ...
    }

    static void initializeMethod2() {}
    {
        if( imgSequence ) return; // or throw exception
        imgSequence.reset( new ImageSequence( ... ) );
        ...
    }

    static ImageSequence &getSequence() 
    { 
        if( !imgSequence ) throw std::runtime_error( "image sequence is not intialized" );
        return *imgSequence;
    }
};
Slava
  • 43,454
  • 1
  • 47
  • 90
  • 1
    Note this isn't thread-safe. With C++11 a function-local `static` variable in `getSequence` would work (and might even work for some C++98 compilers). – Benjamin Bannier Aug 27 '14 at 21:06
  • @BenjaminBannier I missed where OP asked that code has to be thread safe, can you point it? – Slava Aug 27 '14 at 21:09
  • Why not if it's easy https://stackoverflow.com/questions/1661529/is-meyers-implementation-of-singleton-pattern-thread-safe? – Benjamin Bannier Aug 27 '14 at 21:16
  • @BenjaminBannier Then it is not easy to initialize it in 2 separate methods – Slava Aug 27 '14 at 21:18