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