3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
31 /*! \libinternal \file
33 * Declares helper classes and functions for implementing
34 * gmx::HelpTopicInterface.
36 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
38 * \ingroup module_onlinehelp
40 #ifndef GMX_ONLINEHELP_HELPTOPIC_H
41 #define GMX_ONLINEHELP_HELPTOPIC_H
43 #include "../utility/stringutil.h"
44 #include "../utility/uniqueptr.h"
46 #include "helptopicinterface.h"
52 /*! \libinternal \brief
53 * Helper for writing simple help text.
55 * \param[in] context Context for writing the help.
56 * \param[in] topic Topic to write the help for (used for title).
57 * \param[in] text Text to write for the topic.
58 * \throws std::bad_alloc if out of memory.
59 * \throws FileIOError on any I/O error.
61 * Formats basic help by writing a title (obtained from \p topic), followed by
62 * \p text with markup substituted and lines properly wrapped.
66 void writeBasicHelpTopic(const HelpWriterContext &context,
67 const HelpTopicInterface &topic,
68 const std::string &text);
71 /*! \libinternal \brief
72 * Abstract base class for help topics that have simple text and no subtopics.
74 * This class implements subtopic-related methods from HelpTopicInterface such
75 * that there are no subtopics. writeHelp() is also implemented such that it
76 * uses writeBasicHelpTopic() to write out the text returned by a new virtual
79 * \see SimpleHelpTopic
82 * \ingroup module_onlinehelp
84 class AbstractSimpleHelpTopic : public HelpTopicInterface
87 virtual const char *name() const = 0;
88 virtual const char *title() const = 0;
90 virtual bool hasSubTopics() const;
91 virtual const HelpTopicInterface *findSubTopic(const char *name) const;
93 virtual void writeHelp(const HelpWriterContext &context) const;
97 * Returns the help text for this topic.
99 * writeHelp() calls this method to obtain the actual text to format
100 * for the topic. Markup substitution etc. is done automatically by
103 virtual std::string helpText() const = 0;
106 /*! \libinternal \brief
107 * Abstract base class for help topics that have simple text and subtopics.
109 * This class implements an internal container for subtopics and provides
110 * public methods for adding subtopics (as HelpTopicInterface objects).
111 * Subtopic-related methods from HelpTopicInterface are implemented to access
112 * the internal container. writeHelp() is also implemented such that it
113 * uses writeBasicHelpTopic() to write out the text returned by a new virtual
114 * method helpText(), and a list of subtopics is written after the actual text.
116 * \see CompositeHelpTopic
119 * \ingroup module_onlinehelp
121 class AbstractCompositeHelpTopic : public HelpTopicInterface
124 AbstractCompositeHelpTopic();
125 virtual ~AbstractCompositeHelpTopic();
127 virtual const char *name() const = 0;
128 virtual const char *title() const = 0;
130 virtual bool hasSubTopics() const;
131 virtual const HelpTopicInterface *findSubTopic(const char *name) const;
133 virtual void writeHelp(const HelpWriterContext &context) const;
136 * Adds a given topic as a subtopic of this topic.
138 * \param topic Topis to add.
139 * \throws std::bad_alloc if out of memory.
141 * This topic takes ownership of the object.
143 * \see registerSubTopic()
145 void addSubTopic(HelpTopicPointer topic);
147 * Registers a subtopic of a certain type to this topic.
149 * \tparam Topic Type of topic to register.
150 * \throws std::bad_alloc if out of memory.
152 * \p Topic must be default-constructible and implement
153 * HelpTopicInterface.
155 * This method is provided as a convenient alternative to addSubTopic()
156 * for cases where each topic is implemented by a different type
157 * (which is a common case outside unit tests).
159 template <class Topic>
160 void registerSubTopic()
162 addSubTopic(HelpTopicPointer(new Topic));
166 //! \copydoc AbstractSimpleHelpTopic::helpText()
167 virtual std::string helpText() const = 0;
170 * Writes the list of subtopics.
172 * \param[in] context Context for writing the help.
173 * \param[in] title Title for the written list.
174 * \returns true if anything was printed.
175 * \throws std::bad_alloc if out of memory.
176 * \throws FileIOError on any I/O error.
178 * Subtopics with empty titles are skipped from the list.
179 * If there would be no subtopics in the list, \p title is not printed
182 * This method is provided for cases where helpText() does not provide
183 * the needed flexibility and the derived class needs to override
184 * writeHelp(). This method can then be called to print the same
185 * subtopic list that is printed by the default writeHelp()
188 bool writeSubTopicList(const HelpWriterContext &context,
189 const std::string &title) const;
194 PrivateImplPointer<Impl> impl_;
198 /*! \libinternal \brief
199 * Smart pointer type to manage a AbstractCompositeHelpTopic object.
203 typedef gmx_unique_ptr<AbstractCompositeHelpTopic>::type
204 CompositeHelpTopicPointer;
207 /*! \libinternal \brief
208 * Template for simple implementation of AbstractSimpleHelpTopic.
210 * \tparam HelpText Struct that defines the data for the topic.
212 * \p HelpText should have public static members \c "const char name[]",
213 * \c "const char title[]" and \c "const char *const text[]".
217 struct ExampleHelpText
219 static const char name[];
220 static const char title[];
221 static const char *const text[];
224 const char ExampleHelpText::name[] = "example";
225 const char ExampleHelpText::title[] =
227 const char *const ExampleHelpText::text[] = {
228 "Text for the topic.",
229 "More text for the topic."
232 typedef SimpleHelpTopic<ExampleHelpText> ExampleHelpTopic;
236 * \ingroup module_onlinehelp
238 template <class HelpText>
239 class SimpleHelpTopic : public AbstractSimpleHelpTopic
242 virtual const char *name() const
244 return HelpText::name;
246 virtual const char *title() const
248 return HelpText::title;
252 virtual std::string helpText() const
254 return concatenateStrings(HelpText::text);
258 /*! \libinternal \brief
259 * Template for simple implementation of AbstractCompositeHelpTopic.
261 * \tparam HelpText Struct that defines the data for the topic.
263 * Used similarly to SimpleHelpTopic.
264 * \p HelpText should satisfy the same criteria as for SimpleHelpTopic.
266 * \see SimpleHelpTopic
269 * \ingroup module_onlinehelp
271 template <class HelpText>
272 class CompositeHelpTopic : public AbstractCompositeHelpTopic
275 virtual const char *name() const
277 return HelpText::name;
279 virtual const char *title() const
281 return HelpText::title;
285 virtual std::string helpText() const
287 return concatenateStrings(HelpText::text);