diff --git a/include/gz/common/SingletonT.hh b/include/gz/common/SingletonT.hh index ecde948d..0724c582 100644 --- a/include/gz/common/SingletonT.hh +++ b/include/gz/common/SingletonT.hh @@ -41,6 +41,7 @@ namespace gz /// \brief Creates and returns a reference to the unique (static) instance private: static T &GetInstance() { + // Caution: object `t` will be duplicated in each translation unit static T t; return static_cast(t); } diff --git a/profiler/include/gz/common/Profiler.hh b/profiler/include/gz/common/Profiler.hh index 69b93d61..11c330b6 100644 --- a/profiler/include/gz/common/Profiler.hh +++ b/profiler/include/gz/common/Profiler.hh @@ -100,6 +100,9 @@ namespace gz /// \brief Detect if profiler is enabled and has an implementation public: bool Valid() const; + /// \brief Get an instance of the singleton + public: static Profiler *Instance(); + /// \brief Pointer to the profiler implementation private: ProfilerImpl *impl; diff --git a/profiler/src/Profiler.cc b/profiler/src/Profiler.cc index 1eeb11ea..67d41187 100644 --- a/profiler/src/Profiler.cc +++ b/profiler/src/Profiler.cc @@ -115,3 +115,10 @@ bool Profiler::SetImplementation(std::unique_ptr _impl) gzdbg << "Gazebo profiling with: " << this->impl->Name() << std::endl; return true; } + +////////////////////////////////////////////////// +Profiler *Profiler::Instance() +{ + static Profiler profiler; + return &profiler; +}