Use local ProgramInfo in CommandLineModuleManager
authorTeemu Murtola <teemu.murtola@gmail.com>
Sat, 28 Dec 2013 06:12:47 +0000 (08:12 +0200)
committerTeemu Murtola <teemu.murtola@gmail.com>
Wed, 22 Jan 2014 05:05:09 +0000 (07:05 +0200)
Remove all usage of the global ProgramInfo object from
CommandLineModuleManager and the classes it uses.  Instead, use the
local instance.  This allows further refactoring.

Change-Id: I52d930ae4bd6bca8a9b4d12c2d0d66470e56a645

src/gromacs/commandline/cmdlinehelpmodule.cpp
src/gromacs/commandline/cmdlinehelpmodule.h
src/gromacs/commandline/cmdlinemodulemanager-impl.h
src/gromacs/commandline/cmdlinemodulemanager.cpp

index 93f1a29fd425ecb3caf046f9a36df0615987fcda..ba23dcc4a138e969128bc85ad82cd95299287816 100644 (file)
@@ -228,7 +228,7 @@ class ModuleHelpTopic : public HelpTopicInterface
     public:
         //! Constructs a help topic for a specific module.
         ModuleHelpTopic(const CommandLineModuleInterface &module,
-                        const CommandLineHelpModule      &helpModule)
+                        const CommandLineHelpModuleImpl  &helpModule)
             : module_(module), helpModule_(helpModule)
         {
         }
@@ -244,16 +244,16 @@ class ModuleHelpTopic : public HelpTopicInterface
 
     private:
         const CommandLineModuleInterface &module_;
-        const CommandLineHelpModule      &helpModule_;
+        const CommandLineHelpModuleImpl  &helpModule_;
 
         GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
 };
 
 void ModuleHelpTopic::writeHelp(const HelpWriterContext & /*context*/) const
 {
-    CommandLineHelpContext context(helpModule_.context());
+    CommandLineHelpContext context(*helpModule_.context_);
     const char *const      program =
-        helpModule_.programInfo().realBinaryName().c_str();
+        helpModule_.programInfo_.realBinaryName().c_str();
     context.setModuleDisplayName(formatString("%s %s", program, module_.name()));
     module_.writeHelp(context);
 }
@@ -328,20 +328,23 @@ class HelpExportInterface
 /*! \internal \brief
  * Adds hyperlinks to modules within this binary.
  *
- * \param[in,out] links   Links are added here.
- * \param[in]     modules Modules in the current binary.
+ * \param[in,out] links      Links are added here.
+ * \param[in]     helpModule Help module to get module information from.
  * \throws        std::bad_alloc if out of memory.
  *
- * Initializes a HelpLinks object with links to modules defined in \p modules.
+ * Initializes a HelpLinks object with links to modules defined in
+ * \p helpModule.
  *
  * \ingroup module_commandline
  */
