Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Fix Instance() method of Singleton classes #3269

Merged
merged 2 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions gazebo/common/FuelModelDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,16 @@ std::string FuelModelDatabase::CachedFilePath(const std::string &_uri)

return path;
}

//////////////////////////////////////////////////
FuelModelDatabase* FuelModelDatabase::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<FuelModelDatabase>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/common/FuelModelDatabase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ namespace gazebo
/// \return Local path to the file
public: std::string CachedFilePath(const std::string &_uri);

/// \brief Returns a pointer to the unique (static) instance
public: static FuelModelDatabase* Instance();

/// \brief Private data.
private: std::unique_ptr<FuelModelDatabasePrivate> dataPtr;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/common/MeshManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1394,5 +1394,18 @@ void MeshManager::ConvertPolylinesToVerticesAndEdges(
}
}
}

//////////////////////////////////////////////////
MeshManager* MeshManager::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<MeshManager>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
}
}
3 changes: 3 additions & 0 deletions gazebo/common/MeshManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ namespace gazebo
const ignition::math::Vector2d &_p,
double _tol);

/// \brief Returns a pointer to the unique (static) instance
public: static MeshManager* Instance();

/// \brief Singleton implementation
private: friend class SingletonT<MeshManager>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/common/ModelDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,16 @@ std::string ModelDatabase::GetModelFile(const std::string &_uri)

return result;
}

//////////////////////////////////////////////////
ModelDatabase* ModelDatabase::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<ModelDatabase>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/common/ModelDatabase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ namespace gazebo
/// no one else should use this function.
private: bool UpdateModelCacheImpl();

/// \brief Returns a pointer to the unique (static) instance
public: static ModelDatabase* Instance();

/// \brief Private data.
private: ModelDatabasePrivate *dataPtr;

Expand Down
19 changes: 18 additions & 1 deletion gazebo/common/SingletonT.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@

/// \class SingletonT SingletonT.hh common/common.hh
/// \brief Singleton template class
///
/// This class can be used to simplify the implementation of singletons,
/// i.e. classes that only have one instance. An important constraint to
/// respect is that for each type `T`, the method SingletonT<T>::Instance
/// should be instantiated only once. For this reason this method should
/// be called only once for class, and only in a non-inline method not
/// defined in an header file. To ensure that the SingletonT<T>::Instance
/// method is not accidentally called in a inline method, or multiple times,
/// the method is marked as deprecated. To call it (after making sure that
/// you are calling it respecting the constraint given in documentation),
/// make sure to temporary suppress deprecation warnings before calling it,
/// and suppress them again after the Instance method has been called.
///
/// See https://github.com/gazebosim/gazebo-classic/pull/3269 for further
/// details on the use of this class.


template <class T>
class SingletonT
{
/// \brief Get an instance of the singleton
public: static T *Instance()
public: static T *Instance() GAZEBO_DEPRECATED(11.0)
{
return &GetInstance();
}
Expand Down
13 changes: 13 additions & 0 deletions gazebo/common/SystemPaths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,16 @@ void SystemPaths::AddSearchPathSuffix(const std::string &_suffix)

this->suffixPaths.push_back(s);
}

//////////////////////////////////////////////////
SystemPaths* SystemPaths::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<SystemPaths>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/common/SystemPaths.hh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ namespace gazebo
private: void InsertUnique(const std::string &_path,
std::list<std::string> &_list);

/// \brief Returns a pointer to the unique (static) instance
public: static SystemPaths* Instance();

/// \brief Paths to installed gazebo media files
private: std::list<std::string> gazeboPaths;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/KeyEventHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,16 @@ bool KeyEventHandler::Handle(const common::KeyEvent &_event,
}
return false;
}

//////////////////////////////////////////////////
KeyEventHandler* KeyEventHandler::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<KeyEventHandler>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/gui/KeyEventHandler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ namespace gazebo
private: bool Handle(const common::KeyEvent &_event,
std::list<Filter> &_list);

/// \brief Returns a pointer to the unique (static) instance
public: static KeyEventHandler* Instance();

/// \brief This is a singleton class.
private: friend class SingletonT<KeyEventHandler>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/ModelAlign.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,16 @@ void ModelAlign::SetHighlighted(const rendering::VisualPtr &_vis,
}
}
}

//////////////////////////////////////////////////
ModelAlign* ModelAlign::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<ModelAlign>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/gui/ModelAlign.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ namespace gazebo
private: void SetHighlighted(const rendering::VisualPtr &_vis,
const bool _highlight);

/// \brief Returns a pointer to the unique (static) instance
public: static ModelAlign* Instance();

