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]);
// ...
}