PDA

Visualizza la versione completa : [c++]risorse e template


giuseppe500
01-08-2010, 17:35
ciao, sto studiando un sistema di cache , costruito con una classe catalogo che brevemente se la risorsa non esiste la crea e la mette nel catalogo , se invece la risorsa esiste , prende il puntatore alla risorsa da una mappa e lo restituisce al chiamante.
Questo si puo fare perch tutte le classi in questa applicazione derivano da un oggetto comune CObject e hanno la stessa interfaccia base.

Mi ritrovo pero ' ad un problema :
questa la parte del catalog che cerca e se non c' istanzia e restituisce la risorsa:
[code]

template <class T>
T* Catalog<T>::Find (const std::string& rkName)
{
// Attempt to find the object in the catalog.
MapIterator pkIter = m_kEntry.find(rkName);
if (pkIter != m_kEntry.end())
{
// The object exists in the catalog, so return it.
return pkIter->second;
}

// Attempt to load the object from disk.
T* ptObject = T::Load(rkName);
if (ptObject)
{
// The object exists on disk and is already in the catalog. The
// (name,program) pair was automatically inserted into m_kEntry by
// T::Load, so there is no need to insert it again explicitly.
return ptObject;
}

// The program does not exist.
return 0;
}

[code]

Una delle mie risorse , la Mesh ha un metodo Load per caricarsi con due parametri , mentre tutte le altre ne hanno solo 1 .
E' possibile specializzare il template e fare in modo che se di tipo Node si comporta in modo diverso?
se si come?
Grazie.

shodan
01-08-2010, 18:57
Probabilmente si, per dovresti postare l'interfaccia del template generico.

giuseppe500
01-08-2010, 20:22
penso sia questa:


// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 4.10.0 (2009/11/18)

#ifndef WM4CATALOG_H
#define WM4CATALOG_H

#include "Wm4GraphicsLIB.h"
#include "Wm4System.h"

namespace Wm4
{

template <class T>
class Catalog
{
public:
// The default catalog (for each class type Image, Texture, VertexProgram,
// PixelProgram) is created in Main::Initialize and destroyed in
// Main::Terminate (see Wm4Main.cpp). The default catalog is stored in
// ms_pkActive.
//
// Additional catalogs may be created by the application and are stored in
// ms_apkCatalogs. The catalogs are inserted into the array by the
// constructor call and removed from the array by the destructor call.
// Therefore, the default catalog is the first entry to be added to the
// array (when created in Main::Initialize) and the last entry to be
// removed from the array (when destroyed in Main::Terminate).

Catalog (const std::string& rkName);
~Catalog ();

const std::string& GetName () const;
bool Insert (T* ptObject);
bool Remove (T* ptObject);
T* Find (const std::string& rkName);
bool PrintContents (const std::string& rkFilename) const;

// If the application changes the active catalog and wishes to restore the
// previous catalog, it should do the following:
// Catalog<T>* pkPreviousCatalog = Catalog<T>::GetActive();
// Catalog<T>::SetActive(pkMyCatalog);
// <operations involving the active catalog)>
// Catalog<T>::SetActive(pkPreviousCatalog);
static void SetActive (Catalog* pkActive);
static Catalog* GetActive ();

// Operations applied to the array of catalogs. Objects of class T are
// inserted into the active catalog during their construction. Because
// the active catalog at the time of destruction might be different, it
// is necessary for the destructors to call RemoveAll. The RemoveAll
// function call iterates over the array of catalogs and attempts to
// remove the object. It is possible for an application to insert the
// object into multiple catalogs, so RemoveAll must search all catalogs.
static Catalog* GetCatalog(const std::string& rkName);
static void SetActive(const std::string& rkName);
static bool RemoveAll(T* ptObject);

private:
std::string m_kName;

typedef std::map<std::string,T*> Map;
typedef typename Map::iterator MapIterator;
typedef typename Map::const_iterator MapCIterator;
Map m_kEntry;

// The default catalog for each class type (Image, Texture,
// VertexProgram, PixelProgram) is created in Main::Initialize and
// destroyed in Main::Terminate (see Wm4Main.cpp).
WM4_GRAPHICS_ITEM static Catalog* ms_pkActive;

// Additional catalogs may be created by the application. This array does
// not contain the default catalog created by class Main.
WM4_GRAPHICS_ITEM static std::vector<Catalog*> ms_apkCatalogs;
};

#include "Wm4Catalog.inl"

}

#endif

shodan
01-08-2010, 21:18
La specializzazione diventa (con annessa funzione in fondo):


template <> class Catalog<Node>
{
public:

Catalog (const std::string& rkName);
~Catalog ();

const std::string& GetName () const;
bool Insert (Node* ptObject);
bool Remove (Node* ptObject);
Node* Find (const std::string& rkName);
bool PrintContents (const std::string& rkFilename) const;

static void SetActive (Catalog* pkActive);
static Catalog* GetActive ();

static Catalog* GetCatalog(const std::string& rkName);
static void SetActive(const std::string& rkName);
static bool RemoveAll(Node* ptObject);

private:
std::string m_kName;

typedef std::map<std::string,Node*> Map;
typedef typename Map::iterator MapIterator;
typedef typename Map::const_iterator MapCIterator;
Map m_kEntry;

// The default catalog for each class type (Image, Texture,
// VertexProgram, PixelProgram) is created in Main::Initialize and
// destroyed in Main::Terminate (see Wm4Main.cpp).
WM4_GRAPHICS_ITEM static Catalog* ms_pkActive;

// Additional catalogs may be created by the application. This array does
// not contain the default catalog created by class Main.
WM4_GRAPHICS_ITEM static std::vector<Catalog*> ms_apkCatalogs;
};

template<> static bool Catalog<Node>::RemoveAll(Node* ptObject);
etc.

Loading