Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++]risorse e template

    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.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Probabilmente si, però dovresti postare l'interfaccia del template generico.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    penso sia questa:
    codice:
    // http://www.geometrictools.com/Licens...ICENSE_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

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    La specializzazione diventa (con annessa funzione in fondo):
    codice:
    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.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.