Introduce gmxpre.h for truly global definitions
[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, 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/helptopicinterface.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/stringutil.h"
52
53 namespace gmx
54 {
55
56 /********************************************************************
57  * HelpManager::Impl
58  */
59
60 /*! \internal \brief
61  * Private implementation class for HelpManager.
62  *
63  * \ingroup module_onlinehelp
64  */
65 class HelpManager::Impl
66 {
67     public:
68         //! Container type for keeping the stack of active topics.
69         typedef std::vector<const HelpTopicInterface *> TopicStack;
70
71         //! Initializes a new manager with the given context.
72         explicit Impl(const HelpWriterContext &context)
73             : rootContext_(context)
74         {
75         }
76
77         //! Whether the active topic is the root topic.
78         bool isAtRootTopic() const { return topicStack_.size() == 1; }
79         //! Returns the active topic.
80         const HelpTopicInterface &currentTopic() const
81         {
82             return *topicStack_.back();
83         }
84         //! Formats the active topic as a string, including its parent topics.
85         std::string currentTopicAsString() const;
86
87         //! Context with which the manager was initialized.
88         const HelpWriterContext &rootContext_;
89         /*! \brief
90          * Stack of active topics.
91          *
92          * The first item is always the root topic, and each item is a subtopic
93          * of the preceding item.  The last item is the currently active topic.
94          */
95         TopicStack               topicStack_;
96 };
97
98 std::string HelpManager::Impl::currentTopicAsString() const
99 {
100     std::string                result;
101     TopicStack::const_iterator topic;
102     for (topic = topicStack_.begin() + 1; topic != topicStack_.end(); ++topic)
103     {
104         if (!result.empty())
105         {
106             result.append(" ");
107         }
108         result.append((*topic)->name());
109     }
110     return result;
111 }
112
113 /********************************************************************
114  * HelpManager
115  */
116
117 HelpManager::HelpManager(const HelpTopicInterface &rootTopic,
118                          const HelpWriterContext  &context)
119     : impl_(new Impl(context))
120 {
121     impl_->topicStack_.push_back(&rootTopic);
122 }
123
124 HelpManager::~HelpManager()
125 {
126 }
127
128 void HelpManager::enterTopic(const char *name)
129 {
130     const HelpTopicInterface &topic = impl_->currentTopic();
131     if (!topic.hasSubTopics())
132     {
133         GMX_THROW(InvalidInputError(
134                           formatString("Help topic '%s' has no subtopics",
135                                        impl_->currentTopicAsString().c_str())));
136     }
137     const HelpTopicInterface *newTopic = topic.findSubTopic(name);
138     if (newTopic == NULL)
139     {
140         if (impl_->isAtRootTopic())
141         {
142             GMX_THROW(InvalidInputError(
143                               formatString("No help available for '%s'", name)));
144         }
145         else
146         {
147             GMX_THROW(InvalidInputError(
148                               formatString("Help topic '%s' has no subtopic '%s'",
149                                            impl_->currentTopicAsString().c_str(), name)));
150         }
151     }
152     impl_->topicStack_.push_back(newTopic);
153 }
154
155 void HelpManager::enterTopic(const std::string &name)
156 {
157     enterTopic(name.c_str());
158 }
159
160 void HelpManager::writeCurrentTopic() const
161 {
162     const HelpTopicInterface &topic = impl_->currentTopic();
163     topic.writeHelp(impl_->rootContext_);
164 }
165
166 } // namespace gmx