Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helpmanager.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2014,2015,2017,2019,2020, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::HelpManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_onlinehelp
41  */
42 #include "gmxpre.h"
43
44 #include "helpmanager.h"
45
46 #include <string>
47 #include <vector>
48
49 #include "gromacs/onlinehelp/helpwritercontext.h"
50 #include "gromacs/onlinehelp/ihelptopic.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/stringutil.h"
53
54 namespace gmx
55 {
56
57 /********************************************************************
58  * HelpManager::Impl
59  */
60
61 /*! \internal \brief
62  * Private implementation class for HelpManager.
63  *
64  * \ingroup module_onlinehelp
65  */
66 class HelpManager::Impl
67 {
68 public:
69     //! Container type for keeping the stack of active topics.
70     typedef std::vector<const IHelpTopic*> TopicStack;
71
72     //! Initializes a new manager with the given context.
73     explicit Impl(const HelpWriterContext& context) : rootContext_(context) {}
74
75     //! Whether the active topic is the root topic.
76     bool isAtRootTopic() const { return topicStack_.size() == 1; }
77     //! Returns the active topic.
78     const IHelpTopic& currentTopic() const { return *topicStack_.back(); }
79     //! Formats the active topic as a string, including its parent topics.
80     std::string currentTopicAsString() const;
81
82     //! Context with which the manager was initialized.
83     const HelpWriterContext& rootContext_;
84     /*! \brief
85      * Stack of active topics.
86      *
87      * The first item is always the root topic, and each item is a subtopic
88      * of the preceding item.  The last item is the currently active topic.
89      */
90     TopicStack topicStack_;
91 };
92
93 std::string HelpManager::Impl::currentTopicAsString() const
94 {
95     std::string                result;
96     TopicStack::const_iterator topic;
97     for (topic = topicStack_.begin() + 1; topic != topicStack_.end(); ++topic)
98     {
99         if (!result.empty())
100         {
101             result.append(" ");
102         }
103         result.append((*topic)->name());
104     }
105     return result;
106 }
107
108 /********************************************************************
109  * HelpManager
110  */
111
112 HelpManager::HelpManager(const IHelpTopic& rootTopic, const HelpWriterContext& context) :
113     impl_(new Impl(context))
114 {
115     impl_->topicStack_.push_back(&rootTopic);
116 }
117
118 HelpManager::~HelpManager() {}
119
120 void HelpManager::enterTopic(const char* name)
121 {
122     const IHelpTopic& topic = impl_->currentTopic();
123     if (!topic.hasSubTopics())
124     {
125         GMX_THROW(InvalidInputError(formatString("Help topic '%s' has no subtopics",
126                                                  impl_->currentTopicAsString().c_str())));
127     }
128     const IHelpTopic* newTopic = topic.findSubTopic(name);
129     if (newTopic == nullptr)
130     {
131         if (impl_->isAtRootTopic())
132         {
133             GMX_THROW(InvalidInputError(formatString("No help available for '%s'", name)));
134         }
135         else
136         {
137             GMX_THROW(InvalidInputError(formatString(
138                     "Help topic '%s' has no subtopic '%s'", impl_->currentTopicAsString().c_str(), name)));
139         }
140     }
141     impl_->topicStack_.push_back(newTopic);
142 }
143
144 void HelpManager::enterTopic(const std::string& name)
145 {
146     enterTopic(name.c_str());
147 }
148
149 void HelpManager::writeCurrentTopic() const
150 {
151     const IHelpTopic& topic = impl_->currentTopic();
152     const char*       title = topic.title();
153     HelpWriterContext context(impl_->rootContext_);
154     context.enterSubSection(title != nullptr ? title : "");
155     topic.writeHelp(context);
156 }
157
158 } // namespace gmx