/// \brief This is a singleton class.
private: friend class SingletonT<ModelAlign>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/ModelManipulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,19 @@ void ModelManipulator::OnKeyReleaseEvent(const common::KeyEvent &_event)
this->dataPtr->keyEvent.key = 0;
}

//////////////////////////////////////////////////
ModelManipulator* ModelManipulator::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<ModelManipulator>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}

// Function migrated here from GLWidget.cc and commented out since it doesn't
// seem like it's currently used. Kept here for future references
/////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions gazebo/gui/ModelManipulator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ namespace gazebo
const ignition::math::Vector3d &_axis,
const ignition::math::Vector3d &_scale, const std::string &_geom);

/// \brief Returns a pointer to the unique (static) instance
public: static ModelManipulator* Instance();

/// \brief This is a singleton class.
private: friend class SingletonT<ModelManipulator>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/ModelSnap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,16 @@ void ModelSnap::Update()
this->dataPtr->selectedTriangleDirty = false;
}
}

//////////////////////////////////////////////////
ModelSnap* ModelSnap::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<ModelSnap>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/gui/ModelSnap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ namespace gazebo
/// \brief Update the visual representation of the snap spot.
private: void Update();

/// \brief Returns a pointer to the unique (static) instance
public: static ModelSnap* Instance();

/// \brief This is a singleton class.
private: friend class SingletonT<ModelSnap>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/MouseEventHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ void MouseEventHandler::Handle(const common::MouseEvent &_event,
break;
}
}

//////////////////////////////////////////////////
MouseEventHandler* MouseEventHandler::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<MouseEventHandler>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/gui/MouseEventHandler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ namespace gazebo
private: void Handle(const common::MouseEvent &_event,
std::list<Filter> &_list);

/// \brief Returns a pointer to the unique (static) instance
public: static MouseEventHandler* Instance();

/// \brief List of mouse press filters.
private: std::list<Filter> pressFilters;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/gui/plot/PlotManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ std::string PlotManager::HumanReadableName(const std::string &_uri) const

return label;
}

//////////////////////////////////////////////////
PlotManager* PlotManager::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<PlotManager>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/gui/plot/PlotManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ namespace gazebo
/// \return Human readable name
public: std::string HumanReadableName(const std::string &_uri) const;

/// \brief Returns a pointer to the unique (static) instance
public: static PlotManager* Instance();

/// \brief This is a singleton class.
private: friend class SingletonT<PlotManager>;

Expand Down
16 changes: 16 additions & 0 deletions gazebo/gui/plot/TopicCurveHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace gazebo
/// \param[in] _msg WorldStats msg.
public: void OnStats(ConstWorldStatisticsPtr &_msg);

/// \brief Returns a pointer to the unique (static) instance
public: static TopicTime* Instance();

/// \brief Node for communications.
private: transport::NodePtr node;

Expand Down Expand Up @@ -168,6 +171,19 @@ void TopicTime::OnStats(ConstWorldStatisticsPtr &_msg)
this->lastSimTime = msgs::Convert(t);
}

//////////////////////////////////////////////////
TopicTime* TopicTime::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<TopicTime>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}

/////////////////////////////////////////////////
common::Time TopicTime::LastSimTime()
{
Expand Down
13 changes: 13 additions & 0 deletions gazebo/rendering/RTShaderSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,16 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene)
sceneMgr->setShadowTextureCasterMaterial(_scene->ShadowCasterMaterialName());
#endif
}

//////////////////////////////////////////////////
RTShaderSystem* RTShaderSystem::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<RTShaderSystem>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/rendering/RTShaderSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ namespace gazebo
/// \brief Re-apply shadows. Call this if a shadow paramenter is changed.
private: void ReapplyShadows();

/// \brief Returns a pointer to the unique (static) instance
public: static RTShaderSystem* Instance();

/// \brief Make the RTShader system a singleton.
private: friend class SingletonT<RTShaderSystem>;

Expand Down
13 changes: 13 additions & 0 deletions gazebo/rendering/RenderEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,16 @@ Ogre::OverlaySystem *RenderEngine::OverlaySystem() const
return this->dataPtr->overlaySystem;
}
#endif

//////////////////////////////////////////////////
RenderEngine* RenderEngine::Instance()
{
#ifndef _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
return SingletonT<RenderEngine>::Instance();
#ifndef _WIN32
#pragma GCC diagnostic pop
#endif
}
3 changes: 3 additions & 0 deletions gazebo/rendering/RenderEngine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ namespace gazebo
/// \brief Check the rendering capabilities of the system.
private: void CheckSystemCapabilities();

/// \brief Returns a pointer to the unique (static) instance
public: static RenderEngine* Instance();

/// \brief ID for a dummy window. Used for gui-less operation
protected: uint64_t dummyWindowId;

Expand Down
Loading