A simple class like this would do the job.
NameGenerator.h
Code: Select all
#include <string>
/**
* \brief A utility class to generate unique names.
**/
class NameGenerator
{
public:
/**
* \brief Gets the next unique name.
**/
static std::string Next();
/**
* \brief Gets the next unique name for a given prefix.
**/
static String Next(const std::string& prefix);
/**
* \brief Counts the number of unique auto-generated names.
**/
static size_t Count();
/**
* \brief Counts the number of names generated for a given prefix.
* \param prefix The prefix of the generated names.
**/
static size_t Count(const std::string& prefix);
private:
NameGenerator(void);
~NameGenerator(void);
typedef std::map<std::string, uint32> NameCountMap;
static NameCountMap s_nameCount;
};
NameGenerator.cpp
Code: Select all
#include <sstream>
const std::string DEFAULT_NAME_PREFIX = "AutoGeneratedName";
NameGenerator::NameCountMap NameGenerator::s_nameCount;
//---------------------------------------------------------------------
NameGenerator::NameGenerator(void)
{
}
//---------------------------------------------------------------------
NameGenerator::~NameGenerator(void)
{
}
//---------------------------------------------------------------------
String NameGenerator::Next()
{
return Next(DEFAULT_NAME_PREFIX);
}
//---------------------------------------------------------------------
String NameGenerator::Next( const std::string& prefix )
{
if (s_nameCount.count(prefix) == 0)
{
s_nameCount[prefix] = 0;
}
std::stringstream ss;
ss<<prefix<<"_"<<s_nameCount[prefix]++;
return ss.str();
}
//---------------------------------------------------------------------
size_t NameGenerator::Count()
{
size_t count = Count(DEFAULT_NAME_PREFIX);
return count;
}
//---------------------------------------------------------------------
size_t NameGenerator::Count( const std::string& prefix )
{
size_t count = (s_nameCount.count(prefix) == 0)?0:s_nameCount[prefix];
return count;
}
Usage:
Code: Select all
std::string autogenerated_name = NameGenerator::Next(); // Generates (from the above code) 'AutoGeneratedName_x', where x is a number
std::string new_entity_name = NameGenerator::Next("Entity"); // Generates 'Entity_x', where x is a number
Hope this solves your problem regarding generating unique names.
About the which ones should have unique names, all objects of a particular group should have unique names. Like every entity should have a unique name, which doesn't clash with another entity and the same goes for lights, scene nodes and so on. But between the groups they can have the same name, like an Entity can have the name "MyActor", while *a* scene node also has the same name.