2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014,2015,2017, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::HelpManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_onlinehelp
44 #include "helpmanager.h"
49 #include "gromacs/onlinehelp/helpwritercontext.h"
50 #include "gromacs/onlinehelp/ihelptopic.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/stringutil.h"
57 /********************************************************************
62 * Private implementation class for HelpManager.
64 * \ingroup module_onlinehelp
66 class HelpManager::Impl
69 //! Container type for keeping the stack of active topics.
70 typedef std::vector
<const IHelpTopic
*> TopicStack
;
72 //! Initializes a new manager with the given context.
73 explicit Impl(const HelpWriterContext
&context
)
74 : rootContext_(context
)
78 //! Whether the active topic is the root topic.
79 bool isAtRootTopic() const { return topicStack_
.size() == 1; }
80 //! Returns the active topic.
81 const IHelpTopic
¤tTopic() const
83 return *topicStack_
.back();
85 //! Formats the active topic as a string, including its parent topics.
86 std::string
currentTopicAsString() const;
88 //! Context with which the manager was initialized.
89 const HelpWriterContext
&rootContext_
;
91 * Stack of active topics.
93 * The first item is always the root topic, and each item is a subtopic
94 * of the preceding item. The last item is the currently active topic.
96 TopicStack topicStack_
;
99 std::string
HelpManager::Impl::currentTopicAsString() const
102 TopicStack::const_iterator topic
;
103 for (topic
= topicStack_
.begin() + 1; topic
!= topicStack_
.end(); ++topic
)
109 result
.append((*topic
)->name());
114 /********************************************************************
118 HelpManager::HelpManager(const IHelpTopic
&rootTopic
,
119 const HelpWriterContext
&context
)
120 : impl_(new Impl(context
))
122 impl_
->topicStack_
.push_back(&rootTopic
);
125 HelpManager::~HelpManager()
129 void HelpManager::enterTopic(const char *name
)
131 const IHelpTopic
&topic
= impl_
->currentTopic();
132 if (!topic
.hasSubTopics())
134 GMX_THROW(InvalidInputError(
135 formatString("Help topic '%s' has no subtopics",
136 impl_
->currentTopicAsString().c_str())));
138 const IHelpTopic
*newTopic
= topic
.findSubTopic(name
);
139 if (newTopic
== nullptr)
141 if (impl_
->isAtRootTopic())
143 GMX_THROW(InvalidInputError(
144 formatString("No help available for '%s'", name
)));
148 GMX_THROW(InvalidInputError(
149 formatString("Help topic '%s' has no subtopic '%s'",
150 impl_
->currentTopicAsString().c_str(), name
)));
153 impl_
->topicStack_
.push_back(newTopic
);
156 void HelpManager::enterTopic(const std::string
&name
)
158 enterTopic(name
.c_str());
161 void HelpManager::writeCurrentTopic() const
163 const IHelpTopic
&topic
= impl_
->currentTopic();
164 const char *title
= topic
.title();
165 HelpWriterContext
context(impl_
->rootContext_
);
166 context
.enterSubSection(title
!= nullptr ? title
: "");
167 topic
.writeHelp(context
);