Commits

Palmer, 2E0EOL  committed aaeb410

Set up new name() variants and auto_name() to generate associated tag from class name.

  • Participants
  • Parent commits 33841d3

Comments (0)

Files changed (3)

 	return false;
 }
 
+/*
+  Please note that this variant of name() is provided only due to
+  type_info.name() returning the const char * type.  I recommend the
+  std::string version.
+*/
+const std::string &DB::name(const char *NewName)
+{
+	if ( NewName )
+		this->tag.assign(NewName);
+
+	// nb. you can't assign no name
+	return this->name();
+}
+
+void DB::auto_name(const DB *DBClass)
+{
+	this->name(typeid(*this).name());
+}
+
+
+/*
+  The DB::name() function variant accepting a new name may be used by any derived class
+  in order to override the tag associated with a database connection, for logging
+  purposes.  Attempted to set a string of zero length will be ignored.  The new string
+  is returned, to provide consisent behaviour.
+*/
+const std::string &DB::name(const std::string &NewName)
+{
+	if ( NewName.length() )
+		this->tag.assign(NewName);
+
+	// nb. you can't assign no name.
+	return this->name();
+}
+
 const std::string &DB::name()
 {
 	// Return the name associated with the database.
 	bool dbh, dbh_ro; // Handles to master and slave database
 protected:
 	std::string tag; // Human-facing 'name' for the database (no real effect)
+	void auto_name(const DB *DBClass); // Automatically generate a name based on real class name
 public:
 	DB(void);
 	bool is_mock(void);
 	bool query(const std::string &Query);
+	const std::string &name(const char *NewName);
+	const std::string &name(const std::string &NewName);
 	const std::string &name(void);
 };
 

File DBMockExample.cpp

 
 DBMockExample::DBMockExample()
 {
+	this->auto_name(this);
 	this->CreateTables();
 	this->PopulateData();
 }