1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-09-08 14:17:06 +03:00

0030592: Draw Harness, ViewerTest - provide vbackground command unifying vsetbg, vsetbgmode, vsetgradientbg, vsetgrbgmode, vsetcolorbg

A new command vbackground is created. Old background commands are made aliases for the newly created command (including vsetdefaultbg).
Tests are modified using newly added command vbackground.
This commit is contained in:
tiv
2019-04-22 10:51:22 +03:00
committed by bugmaster
parent 1bbd7c793c
commit 293211aee0
54 changed files with 1871 additions and 930 deletions

View File

@@ -5965,7 +5965,7 @@ static int VBsdf (Draw_Interpretor& theDI,
ViewerTest_CmdParser aCmd;
aCmd.AddDescription ("Adjusts parameters of material BSDF:");
aCmd.SetDescription ("Adjusts parameters of material BSDF:");
aCmd.AddOption ("print|echo|p", "Prints BSDF");
@@ -6007,7 +6007,7 @@ static int VBsdf (Draw_Interpretor& theDI,
}
// find object
TCollection_AsciiString aName (aCmd.Arg ("", 0).c_str());
TCollection_AsciiString aName (aCmd.Arg (ViewerTest_CmdParser::THE_UNNAMED_COMMAND_OPTION_KEY, 0).c_str());
Handle(AIS_InteractiveObject) anIObj;
if (!GetMapOfAIS().Find2 (aName, anIObj))
{

View File

@@ -16,237 +16,593 @@
#include <ViewerTest_CmdParser.hxx>
#include <Draw.hxx>
#include <ViewerTest.hxx>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
//===============================================================================================
//function : ViewerTest_CmdParser
//purpose :
//===============================================================================================
ViewerTest_CmdParser::ViewerTest_CmdParser()
namespace
{
ViewerTest_CmdOption aDefaultOption;
myArgumentStorage.push_back (aDefaultOption);
myArgumentLists[""] = 0;
myArgumentLists["help"] = 0;
//! Converts the given string to lowercase
//! @param theString the string to be converted
//! @return a converted string (a string in lowercase)
static std::string toLowerCase (std::string theString)
{
std::transform (theString.begin(), theString.end(), theString.begin(), ::LowerCase);
return theString;
}
//! Converts the vector of std::strings to a vector of pointers to its data
//! @param theStringList the vector of strings to be converted
//! @return a vector of pointers to the data of given strings
static std::vector<const char*> convertToRawStringList (const std::vector<std::string>& theStringList)
{
const std::size_t aListSize = theStringList.size();
std::vector<const char*> aRawStringList (aListSize);
for (std::size_t anIndex = 0; anIndex < aListSize; ++anIndex)
{
aRawStringList[anIndex] = theStringList[anIndex].c_str();
}
return aRawStringList;
}
} // namespace
const std::size_t ViewerTest_CmdParser::THE_UNNAMED_COMMAND_OPTION_KEY = (std::numeric_limits<std::size_t>::max)();
const std::size_t ViewerTest_CmdParser::THE_HELP_COMMAND_OPTION_KEY = 0;
//===============================================================================================
// function : ViewerTest_CmdParser
// purpose :
//===============================================================================================
ViewerTest_CmdParser::ViewerTest_CmdParser (const std::string& theDescription) : myDescription (theDescription)
{
AddOption ("help|h", "Prints a short description of the command and its options.");
}
//===============================================================================================
//function : AddOption
//purpose :
// function : AddOption
// purpose :
//===============================================================================================
void ViewerTest_CmdParser::AddOption (const std::string& theOptionNames, const std::string& theOptionDescription)
ViewerTest_CommandOptionKey ViewerTest_CmdParser::AddOption (const std::string& theOptionNames,
const std::string& theOptionDescription)
{
ViewerTest_CmdOption aNewOption;
CommandOption aNewOption;
// extract option names
std::vector<std::string> aNames;
std::stringstream aStream (theOptionNames);
std::string anItem;
std::stringstream aStream (theOptionNames);
std::string anItem;
while (std::getline (aStream, anItem, '|'))
{
std::transform (anItem.begin(), anItem.end(), anItem.begin(), ::LowerCase);
if (!anItem.empty())
{
aNames.push_back (anItem);
}
}
aNewOption.Name = aNames.front();
aNewOption.Description = theOptionDescription;
aNewOption.IsSet = Standard_False;
aNewOption.Name = aNames.front();
if (aNames.size() > 1)
{
const std::size_t aNumberOfAliases = aNames.size() - 1;
myArgumentStorage.push_back (aNewOption);
aNewOption.Aliases.reserve (aNumberOfAliases);
std::copy (aNames.begin() + 1, aNames.end(), std::back_inserter (aNewOption.Aliases));
}
aNewOption.Description = theOptionDescription;
const ViewerTest_CommandOptionKey aNewOptionKey = myOptionStorage.size();
myOptionStorage.push_back (aNewOption);
std::vector<std::string>::const_iterator anIt = aNames.begin();
for (; anIt != aNames.end(); ++anIt)
{
myArgumentLists[*anIt] = (Standard_Integer) myArgumentStorage.size() - 1;
const std::string aNameInLowerCase = toLowerCase (*anIt);
myOptionMap[aNameInLowerCase] = aNewOptionKey;
}
return aNewOptionKey;
}
//===============================================================================================
//function : Help
//purpose :
// function : PrintHelp
// purpose :
//===============================================================================================
void ViewerTest_CmdParser::Help()
void ViewerTest_CmdParser::PrintHelp() const
{
std::cout << myDescription << std::endl;
std::vector<ViewerTest_CmdOption>::const_iterator anIt = myArgumentStorage.begin();
for (++anIt; anIt != myArgumentStorage.end(); ++anIt)
std::vector<CommandOption>::const_iterator anIt = myOptionStorage.begin();
for (++anIt; anIt != myOptionStorage.end(); ++anIt)
{
std::cout << "\n -" << (*anIt).Name << " : " << (*anIt).Description;
const CommandOption& aCommandOption = *anIt;
std::cout << "\n\t-" << aCommandOption.Name;
const OptionAliases& anAliases = aCommandOption.Aliases;
if (!anAliases.empty())
{
std::cout << " (-" << anAliases.front();
for (OptionAliases::const_iterator anAliasIterator = anAliases.begin() + 1; anAliasIterator != anAliases.end();
++anAliasIterator)
{
std::cout << ", -" << *anAliasIterator;
}
std::cout << ")";
}
std::cout << " : " << aCommandOption.Description;
}
std::cout << std::endl;
}
//===============================================================================================
//function : Parse
//purpose :
// function : Parse
// purpose :
//===============================================================================================
void ViewerTest_CmdParser::Parse (Standard_Integer theArgsNb, const char** theArgVec)
void ViewerTest_CmdParser::Parse (const Standard_Integer theArgsNb, const char* const* const theArgVec)
{
Standard_Integer aCurrentOption = 0;
std::size_t aCurrentUsedOptionIndex = 0;
for (Standard_Integer anIter = 1; anIter < theArgsNb; ++anIter)
{
if (theArgVec[anIter][0] == '-' && !std::isdigit (theArgVec[anIter][1]))
const char* const anArgument = theArgVec[anIter];
if (anArgument[0] == '-' && !std::isdigit (anArgument[1]))
{
std::string anOptionName (&theArgVec[anIter][1]);
std::transform (anOptionName.begin(), anOptionName.end(), anOptionName.begin(), ::LowerCase);
std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (anOptionName);
if (aMapIter != myArgumentLists.end())
const std::string anOptionName = toLowerCase (anArgument + 1);
OptionMap::iterator aMapIter = myOptionMap.find (anOptionName);
if (aMapIter != myOptionMap.end())
{
aCurrentOption = aMapIter->second;
myArgumentStorage[aCurrentOption].IsSet = true;
myArgumentStorage[aCurrentOption].Arguments.clear();
const ViewerTest_CommandOptionKey aCurrentUsedOptionKey = aMapIter->second;
aCurrentUsedOptionIndex = addUsedOption (aCurrentUsedOptionKey);
}
else
{
std::cerr << "Error: unknown argument '" << theArgVec[anIter] << "'\n";
std::cerr << "Error: unknown argument '" << anOptionName << "'\n";
return;
}
}
else
{
myArgumentStorage[aCurrentOption].Arguments.push_back (theArgVec[anIter]);
if (anIter == 1)
{
aCurrentUsedOptionIndex = addUsedOption (THE_UNNAMED_COMMAND_OPTION_KEY);
}
myOptionArgumentStorage[aCurrentUsedOptionIndex].push_back (anArgument);
}
}
}
//===============================================================================================
//function : HasOption
//purpose :
// function : GetOptionNameByKey
// purpose :
//===============================================================================================
Standard_Boolean ViewerTest_CmdParser::HasOption (const std::string& theOptionName, Standard_Integer theMandatoryArgsNb /*= 0*/, Standard_Boolean isFatal /*= Standard_False*/)
std::string ViewerTest_CmdParser::GetOptionNameByKey (const ViewerTest_CommandOptionKey theOptionKey) const
{
std::string aLowerName = theOptionName;
std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::LowerCase);
Standard_Boolean aResult = Standard_False;
std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
if (aMapIter != myArgumentLists.end())
if (theOptionKey == THE_UNNAMED_COMMAND_OPTION_KEY)
{
Standard_Integer anOption = aMapIter->second;
aResult = myArgumentStorage[anOption].Arguments.size() >= static_cast<size_t> (theMandatoryArgsNb);
if (isFatal && !aResult && myArgumentStorage[anOption].IsSet)
{
std::cerr << "Error: wrong syntax at argument '" << theOptionName << "'\n";
}
aResult &= myArgumentStorage[anOption].IsSet;
return "Unnamed";
}
return myOptionStorage[theOptionKey].Name;
}
//===============================================================================================
// function : GetUsedOptions
// purpose :
//===============================================================================================
ViewerTest_CommandOptionKeySet ViewerTest_CmdParser::GetUsedOptions() const
{
ViewerTest_CommandOptionKeySet aUsedOptions;
for (UsedOptionMap::const_iterator aUsedOptionMapIterator = myUsedOptionMap.begin();
aUsedOptionMapIterator != myUsedOptionMap.end();
++aUsedOptionMapIterator)
{
aUsedOptions.insert (aUsedOptionMapIterator->first);
}
return aUsedOptions;
}
//===============================================================================================
// function : HasNoOption
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::HasNoOption() const
{
return myUsedOptionMap.empty();
}
//===============================================================================================
// function : HasUnnamedOption
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::HasUnnamedOption() const
{
return myUsedOptionMap.find (THE_UNNAMED_COMMAND_OPTION_KEY) != myUsedOptionMap.end();
}
//===============================================================================================
// function : HasNoUnnamedOption
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::HasOnlyUnnamedOption() const
{
return HasUnnamedOption() && (myUsedOptionMap.size() == 1);
}
//===============================================================================================
// function : HasOption
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::HasOption (const std::string& theOptionName,
const std::size_t theMandatoryArgsNb /* = 0 */,
const bool isFatal /* = false */) const
{
ViewerTest_CommandOptionKey anOptionKey;
if (!findOptionKey (theOptionName, anOptionKey))
{
return false;
}
return HasOption (anOptionKey, theMandatoryArgsNb, isFatal);
}
//===============================================================================================
// function : HasOption
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::HasOption (const ViewerTest_CommandOptionKey theOptionKey,
const std::size_t theMandatoryArgsNb /* = 0 */,
const bool isFatal /* = false */) const
{
std::size_t aUsedOptionIndex = 0;
if (!findUsedOptionIndex (theOptionKey, aUsedOptionIndex))
{
return false;
}
const OptionArguments& anOptionArguments = myOptionArgumentStorage[aUsedOptionIndex];
const bool aResult = (anOptionArguments.size() >= theMandatoryArgsNb);
if (isFatal && !aResult)
{
std::cerr << "Error: wrong syntax at option '" << myOptionStorage[theOptionKey].Name << "'\n"
<< "At least " << theMandatoryArgsNb << "expected, but only " << anOptionArguments.size()
<< "provided.\n";
}
return aResult;
}
//===============================================================================================
//function : Arg
//purpose :
// function : GetNumberOfOptionArguments
// purpose :
//===============================================================================================
std::string ViewerTest_CmdParser::Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex)
Standard_Integer ViewerTest_CmdParser::GetNumberOfOptionArguments (const std::string& theOptionName) const
{
std::string aLowerName = theOptionName;
std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::LowerCase);
std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
if (aMapIter != myArgumentLists.end())
ViewerTest_CommandOptionKey anOptionKey = THE_UNNAMED_COMMAND_OPTION_KEY;
if (!findOptionKey (theOptionName, anOptionKey))
{
Standard_Integer anOption = aMapIter->second;
if (myArgumentStorage[anOption].Arguments.size() > static_cast<size_t> (theArgumentIndex))
{
return myArgumentStorage[anOption].Arguments[theArgumentIndex];
}
else
{
std::cerr << "Error: wrong syntax at argument '" << aLowerName << "'\n";
}
return 0;
}
return "";
return GetNumberOfOptionArguments (anOptionKey);
}
//===============================================================================================
//function : ArgVec3f
//purpose :
// function : GetNumberOfOptionArguments
// purpose :
//===============================================================================================
Graphic3d_Vec3 ViewerTest_CmdParser::ArgVec3f (const std::string& theOptionName, Standard_Integer theArgumentIndex)
Standard_Integer ViewerTest_CmdParser::GetNumberOfOptionArguments (const ViewerTest_CommandOptionKey theOptionKey) const
{
return Graphic3d_Vec3 (static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str())),
static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str())),
static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str())));
std::size_t aUsedOptionIndex = 0;
if (!findUsedOptionIndex (theOptionKey, aUsedOptionIndex))
{
return false;
}
return static_cast<Standard_Integer> (myOptionArgumentStorage[aUsedOptionIndex].size());
}
//===============================================================================================
//function : ArgVec3d
//purpose :
// function : Arg
// purpose :
//===============================================================================================
Graphic3d_Vec3d ViewerTest_CmdParser::ArgVec3d (const std::string& theOptionName, Standard_Integer theArgumentIndex)
bool ViewerTest_CmdParser::Arg (const std::string& theOptionName,
const Standard_Integer theArgumentIndex,
std::string& theOptionArgument) const
{
return Graphic3d_Vec3d ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
Standard_ASSERT_RETURN (theArgumentIndex >= 0,
__FUNCTION__ ": 'theArgumentIndex' must be greater than or equal to zero.",
false);
ViewerTest_CommandOptionKey anOptionKey = THE_UNNAMED_COMMAND_OPTION_KEY;
if (!theOptionName.empty() && !findOptionKey (theOptionName, anOptionKey))
{
return false;
}
return Arg (anOptionKey, theArgumentIndex, theOptionArgument);
}
//===============================================================================================
//function : ArgVec
//purpose :
// function : Arg
// purpose :
//===============================================================================================
gp_Vec ViewerTest_CmdParser::ArgVec (const std::string& theOptionName, Standard_Integer theArgumentIndex)
bool ViewerTest_CmdParser::Arg (const ViewerTest_CommandOptionKey theOptionKey,
const Standard_Integer theArgumentIndex,
std::string& theOptionArgument) const
{
return gp_Vec ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
Standard_ASSERT_RETURN (theArgumentIndex >= 0,
__FUNCTION__ ": 'theArgumentIndex' must be greater than or equal to zero.",
false);
std::size_t aUsedOptionIndex = 0;
if (!findUsedOptionIndex (theOptionKey, aUsedOptionIndex))
{
return false;
}
const OptionArguments& anOptionArguments = myOptionArgumentStorage[aUsedOptionIndex];
if (static_cast<std::size_t> (theArgumentIndex) >= anOptionArguments.size())
{
return false;
}
theOptionArgument = anOptionArguments[theArgumentIndex];
return true;
}
//===============================================================================================
//function : ArgPnt
//purpose :
// function : Arg
// purpose :
//===============================================================================================
gp_Pnt ViewerTest_CmdParser::ArgPnt (const std::string& theOptionName, Standard_Integer theArgumentIndex)
std::string ViewerTest_CmdParser::Arg (const std::string& theOptionName, const Standard_Integer theArgumentIndex) const
{
return gp_Pnt ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
Standard_ASSERT_RETURN (theArgumentIndex >= 0,
__FUNCTION__ ": 'theArgumentIndex' must be greater than or equal to zero.",
std::string());
std::string anOptionArgument;
if (!Arg (theOptionName, theArgumentIndex, anOptionArgument))
{
return std::string();
}
return anOptionArgument;
}
//===============================================================================================
//function : ArgDouble
//purpose :
// function : Arg
// purpose :
//===============================================================================================
Standard_Real ViewerTest_CmdParser::ArgDouble (const std::string& theOptionName, Standard_Integer theArgumentIndex)
std::string ViewerTest_CmdParser::Arg (const ViewerTest_CommandOptionKey theOptionKey,
const Standard_Integer theArgumentIndex) const
{
std::string anOptionArgument;
if (!Arg (theOptionKey, theArgumentIndex, anOptionArgument))
{
return std::string();
}
return anOptionArgument;
}
//===============================================================================================
// function : ArgVec3f
// purpose :
//===============================================================================================
Graphic3d_Vec3 ViewerTest_CmdParser::ArgVec3f (const std::string& theOptionName,
Standard_Integer theArgumentIndex) const
{
return Graphic3d_Vec3 (
static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str())),
static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str())),
static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str())));
}
//===============================================================================================
// function : ArgVec3d
// purpose :
//===============================================================================================
Graphic3d_Vec3d ViewerTest_CmdParser::ArgVec3d (const std::string& theOptionName,
Standard_Integer theArgumentIndex) const
{
return Graphic3d_Vec3d (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
}
//===============================================================================================
// function : ArgVec
// purpose :
//===============================================================================================
gp_Vec ViewerTest_CmdParser::ArgVec (const std::string& theOptionName, Standard_Integer theArgumentIndex) const
{
return gp_Vec (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
}
//===============================================================================================
// function : ArgPnt
// purpose :
//===============================================================================================
gp_Pnt ViewerTest_CmdParser::ArgPnt (const std::string& theOptionName, Standard_Integer theArgumentIndex) const
{
return gp_Pnt (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
}
//===============================================================================================
// function : ArgDouble
// purpose :
//===============================================================================================
Standard_Real ViewerTest_CmdParser::ArgDouble (const std::string& theOptionName,
Standard_Integer theArgumentIndex) const
{
return Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str());
}
//===============================================================================================
//function : ArgFloat
//purpose :
// function : ArgFloat
// purpose :
//===============================================================================================
Standard_ShortReal ViewerTest_CmdParser::ArgFloat (const std::string& theOptionName, Standard_Integer theArgumentIndex)
Standard_ShortReal ViewerTest_CmdParser::ArgFloat (const std::string& theOptionName,
Standard_Integer theArgumentIndex) const
{
return static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()));
}
//===============================================================================================
//function : ArgInt
//purpose :
// function : ArgInt
// purpose :
//===============================================================================================
Standard_Integer ViewerTest_CmdParser::ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex)
Standard_Integer ViewerTest_CmdParser::ArgInt (const std::string& theOptionName,
const Standard_Integer theArgumentIndex) const
{
return static_cast<Standard_Integer> (Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str()));
}
//===============================================================================================
//function : ArgBool
//purpose :
// function : ArgBool
// purpose :
//===============================================================================================
Standard_Boolean ViewerTest_CmdParser::ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex)
bool ViewerTest_CmdParser::ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex) const
{
return Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str()) != 0;
}
//===============================================================================================
// function : ArgColor
// purpose :
//===============================================================================================
template <typename TheColor>
bool ViewerTest_CmdParser::ArgColor (const std::string& theOptionName,
Standard_Integer& theArgumentIndex,
TheColor& theColor) const
{
ViewerTest_CommandOptionKey anOptionKey;
if (!findOptionKey (theOptionName, anOptionKey))
{
return false;
}
return ArgColor (anOptionKey, theArgumentIndex, theColor);
}
//! ViewerTest_CmdParser::ArgColor() explicit template instantiation definitions
template bool ViewerTest_CmdParser::ArgColor (const std::string& theOptionName,
Standard_Integer& theArgumentIndex,
Quantity_Color& theColor) const;
template bool ViewerTest_CmdParser::ArgColor (const std::string& theOptionName,
Standard_Integer& theArgumentIndex,
Quantity_ColorRGBA& theColor) const;
//===============================================================================================
// function : ArgColor
// purpose :
//===============================================================================================
template <typename TheColor>
bool ViewerTest_CmdParser::ArgColor (const ViewerTest_CommandOptionKey theOptionKey,
Standard_Integer& theArgumentIndex,
TheColor& theColor) const
{
std::size_t aUsedOptionIndex = 0;
if (!findUsedOptionIndex (theOptionKey, aUsedOptionIndex))
{
return false;
}
const RawStringArguments aRawStringArguments = getRawStringArguments (aUsedOptionIndex);
const Standard_Integer aNumberOfArguments = static_cast<Standard_Integer> (aRawStringArguments.size());
Standard_ASSERT_RETURN (theArgumentIndex < aNumberOfArguments,
__FUNCTION__ ": 'theArgumentIndex' must be less than the number of command-line arguments "
"passed with the option which access key is 'theOptionKey'.",
false);
const Standard_Integer aNumberOfAvailableArguments = aNumberOfArguments - theArgumentIndex;
TheColor aColor;
const Standard_Integer aNumberOfParsedArguments = ViewerTest::ParseColor (aNumberOfAvailableArguments,
&aRawStringArguments[theArgumentIndex],
aColor);
if (aNumberOfParsedArguments == 0)
{
return false;
}
theArgumentIndex += aNumberOfParsedArguments;
theColor = aColor;
return true;
}
//! ViewerTest_CmdParser::ArgColor() explicit template instantiation definitions
template bool ViewerTest_CmdParser::ArgColor (ViewerTest_CommandOptionKey theOptionKey,
Standard_Integer& theArgumentIndex,
Quantity_Color& theColor) const;
template bool ViewerTest_CmdParser::ArgColor (ViewerTest_CommandOptionKey theOptionKey,
Standard_Integer& theArgumentIndex,
Quantity_ColorRGBA& theColor) const;
//===============================================================================================
// function : findUsedOptionKey
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::findOptionKey (const std::string& theOptionName,
ViewerTest_CommandOptionKey& theOptionKey) const
{
const std::string anOptionNameInLowercase = toLowerCase (theOptionName);
const OptionMap::const_iterator aMapIter = myOptionMap.find (anOptionNameInLowercase);
if (aMapIter == myOptionMap.end())
{
return false;
}
theOptionKey = aMapIter->second;
return true;
}
//===============================================================================================
// function : findUsedOptionKey
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::findUsedOptionIndex (const ViewerTest_CommandOptionKey theOptionKey,
std::size_t& theUsedOptionIndex) const
{
const UsedOptionMap::const_iterator aUsedOptionIterator = myUsedOptionMap.find (theOptionKey);
if (aUsedOptionIterator == myUsedOptionMap.end())
{
return false;
}
theUsedOptionIndex = aUsedOptionIterator->second;
return true;
}
//===============================================================================================
// function : findUsedOptionIndex
// purpose :
//===============================================================================================
bool ViewerTest_CmdParser::findUsedOptionIndex (const std::string& theOptionName, std::size_t& theUsedOptionIndex) const
{
ViewerTest_CommandOptionKey anOptionKey = THE_UNNAMED_COMMAND_OPTION_KEY;
if (!findOptionKey (theOptionName, anOptionKey))
{
return false;
}
std::size_t aUsedOptionIndex = 0;
if (!findUsedOptionIndex (anOptionKey, aUsedOptionIndex))
{
return false;
}
theUsedOptionIndex = aUsedOptionIndex;
return true;
}
//===============================================================================================
// function : addUsedOption
// purpose :
//===============================================================================================
std::size_t ViewerTest_CmdParser::addUsedOption (const ViewerTest_CommandOptionKey theNewUsedOptionKey)
{
const std::size_t aNewUsedOptionIndex = myOptionArgumentStorage.size();
myOptionArgumentStorage.push_back (OptionArguments());
myUsedOptionMap[theNewUsedOptionKey] = aNewUsedOptionIndex;
return aNewUsedOptionIndex;
}
//===============================================================================================
// function : getRawStringArguments
// purpose :
//===============================================================================================
ViewerTest_CmdParser::RawStringArguments ViewerTest_CmdParser::getRawStringArguments (
const std::size_t theUsedOptionIndex) const
{
Standard_ASSERT_RETURN (
theUsedOptionIndex < myOptionArgumentStorage.size(),
__FUNCTION__ ": 'theUsedOptionIndex' must be less than the size of 'myOptionArgumentStorage'.",
RawStringArguments());
const OptionArguments& anOptionArguments = myOptionArgumentStorage[theUsedOptionIndex];
return convertToRawStringList (anOptionArguments);
}

