Skip to content

Commit

Permalink
Provide trivial copy ctor and assignment operator for singleton classes
Browse files Browse the repository at this point in the history
One could still create copies with the following code:

// Create singleton
MockImageManager::RegisterManager();
ImageManager * im = ImageManager::getImageManager(MockImageManager::id);

// Violate singleton's contract with copy constructor
MockImageManager * temp = (MockImageManager*)im; // for conciseness
MockImageManager mim1(*temp);                         // copy-ctor #1
MockImageManager mim2 = *temp;                        // copy-ctor #2
MockImageManager mim3 = MockImageManager(*temp);      // copy-ctor #3
MockImageManager *pmim = new MockImageManager(*temp); // copy-ctor #4

// Copy assignment operator
mim1 = mim3;

See also http://stackoverflow.com/a/6811055
  • Loading branch information
PeterBowman committed Apr 16, 2017
1 parent e96d91f commit 2272d97
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/libraries/ImageLib/MockImageManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ class MockImageManager : public ImageManager
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getMentalMap() method.
* and the instantiation is done at the RegisterManager() method.
*/
MockImageManager();

MockImageManager(const MockImageManager &);
MockImageManager & operator=(const MockImageManager &);

//! @brief Reference to this manager (unique instance)
static MockImageManager * uniqueInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/ImageLib/YarpImageManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ class YarpImageManager : public ImageManager,
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getMentalMap() method.
* and the instantiation is done at the RegisterManager() method.
*/
YarpImageManager();

YarpImageManager(const YarpImageManager &);
YarpImageManager & operator=(const YarpImageManager &);

//! @brief Reference to this manager (unique instance)
static YarpImageManager * uniqueInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/ImageLib/YarpLocalImageManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ class YarpLocalImageManager : public ImageManager,
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getMentalMap() method.
* and the instantiation is done at the RegisterManager() method.
*/
YarpLocalImageManager();

YarpLocalImageManager(const YarpLocalImageManager &);
YarpLocalImageManager & operator=(const YarpLocalImageManager &);

//! @brief Reference to this manager (unique instance)
static YarpLocalImageManager * uniqueInstance;

Expand Down
12 changes: 11 additions & 1 deletion src/libraries/InputLib/MockInputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MockInputManager : public InputManager
bool sendWindowEvent(const WindowEvent & event);

//------------------------------ Construction & destruction ---------------------------------------------------//
MockInputManager();
virtual ~MockInputManager();

/**
Expand All @@ -49,6 +48,17 @@ class MockInputManager : public InputManager
int getNumListeners();

private:
/**
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the RegisterManager() method.
*/
MockInputManager();

MockInputManager(const MockInputManager &);
MockInputManager & operator=(const MockInputManager &);

//! @brief Reference to this manager (unique instance)
static MockInputManager * uniqueInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/InputLib/SDLInputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ class SDLInputManager : public InputManager
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getInputManager() method.
* and the instantiation is done at the RegisterManager() method.
*/
SDLInputManager();

SDLInputManager(const SDLInputManager &);
SDLInputManager & operator=(const SDLInputManager &);

// This static function is the real callback function. It's compatible
// with the C-style CallbackFunctionPtr. The extra void* is used to
// get back into the real object of this class type.
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/MentalMapLib/MentalMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class MentalMap : public NetworkEventListener
*/
MentalMap();

MentalMap(const MentalMap &);
MentalMap & operator=(const MentalMap &);

//! @brief Stores the unique instance of the MentalMap
static MentalMap * mentalMapInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/MusicLib/MockAudioManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ class MockAudioManager : public AudioManager,
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getAudioManager() method.
* and the instantiation is done at the RegisterManager() method.
*/
MockAudioManager();

MockAudioManager(const MockAudioManager &);
MockAudioManager & operator=(const MockAudioManager &);

//! @brief Method called periodically from the RateThread class. It simply calls the update() method.
void run();

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/MusicLib/SDLAudioManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ class SDLAudioManager : public AudioManager
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getAudioManager() method
* and the instantiation is done at the RegisterManager() method
*/
SDLAudioManager();

SDLAudioManager(const SDLAudioManager &);
SDLAudioManager & operator=(const SDLAudioManager &);

//! \brief Stores the unique instance of the AudioManager
static SDLAudioManager * uniqueInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/NetworkLib/MockNetworkManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ class MockNetworkManager : public NetworkManager
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getNetworkManager() method.
* and the instantiation is done at the RegisterManager() method.
*/
MockNetworkManager();

MockNetworkManager(const MockNetworkManager &);
MockNetworkManager & operator=(const MockNetworkManager &);

//! @brief Reference to this manager (unique instance)
static MockNetworkManager * uniqueInstance;

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/NetworkLib/YarpNetworkManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class YarpNetworkManager : public NetworkManager,
void run();

private:
YarpNetworkManager(const YarpNetworkManager &);
YarpNetworkManager & operator=(const YarpNetworkManager &);

//! @brief Reference to this manager (unique instance)
static YarpNetworkManager * uniqueInstance;

Expand Down
5 changes: 4 additions & 1 deletion src/libraries/UserInterfaceLib/SDLScreenManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ class SDLScreenManager : public ScreenManager
* @brief Constructor
*
* Constructor for this class is private, since the singleton can only be instantiated once,
* and the instantiation is done at the getScreenManager() method
* and the instantiation is done at the RegisterManager() method
*/
SDLScreenManager();

SDLScreenManager(const SDLScreenManager &);
SDLScreenManager & operator=(const SDLScreenManager &);

//! \brief Stores the unique instance of the SDLScreenManager
static SDLScreenManager * uniqueInstance;

Expand Down

0 comments on commit 2272d97

Please sign in to comment.