0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / ViewerTest / ViewerTest_CmdParser.hxx
CommitLineData
189f85a3 1// Created on: 2015-03-15
2// Created by: Danila ULYANOV
3// Copyright (c) 2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#ifndef _ViewerTest_CmdParser_HeaderFile
17#define _ViewerTest_CmdParser_HeaderFile
18
293211ae 19#include <Graphic3d_Vec3.hxx>
20
21#include <limits>
189f85a3 22#include <map>
23#include <vector>
293211ae 24#include <set>
189f85a3 25#include <string>
189f85a3 26
293211ae 27class Quantity_Color;
28class Quantity_ColorRGBA;
29class gp_Vec;
30class gp_Pnt;
31
32//! A key for a command line option used for a ViewerTest_CmdParser work
33typedef std::size_t ViewerTest_CommandOptionKey;
34
35//! A set of keys for command-line options
36typedef std::set<ViewerTest_CommandOptionKey> ViewerTest_CommandOptionKeySet;
189f85a3 37
38//! Command parser.
39class ViewerTest_CmdParser
40{
41public:
293211ae 42 //! The key of the unnamed command option
43 static const std::size_t THE_UNNAMED_COMMAND_OPTION_KEY;
189f85a3 44
293211ae 45 //! The key of the help command option
46 static const std::size_t THE_HELP_COMMAND_OPTION_KEY;
47
48 //! Initializes help option.
49 //! @param theDescription the description of the command
50 ViewerTest_CmdParser (const std::string& theDescription = std::string());
189f85a3 51
52 //! Sets description for command.
293211ae 53 void SetDescription (const std::string& theDescription)
189f85a3 54 {
55 myDescription = theDescription;
56 }
57
58 //! Adds option to available option list. Several names may be provided if separated with '|'.
293211ae 59 //! @param theOptionNames the list of possible option names separated with '|'
60 //! (the first name is main, the other names are aliases)
61 //! @param theOptionDescription the description of the option
62 //! @return an access key of the newly added option
63 ViewerTest_CommandOptionKey AddOption (const std::string& theOptionNames,
64 const std::string& theOptionDescription = std::string());
189f85a3 65
66 //! Prints help message based on provided command and options descriptions.
293211ae 67 void PrintHelp() const;
68
69 //! Parses argument list (skips the command name); assigns local arguments to each option.
70 void Parse (Standard_Integer theArgsNb, const char* const* theArgVec);
71
72 //! Gets an option name by its access key
73 //! @param theOptionKey the access key of the option which name is to be found
74 //! @retuan a name of the option with the given access key
75 std::string GetOptionNameByKey (ViewerTest_CommandOptionKey theOptionKey) const;
76
77 //! Gets a set of used options
78 //! @return a set of used options
79 ViewerTest_CommandOptionKeySet GetUsedOptions() const;
80
81 //! Tests if there were no command line options provided
82 //! @return true if no command line options were provided, or false otherwise
83 bool HasNoOption() const;
84
85 //! Tests if the unnamed command line option was provided
86 //! @return true if the unnamed command line option was provided, or false otherwise
87 bool HasUnnamedOption() const;
189f85a3 88
293211ae 89 //! Tests if only unnamed command line option was provided
90 //! @return true if only unnamed command line option was provided, or false otherwise
91 bool HasOnlyUnnamedOption() const;
189f85a3 92
293211ae 93 //! Checks if option was used with given minimal number of arguments.
189f85a3 94 //! Prints error message if isFatal flag was set.
293211ae 95 //! @param theOptionName the name of the option to be checked
96 //! @param theMandatoryArgsNb the number of mandatory arguments
97 //! @param isFatal the flag that controls printing of an error message
98 //! @return true if an option was set, or false otherwise
99 bool HasOption (const std::string& theOptionName,
100 std::size_t theMandatoryArgsNb = 0,
101 bool isFatal = Standard_False) const;
102
103 //! Checks if option was used with given minimal number of arguments.
104 //! Prints error message if isFatal flag was set.
105 //! @param theOptionKey the access key of the option to be checked
106 //! @param theMandatoryArgsNb the number of mandatory arguments
107 //! @param isFatal the flag that controls printing of an error message
108 //! @return true if an option was set, or false otherwise
109 bool HasOption (ViewerTest_CommandOptionKey theOptionKey,
110 std::size_t theMandatoryArgsNb = 0,
111 bool isFatal = Standard_False) const;
112
113 //! Gets a number of option arguments
114 //! @param theOptionName the name of the option
115 //! @return a number of option arguments, or 0 if option was not used
116 Standard_Integer GetNumberOfOptionArguments (const std::string& theOptionName) const;
117
118 //! Gets a number of option arguments
119 //! @param theOptionKey the access key of the option
120 //! @return a number of option arguments, or 0 if option was not used
121 Standard_Integer GetNumberOfOptionArguments (ViewerTest_CommandOptionKey theOptionKey) const;
122
123 //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
124 //! @param theOptionName the name of the option which argument is to be accessed
125 //! @param theArgumentIndex the index of an accessed argument
126 //! @param theOptionArgument an argument of the option with the given name
127 //! @return true if an access was successful, or false otherwise
128 bool Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex, std::string& theOptionArgument) const;
129
130 //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
131 //! @param theOptionKey the access key of the option which argument is to be accessed
132 //! @param theArgumentIndex the index of an accessed argument
133 //! @param theOptionArgument an argument of the option with the given key
134 //! @return true if an access was successful, or false otherwise
135 bool Arg (ViewerTest_CommandOptionKey theOptionKey,
136 Standard_Integer theArgumentIndex,
137 std::string& theOptionArgument) const;
189f85a3 138
139 //! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
293211ae 140 //! @param theOptionName the name of the option which argument is to be accessed
141 //! @param theArgumentIndex the index of an accessed argument
142 //! @return an argument of the option with the given name
143 std::string Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex) const;
144
145 //! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
146 //! @param theOptionKey the access key of the option which argument is to be accessed
147 //! @param theArgumentIndex the index of an accessed argument
148 //! @return an argument of the option with the given key
149 std::string Arg (ViewerTest_CommandOptionKey theOptionKey, Standard_Integer theArgumentIndex) const;
189f85a3 150
625e1958 151 // Interprets arguments of option 'theOptionName' as float vector starting with index 'theArgumentIndex'.
293211ae 152 Graphic3d_Vec3 ArgVec3f (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
189f85a3 153
625e1958 154 // Interprets arguments of option 'theOptionName' as double vector starting with index 'theArgumentIndex'.
293211ae 155 Graphic3d_Vec3d ArgVec3d (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
189f85a3 156
625e1958 157 // Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
293211ae 158 gp_Vec ArgVec (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
189f85a3 159
625e1958 160 // Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
293211ae 161 gp_Pnt ArgPnt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
625e1958 162
163 // Interprets arguments of option 'theOptionName' as double at index 'theArgumentIndex'.
293211ae 164 Standard_Real ArgDouble (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
625e1958 165
166 // Interprets arguments of option 'theOptionName' as float at index 'theArgumentIndex'.
293211ae 167 Standard_ShortReal ArgFloat (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
625e1958 168
169 // Interprets arguments of option 'theOptionName' as integer at index 'theArgumentIndex'.
293211ae 170 Standard_Integer ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
625e1958 171
172 // Interprets arguments of option 'theOptionName' as boolean at index 'theArgumentIndex'.
293211ae 173 bool ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
174
175 //! Interprets arguments of the option 'theOptionName' with the index 'theArgumentIndex' as an RGB(A) color object.
176 //! @tparam theColor the type of a resulting RGB(A) color object
177 //! @param theOptionName the name of the option which arguments are to be interpreted
178 //! @param theArgumentIndex the index of the first argument to be interpreted
179 //! (will be promoted to the next argument after the block of interpreted arguments)
180 //! @param theColor a color that is an interpretation of argument(s) of the option with the given name
181 //! @return true if an interpretation was successful, or false otherwise
182 template <typename TheColor>
183 bool ArgColor (const std::string& theOptionName, Standard_Integer& theArgumentIndex, TheColor& theColor) const;
184
185 //! Interprets arguments of the option with the key 'theOptionKey' as an RGB(A) color object.
186 //! @tparam theColor the type of a resulting RGB(A) color object
187 //! @param theOptionKey the access key of the option which arguments are to be interpreted
188 //! @param theArgumentIndex the index of the first argument to be interpreted
189 //! (will be promoted to the next argument after the block of interpreted arguments)
190 //! @param theColor a color that is an interpretation of argument(s) of the option with the given name
191 //! @return true if an interpretation was successful, or false otherwise
192 template <typename TheColor>
193 bool ArgColor (ViewerTest_CommandOptionKey theOptionKey,
194 Standard_Integer& theArgumentIndex,
195 TheColor& theColor) const;
189f85a3 196
197private:
293211ae 198 //! A list of aliases to a command option name
199 typedef std::vector<std::string> OptionAliases;
189f85a3 200
293211ae 201 //! A map from all possible option names to option access keys
202 typedef std::map<std::string, ViewerTest_CommandOptionKey> OptionMap;
203
204 //! A map from keys of used options to their indices in the storage
205 typedef std::map<ViewerTest_CommandOptionKey, std::size_t> UsedOptionMap;
206
207 //! A list of command option arguments
208 typedef std::vector<std::string> OptionArguments;
209
210 //! A storage of arguments of different command options
211 typedef std::vector<OptionArguments> OptionArgumentsStorage;
189f85a3 212
293211ae 213 //! A full description of a command option
214 struct CommandOption
215 {
216 std::string Name; //!< A command option name
217 OptionAliases Aliases; //!< A list of aliases to a command option name
218 std::string Description; //!< A text description of a command option
189f85a3 219 };
220
293211ae 221 // A storage of command options descriptions
222 typedef std::vector<CommandOption> CommandOptionStorage;
223
224 // A list of raw string arguments
225 typedef std::vector<const char*> RawStringArguments;
226
189f85a3 227 //! Description of command.
228 std::string myDescription;
229
189f85a3 230 //! Container which stores option objects.
293211ae 231 std::vector<CommandOption> myOptionStorage;
232
233 //! Map from all possible option names to option access keys (that are indices in myOptionStorage)
234 OptionMap myOptionMap;
235
236 //! Map from keys of used options to their indices in the option arguments storage
237 UsedOptionMap myUsedOptionMap;
238
239 //! Container which stores the arguments of all used options
240 OptionArgumentsStorage myOptionArgumentStorage;
241
242 //! Gets an access key of the option
243 //! @param theOptionName the name of the option which key is to be found
244 //! @param theOptionKey an access key of the option with the given name
245 //! @return true if the given option was found, or false otherwise
246 bool findOptionKey (const std::string& theOptionName, ViewerTest_CommandOptionKey& theOptionKey) const;
247
248 //! Gets an index of an option that was used
249 //! @param theOptionKey the access key of the used option which index is to be found
250 //! @param theUsedOptionIndex an index of the used option with the given access key
251 //! @return true if the given option was not found or not used, or false otherwise
252 bool findUsedOptionIndex (ViewerTest_CommandOptionKey theOptionKey, std::size_t& theUsedOptionIndex) const;
253
254 //! Gets an index of an option that was used
255 //! @param theOptionName the name of the used option which index is to be found
256 //! @param theUsedOptionIndex an index of the used option with the given name
257 //! @return true if the given option was not found or not used, or false otherwise
258 bool findUsedOptionIndex (const std::string& theOptionName, std::size_t& theUsedOptionIndex) const;
259
260 //! Adds the option that is used in the passed command line parameters
261 //! @param theNewUsedOptionKey the access key of the adding option
262 //! @return an index of a newly added option
263 std::size_t addUsedOption (ViewerTest_CommandOptionKey theNewUsedOptionKey);
264
265 //! Gets an index of an option that was used
266 //! @param theOptionName the name of the used option which index is to be found
267 //! @param theUsedOptionIndex an index of the used option with the given name
268 //! @return true if the given option was not found or not used, or false otherwise
269 RawStringArguments getRawStringArguments (std::size_t theUsedOptionIndex) const;
189f85a3 270};
271
272#endif // _ViewerTest_CmdParser_HeaderFile