View File

@@ -16,93 +16,257 @@
#ifndef _ViewerTest_CmdParser_HeaderFile
#define _ViewerTest_CmdParser_HeaderFile
#include <Graphic3d_Vec3.hxx>
#include <limits>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <Standard.hxx>
#include <Graphic3d_Vec.hxx>
#include <gp_Vec.hxx>
class Quantity_Color;
class Quantity_ColorRGBA;
class gp_Vec;
class gp_Pnt;
//! A key for a command line option used for a ViewerTest_CmdParser work
typedef std::size_t ViewerTest_CommandOptionKey;
//! A set of keys for command-line options
typedef std::set<ViewerTest_CommandOptionKey> ViewerTest_CommandOptionKeySet;
//! Command parser.
class ViewerTest_CmdParser
{
public:
//! The key of the unnamed command option
static const std::size_t THE_UNNAMED_COMMAND_OPTION_KEY;
//! Initializes default option.
ViewerTest_CmdParser();
//! The key of the help command option
static const std::size_t THE_HELP_COMMAND_OPTION_KEY;
//! Initializes help option.
//! @param theDescription the description of the command
ViewerTest_CmdParser (const std::string& theDescription = std::string());
//! Sets description for command.
void AddDescription (const std::string& theDescription)
void SetDescription (const std::string& theDescription)
{
myDescription = theDescription;
}
//! Adds option to available option list. Several names may be provided if separated with '|'.
void AddOption (const std::string& theOptionNames, const std::string& theOptionDescription = "");
//! @param theOptionNames the list of possible option names separated with '|'
//! (the first name is main, the other names are aliases)
//! @param theOptionDescription the description of the option
//! @return an access key of the newly added option
ViewerTest_CommandOptionKey AddOption (const std::string& theOptionNames,
const std::string& theOptionDescription = std::string());
//! Prints help message based on provided command and options descriptions.
void Help();
void PrintHelp() const;
//! Parses argument list; assignes local arguments to each option.
void Parse (Standard_Integer theArgsNb,
const char** theArgVec);
//! Parses argument list (skips the command name); assigns local arguments to each option.
void Parse (Standard_Integer theArgsNb, const char* const* theArgVec);
//! Checks if option was set with given minimal argument number.
//! Gets an option name by its access key
//! @param theOptionKey the access key of the option which name is to be found
//! @retuan a name of the option with the given access key
std::string GetOptionNameByKey (ViewerTest_CommandOptionKey theOptionKey) const;
//! Gets a set of used options
//! @return a set of used options
ViewerTest_CommandOptionKeySet GetUsedOptions() const;
//! Tests if there were no command line options provided
//! @return true if no command line options were provided, or false otherwise
bool HasNoOption() const;
//! Tests if the unnamed command line option was provided
//! @return true if the unnamed command line option was provided, or false otherwise
bool HasUnnamedOption() const;
//! Tests if only unnamed command line option was provided
//! @return true if only unnamed command line option was provided, or false otherwise
bool HasOnlyUnnamedOption() const;
//! Checks if option was used with given minimal number of arguments.
//! Prints error message if isFatal flag was set.
Standard_Boolean HasOption (const std::string& theOptionName,
Standard_Integer theMandatoryArgsNb = 0,
Standard_Boolean isFatal = Standard_False);
//! @param theOptionName the name of the option to be checked
//! @param theMandatoryArgsNb the number of mandatory arguments
//! @param isFatal the flag that controls printing of an error message
//! @return true if an option was set, or false otherwise
bool HasOption (const std::string& theOptionName,
std::size_t theMandatoryArgsNb = 0,
bool isFatal = Standard_False) const;
//! Checks if option was used with given minimal number of arguments.
//! Prints error message if isFatal flag was set.
//! @param theOptionKey the access key of the option to be checked
//! @param theMandatoryArgsNb the number of mandatory arguments
//! @param isFatal the flag that controls printing of an error message
//! @return true if an option was set, or false otherwise
bool HasOption (ViewerTest_CommandOptionKey theOptionKey,
std::size_t theMandatoryArgsNb = 0,
bool isFatal = Standard_False) const;
//! Gets a number of option arguments
//! @param theOptionName the name of the option
//! @return a number of option arguments, or 0 if option was not used
Standard_Integer GetNumberOfOptionArguments (const std::string& theOptionName) const;
//! Gets a number of option arguments
//! @param theOptionKey the access key of the option
//! @return a number of option arguments, or 0 if option was not used
Standard_Integer GetNumberOfOptionArguments (ViewerTest_CommandOptionKey theOptionKey) const;
//! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
std::string Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex);
//! @param theOptionName the name of the option which argument is to be accessed
//! @param theArgumentIndex the index of an accessed argument
//! @param theOptionArgument an argument of the option with the given name
//! @return true if an access was successful, or false otherwise
bool Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex, std::string& theOptionArgument) const;
//! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
//! @param theOptionKey the access key of the option which argument is to be accessed
//! @param theArgumentIndex the index of an accessed argument
//! @param theOptionArgument an argument of the option with the given key
//! @return true if an access was successful, or false otherwise
bool Arg (ViewerTest_CommandOptionKey theOptionKey,
Standard_Integer theArgumentIndex,
std::string& theOptionArgument) const;
//! Accesses local argument of option 'theOptionName' with index 'theArgumentIndex'.
//! @param theOptionName the name of the option which argument is to be accessed
//! @param theArgumentIndex the index of an accessed argument
//! @return an argument of the option with the given name
std::string Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex) const;
//! Accesses a local argument with the index 'theArgumentIndex' of the option with the key 'theOptionKey'.
//! @param theOptionKey the access key of the option which argument is to be accessed
//! @param theArgumentIndex the index of an accessed argument
//! @return an argument of the option with the given key
std::string Arg (ViewerTest_CommandOptionKey theOptionKey, Standard_Integer theArgumentIndex) const;
// Interprets arguments of option 'theOptionName' as float vector starting with index 'theArgumentIndex'.
Graphic3d_Vec3 ArgVec3f (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
Graphic3d_Vec3 ArgVec3f (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as double vector starting with index 'theArgumentIndex'.
Graphic3d_Vec3d ArgVec3d (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
Graphic3d_Vec3d ArgVec3d (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
gp_Vec ArgVec (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
gp_Vec ArgVec (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as gp vector starting with index 'theArgumentIndex'.
gp_Pnt ArgPnt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
gp_Pnt ArgPnt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as double at index 'theArgumentIndex'.
Standard_Real ArgDouble (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
Standard_Real ArgDouble (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as float at index 'theArgumentIndex'.
Standard_ShortReal ArgFloat (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
Standard_ShortReal ArgFloat (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as integer at index 'theArgumentIndex'.
Standard_Integer ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
Standard_Integer ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
// Interprets arguments of option 'theOptionName' as boolean at index 'theArgumentIndex'.
Standard_Boolean ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0);
bool ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex = 0) const;
//! Interprets arguments of the option 'theOptionName' with the index 'theArgumentIndex' as an RGB(A) color object.
//! @tparam theColor the type of a resulting RGB(A) color object
//! @param theOptionName the name of the option which arguments are to be interpreted
//! @param theArgumentIndex the index of the first argument to be interpreted
//! (will be promoted to the next argument after the block of interpreted arguments)
//! @param theColor a color that is an interpretation of argument(s) of the option with the given name
//! @return true if an interpretation was successful, or false otherwise
template <typename TheColor>
bool ArgColor (const std::string& theOptionName, Standard_Integer& theArgumentIndex, TheColor& theColor) const;
//! Interprets arguments of the option with the key 'theOptionKey' as an RGB(A) color object.
//! @tparam theColor the type of a resulting RGB(A) color object
//! @param theOptionKey the access key of the option which arguments are to be interpreted
//! @param theArgumentIndex the index of the first argument to be interpreted
//! (will be promoted to the next argument after the block of interpreted arguments)
//! @param theColor a color that is an interpretation of argument(s) of the option with the given name
//! @return true if an interpretation was successful, or false otherwise
template <typename TheColor>
bool ArgColor (ViewerTest_CommandOptionKey theOptionKey,
Standard_Integer& theArgumentIndex,
TheColor& theColor) const;
private:
//! A list of aliases to a command option name
typedef std::vector<std::string> OptionAliases;
//! Object representing option state.
struct ViewerTest_CmdOption
//! A map from all possible option names to option access keys
typedef std::map<std::string, ViewerTest_CommandOptionKey> OptionMap;
//! A map from keys of used options to their indices in the storage
typedef std::map<ViewerTest_CommandOptionKey, std::size_t> UsedOptionMap;
//! A list of command option arguments
typedef std::vector<std::string> OptionArguments;
//! A storage of arguments of different command options
typedef std::vector<OptionArguments> OptionArgumentsStorage;
//! A full description of a command option
struct CommandOption
{
ViewerTest_CmdOption() : IsSet (Standard_False) {}
std::string Name;
std::string Description;
Standard_Boolean IsSet;
std::vector<std::string> Arguments;
std::string Name; //!< A command option name
OptionAliases Aliases; //!< A list of aliases to a command option name
std::string Description; //!< A text description of a command option
};
// A storage of command options descriptions
typedef std::vector<CommandOption> CommandOptionStorage;
// A list of raw string arguments
typedef std::vector<const char*> RawStringArguments;
//! Description of command.
std::string myDescription;
//! Map from all possible option names to option object indexes in myArgumentStorage.
std::map<std::string, Standard_Integer> myArgumentLists;
//! Container which stores option objects.
std::vector<ViewerTest_CmdOption> myArgumentStorage;
std::vector<CommandOption> myOptionStorage;
//! Map from all possible option names to option access keys (that are indices in myOptionStorage)
OptionMap myOptionMap;
//! Map from keys of used options to their indices in the option arguments storage
UsedOptionMap myUsedOptionMap;
//! Container which stores the arguments of all used options
OptionArgumentsStorage myOptionArgumentStorage;
//! Gets an access key of the option
//! @param theOptionName the name of the option which key is to be found
//! @param theOptionKey an access key of the option with the given name
//! @return true if the given option was found, or false otherwise
bool findOptionKey (const std::string& theOptionName, ViewerTest_CommandOptionKey& theOptionKey) const;
//! Gets an index of an option that was used
//! @param theOptionKey the access key of the used option which index is to be found
//! @param theUsedOptionIndex an index of the used option with the given access key
//! @return true if the given option was not found or not used, or false otherwise
bool findUsedOptionIndex (ViewerTest_CommandOptionKey theOptionKey, std::size_t& theUsedOptionIndex) const;
//! Gets an index of an option that was used
//! @param theOptionName the name of the used option which index is to be found
//! @param theUsedOptionIndex an index of the used option with the given name
//! @return true if the given option was not found or not used, or false otherwise
bool findUsedOptionIndex (const std::string& theOptionName, std::size_t& theUsedOptionIndex) const;
//! Adds the option that is used in the passed command line parameters
//! @param theNewUsedOptionKey the access key of the adding option
//! @return an index of a newly added option
std::size_t addUsedOption (ViewerTest_CommandOptionKey theNewUsedOptionKey);
//! Gets an index of an option that was used
//! @param theOptionName the name of the used option which index is to be found
//! @param theUsedOptionIndex an index of the used option with the given name
//! @return true if the given option was not found or not used, or false otherwise
RawStringArguments getRawStringArguments (std::size_t theUsedOptionIndex) const;
};
#endif // _ViewerTest_CmdParser_HeaderFile

File diff suppressed because it is too large Load Diff