-void initProgramLinks(HelpLinks *links, const CommandLineModuleMap &modules)
+void initProgramLinks(HelpLinks *links, const CommandLineHelpModuleImpl &helpModule)
 {
     const char *const                    program =
-        ProgramInfo::getInstance().realBinaryName().c_str();
+        helpModule.programInfo_.realBinaryName().c_str();
     CommandLineModuleMap::const_iterator module;
-    for (module = modules.begin(); module != modules.end(); ++module)
+    for (module = helpModule.modules_.begin();
+         module != helpModule.modules_.end();
+         ++module)
     {
         if (module->second->shortDescription() != NULL)
         {
@@ -368,10 +371,10 @@ class HelpExportMan : public HelpExportInterface
 {
     public:
         //! Initializes man page exporter.
-        explicit HelpExportMan(const CommandLineModuleMap &modules)
+        explicit HelpExportMan(const CommandLineHelpModuleImpl &helpModule)
             : links_(eHelpOutputFormat_Man)
         {
-            initProgramLinks(&links_, modules);
+            initProgramLinks(&links_, helpModule);
         }
 
         virtual void startModuleExport() {}
@@ -472,7 +475,7 @@ class HelpExportHtml : public HelpExportInterface
 {
     public:
         //! Initializes HTML exporter.
-        explicit HelpExportHtml(const CommandLineModuleMap &modules);
+        explicit HelpExportHtml(const CommandLineHelpModuleImpl &helpModule);
 
         virtual void startModuleExport();
         virtual void exportModuleHelp(
@@ -498,10 +501,10 @@ class HelpExportHtml : public HelpExportInterface
         std::string              footer_;
 };
 
-HelpExportHtml::HelpExportHtml(const CommandLineModuleMap &modules)
+HelpExportHtml::HelpExportHtml(const CommandLineHelpModuleImpl &helpModule)
     : links_(eHelpOutputFormat_Html)
 {
-    initProgramLinks(&links_, modules);
+    initProgramLinks(&links_, helpModule);
     File             linksFile("links.dat", "r");
     std::string      line;
     while (linksFile.readLine(&line))
@@ -658,7 +661,7 @@ CommandLineHelpModule::~CommandLineHelpModule()
 HelpTopicPointer CommandLineHelpModule::createModuleHelpTopic(
         const CommandLineModuleInterface &module) const
 {
-    return HelpTopicPointer(new ModuleHelpTopic(module, *this));
+    return HelpTopicPointer(new ModuleHelpTopic(module, *impl_));
 }
 
 void CommandLineHelpModule::addTopic(HelpTopicPointer topic)
@@ -677,16 +680,6 @@ void CommandLineHelpModule::setModuleOverride(
     impl_->moduleOverride_ = &module;
 }
 
-const CommandLineHelpContext &CommandLineHelpModule::context() const
-{
-    return *impl_->context_;
-}
-
-const ProgramInfo &CommandLineHelpModule::programInfo() const
-{
-    return impl_->programInfo_;
-}
-
 int CommandLineHelpModule::run(int argc, char *argv[])
 {
     const char *const exportFormats[] = { "man", "html", "completion" };
@@ -700,11 +693,11 @@ int CommandLineHelpModule::run(int argc, char *argv[])
         boost::scoped_ptr<HelpExportInterface> exporter;
         if (exportFormat == "man")
         {
-            exporter.reset(new HelpExportMan(impl_->modules_));
+            exporter.reset(new HelpExportMan(*impl_));
         }
         else if (exportFormat == "html")
         {
-            exporter.reset(new HelpExportHtml(impl_->modules_));
+            exporter.reset(new HelpExportHtml(*impl_));
         }
         else
         {
@@ -715,7 +708,7 @@ int CommandLineHelpModule::run(int argc, char *argv[])
     }
 
     HelpLinks                                 links(eHelpOutputFormat_Console);
-    initProgramLinks(&links, impl_->modules_);
+    initProgramLinks(&links, *impl_);
     boost::scoped_ptr<CommandLineHelpContext> context(
             new CommandLineHelpContext(&File::standardOutput(),
                                        eHelpOutputFormat_Console, &links));
index 27d5d2a7c50d3c36ae52033535a197544e3db1cf..ce309164385b5d7af4da13e4be24109c15ad4029 100644 (file)
@@ -110,11 +110,6 @@ class CommandLineHelpModule : public CommandLineModuleInterface
          */
         void setModuleOverride(const CommandLineModuleInterface &module);
 
-        //! Returns the context object for help output.
-        const CommandLineHelpContext &context() const;
-        //! Returns the program info object for the running binary.
-        const ProgramInfo            &programInfo() const;
-
         virtual const char *name() const { return "help"; }
         virtual const char *shortDescription() const
         {
index 7612920cd0044b50a70b26ade957ca1a5926249f..1d9f1c08f8cbacbd2693addae8ea4fd1f9090f12 100644 (file)
@@ -83,15 +83,17 @@ class CommandLineModuleGroupData
         /*! \brief
          * Constructs an empty module group.
          *
-         * \param[in] modules  List of all modules
+         * \param[in] modules     List of all modules
          *     (used for checking and default descriptions).
-         * \param[in] title    Title of the group.
+         * \param[in] binaryName  Name of the binary containing the modules.
+         * \param[in] title       Title of the group.
          *
          * Does not throw.
          */
         CommandLineModuleGroupData(const CommandLineModuleMap &modules,
+                                   const char                 *binaryName,
                                    const char                 *title)
-            : allModules_(modules), title_(title)
+            : allModules_(modules), binaryName_(binaryName), title_(title)
         {
         }
 
@@ -114,6 +116,7 @@ class CommandLineModuleGroupData
 
     private:
         const CommandLineModuleMap &allModules_;
+        const char                 *binaryName_;
         const char                 *title_;
         ModuleList                  modules_;
 
index 10963999cf6b69f7865b4e7f9b0d3b05712eedbc..3536199592fb7de04866dd7a3a1737ee0c32d8e8 100644 (file)
@@ -448,8 +448,10 @@ void CommandLineModuleManager::addModuleCMain(
 CommandLineModuleGroup CommandLineModuleManager::addModuleGroup(
         const char *title)
 {
+    const char *const                 binaryName =
+        impl_->programInfo_.invariantProgramName().c_str();
     CommandLineModuleGroupDataPointer group(
-            new CommandLineModuleGroupData(impl_->modules_, title));
+            new CommandLineModuleGroupData(impl_->modules_, binaryName, title));
     impl_->moduleGroups_.push_back(move(group));
     return CommandLineModuleGroup(impl_->moduleGroups_.back().get());
 }
@@ -544,9 +546,7 @@ void CommandLineModuleGroupData::addModule(const char *name,
         GMX_RELEASE_ASSERT(description != NULL,
                            "Module without a description added to a group");
     }
-    const char *const program =
-        ProgramInfo::getInstance().invariantProgramName().c_str();
-    std::string       tag(formatString("%s-%s", program, name));
+    std::string       tag(formatString("%s-%s", binaryName_, name));
     modules_.push_back(std::make_pair(tag, description));
 }