Thread: Hashmap of objects or references?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I don't intend to put words in his mouth, but presumably, he means using something like the following example.

    The fancy 'Model' constructor call followed by a chained call to the 'swap' method constructs a 'Model' object in all its glory, swaps the data pointer owned by the just constructed 'Model' instance with the data pointer of the 'Model' instance owned by the 'std::map', and finally calls the destructor for the local object.

    The default constructor sets the data pointer to null. The destructor simply calls 'delete' on the pointer. The 'swap' method swaps the data pointers.

    An exception safe 'swap' method is just an elegant means of having two instances exchange ownership of the objects they represent.

    In this case, the availability of the option means that 'std::map' can cheaply construct the 'Model' instances it needs and you can construct the instances you want without needing to worry about the fiddly bits--like possibly expensive temporaries and complex assignment operators.

    Soma

    Code:
    struct modelFace {
        int v[3];
        int vt[3];
        int vn[3];
        int matIndex;
        
    };
    
    struct modelVertex {
        GLfloat x;
        GLfloat y;
        GLfloat z;
    };
    
    struct modelMaterial {
        string name;
        
        GLfloat specExponent;
        GLfloat optDensity;
        GLfloat ambRefl[4];
        GLfloat difRefl[4];
        GLfloat specRefl[4];
        GLfloat transFilter[4];
        GLuint difMap;
        GLuint specMap;
        GLuint bumpMap;
        bool bump;
    };
    
    struct ModelMembers
    {
        GLuint displayList;
        
        GLint shaderProgram;
        
        modelFace *faces;
        modelVertex *vertices;
        modelVertex *texels;
        modelVertex *normals;
        modelMaterial *materials;
        
        int numFaces;
        int totalVertices;
        int totalTexels;
        int totalNormals;
        int numMaterials;
        int numTextures;
    };
    
    class Model{
    public:
        Model();
        Model(const string &filename);
        Model(const string &filename, int id);
        ~Model();
        void draw();
        bool load(const string &filename);
        static bool load_tex(const string &filename, GLuint *texture, bool buildMipmaps);
        
        GLuint getDisplayList();
        GLint getShaderProgram();
        
        void setShaderProgram(GLint sp);
        
    private:
        bool load_mtl(const string &filename);
        
        void drawFace(const modelFace &face);
        bool isFacePoint(const string &s);
        void getFacePoint(const string &s, int &vertexIndex, int &textureIndex, int &normalIndex);
        
        int getMaterial(const string &name);
        
        void useMaterial(int i);
    
    public:
        
        void swap
        (
            Model & other_f
        )
        {
            using std::swap;
            swap(data_m, other_f.data_m);
        }
    
    private:    
        //Members
        ModelMembers * data_m;
        
    };
    
    void swap
    (
        Model & m1_f,
        Model & m2_f
    )
    {
        m1_f.swap(m2_f);
    }
    
    int main()
    {
        std::map<int, Model> my_map;
        // ...
        Model("FileName", ID /*, Whatever, Other, Parameters */).swap(my_map[GIANT_BUG]);
        // ...
    }
    Last edited by phantomotap; 03-06-2009 at 05:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM