SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinehelpcontext.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements gmx::CommandLineHelpContext.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_commandline
42  */
43 #include "gmxpre.h"
44
45 #include "cmdlinehelpcontext.h"
46
47 #include "gromacs/utility/gmxassert.h"
48
49 #include "shellcompletions.h"
50
51 namespace gmx
52 {
53
54 namespace
55 {
56
57 /*! \brief
58  * Stores the global context set with GlobalCommandLineHelpContext.
59  *
60  * This is not protected by a mutex, since it is only used in command-line
61  * start-up (i.e., single-threaded context), and is inherently not thread-safe.
62  *
63  * \ingroup module_commandline
64  */
65 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
66 const CommandLineHelpContext* g_globalContext = nullptr;
67
68 } // namespace
69
70 /*! \internal \brief
71  * Private implementation class for CommandLineHelpContext.
72  *
73  * \ingroup module_commandline
74  */
75 class CommandLineHelpContext::Impl
76 {
77 public:
78     //! Creates the implementation class and the low-level context.
79     Impl(TextWriter* writer, HelpOutputFormat format, const HelpLinks* links) :
80         writerContext_(writer, format, links),
81         moduleDisplayName_("gmx"),
82         completionWriter_(nullptr),
83         bHidden_(false)
84     {
85     }
86     //! Creates an implementation class from a low-level context.
87     explicit Impl(const HelpWriterContext& writerContext) :
88         writerContext_(writerContext), completionWriter_(nullptr), bHidden_(false)
89     {
90     }
91
92     //! Wrapped lower-level context.
93     HelpWriterContext writerContext_;
94     //! Display name for the module for which help is written.
95     std::string moduleDisplayName_;
96     //! Shell completion writer (`NULL` if not doing completions).
97     ShellCompletionWriter* completionWriter_;
98     //! Whether hidden options should be shown in help output.
99     bool bHidden_;
100 };
101
102 CommandLineHelpContext::CommandLineHelpContext(TextWriter*        writer,
103                                                HelpOutputFormat   format,
104                                                const HelpLinks*   links,
105                                                const std::string& programName) :
106     impl_(new Impl(writer, format, links))
107 {
108     impl_->writerContext_.setReplacement("[PROGRAM]", programName);
109 }
110
111 CommandLineHelpContext::CommandLineHelpContext(const HelpWriterContext& writerContext) :
112     impl_(new Impl(writerContext))
113 {
114 }
115
116 CommandLineHelpContext::CommandLineHelpContext(ShellCompletionWriter* writer) :
117     impl_(new Impl(&writer->outputWriter(), eHelpOutputFormat_Other, nullptr))
118 {
119     impl_->completionWriter_ = writer;
120 }
121
122 CommandLineHelpContext::CommandLineHelpContext(const CommandLineHelpContext& other) :
123     impl_(new Impl(*other.impl_))
124 {
125 }
126
127 CommandLineHelpContext::CommandLineHelpContext(CommandLineHelpContext&& other) noexcept :
128     impl_(std::move(other.impl_))
129 {
130 }
131
132 CommandLineHelpContext& CommandLineHelpContext::operator=(CommandLineHelpContext&& other) noexcept
133 {
134     impl_ = std::move(other.impl_);
135     return *this;
136 }
137
138 CommandLineHelpContext::~CommandLineHelpContext() {}
139
140 void CommandLineHelpContext::setModuleDisplayName(const std::string& name)
141 {
142     impl_->writerContext_.setReplacement("[THISMODULE]", "[TT]" + name + "[tt]");
143     impl_->moduleDisplayName_ = name;
144 }
145
146 void CommandLineHelpContext::setShowHidden(bool bHidden)
147 {
148     impl_->bHidden_ = bHidden;
149 }
150
151 void CommandLineHelpContext::enterSubSection(const std::string& title)
152 {
153     impl_->writerContext_.enterSubSection(title);
154 }
155
156 const HelpWriterContext& CommandLineHelpContext::writerContext() const
157 {
158     return impl_->writerContext_;
159 }
160
161 const char* CommandLineHelpContext::moduleDisplayName() const
162 {
163     return impl_->moduleDisplayName_.c_str();
164 }
165
166 bool CommandLineHelpContext::showHidden() const
167 {
168     return impl_->bHidden_;
169 }
170
171 bool CommandLineHelpContext::isCompletionExport() const
172 {
173     return impl_->completionWriter_ != nullptr;
174 }
175
176 ShellCompletionWriter& CommandLineHelpContext::shellCompletionWriter() const
177 {
178     GMX_RELEASE_ASSERT(isCompletionExport(), "Invalid call when not writing shell completions");
179     return *impl_->completionWriter_;
180 }
181
182 /********************************************************************
183  * GlobalCommandLineHelpContext
184  */
185
186 // static
187 const CommandLineHelpContext* GlobalCommandLineHelpContext::get()
188 {
189     return g_globalContext;
190 }
191
192 GlobalCommandLineHelpContext::GlobalCommandLineHelpContext(const CommandLineHelpContext& context)
193 {
194     GMX_RELEASE_ASSERT(g_globalContext == nullptr, "Global context set more than once");
195     g_globalContext = &context;
196 }
197
198 GlobalCommandLineHelpContext::~GlobalCommandLineHelpContext()
199 {
200     g_globalContext = nullptr;
201 }
202
203 } // namespace gmx