next up previous
Next: Formatting Nested Output Up: Loading/Saving by Base-Class Pointer Previous: Loading/Saving by Base-Class Pointer

RD_LoaderSingleton<T>

#include "Rad/Loader.h"

// For load/save by pointer:
RD_LoaderSingleton<BaseClass>* 
      RD_LoaderSingleton<BaseClass>::_instance = NULL;

void RD_AddToLoader(const BaseClass& base)
{
    RD_LoaderSingleton<BaseClass>::instance().append(base);
}

If this is all done for DistanceBase, an example of its use is:

// Configure loader
RD_AddToLoader(L1Distance());
RD_AddToLoader(L2Distance());

DistanceBase d_ptr1 = new L1Distance();
// Save by pointer:
RD_Bfstream bfs("data.out",ios::out);
bfs<<d_ptr1;
bfs.close();

// Load by pointer:
DistanceBase d_ptr2 = NULL;
bfs.open("data.out",ios::in);
bfs>>d_ptr2;	// Invokes RD_LoaderSingleton
bfs.close();

// d_ptr2 now points to a new L1Distance object

We can then define load/save methods for NearestNeighbour:

void NearestNeighbour::save(RD_Bfstream& bfs) const
{
   bfs<<versionNo();
   bfs<<_d;   // Save by pointer
}

void NearestNeighbour::load(RD_Bfstream& bfs)
{
    delete _d; _d=NULL; // Zap current distance

    short v;
    bfs>>v;
    switch (v)
    {
        case (1):
            bfs>>_d;  // Load by pointer
            break;
        default:
            cerr<<"Unexpected version number "<<v<<endl;
            abort();
    }
}



Tim Cootes
2/24/1998