#ifndef GLTB_COMMANDLINE_H_ #define GLTB_COMMANDLINE_H_ #include #include #include #ifdef USE_GLM #include #endif #include "GLTB/referencedobject.h" #include "GLTB/refptr.h" namespace gltb { class CommandLine { public: CommandLine() {}; CommandLine(int argc, char *argv[]) { setArgs(argc, argv); }; void setArgs(int argc, char *argv[]); bool checkPresence(std::string argument); std::string getStringOption(std::string argument); #ifdef USE_GLM glm::vec2 getVec2Option(std::string argument, bool &isPresent); glm::vec2 getVec2Option(std::string argument); glm::vec3 getVec3Option(std::string argument, bool &isPresent); glm::vec3 getVec3Option(std::string argument); glm::vec4 getVec4Option(std::string argument, bool &isPresent); glm::vec4 getVec4Option(std::string argument); glm::ivec2 getIVec2Option(std::string argument, bool &isPresent); glm::ivec2 getIVec2Option(std::string argument); glm::ivec3 getIVec3Option(std::string argument, bool &isPresent); glm::ivec3 getIVec3Option(std::string argument); glm::ivec4 getIVec4Option(std::string argument, bool &isPresent); glm::ivec4 getIVec4Option(std::string argument); #endif int getNumUnvisitedParameters(); std::string getUnvisitedParameter(int numParameter); private: std::vector parameters; std::vector visited; }; class CommandLineArgument : public ReferencedObject { public: CommandLineArgument(std::string name, int numParameters, bool onlyOnce = true) : name(name), numParameters(numParameters), onlyOnce(onlyOnce) {} std::string getName() { return name; } int getNumParameters() { return numParameters; } bool onlyOnceAllowed() { return onlyOnce; } void _addParameters(std::vector parameters) { this->parameters.push_back(parameters); } bool isPresent() { return parameters.size() > 0; } int getNumOccurrences() { return parameters.size(); } std::vector getParameters(int occurrence = 0) { return parameters[occurrence]; } private: std::string name; int numParameters; bool onlyOnce; std::vector> parameters; }; class CommandLineIntArgument : public CommandLineArgument { public: CommandLineIntArgument(std::string name, bool onlyOnce = true) : CommandLineArgument(name, 1, onlyOnce) { } int getValue(int occurrence = 0) { return atoi(getParameters(occurrence)[0].c_str()); } }; class CommandLineFloatArgument : public CommandLineArgument { public: CommandLineFloatArgument(std::string name, bool onlyOnce = true) : CommandLineArgument(name, 1, onlyOnce) { } float getValue(int occurrence = 0) { return atof(getParameters(occurrence)[0].c_str()); } }; #ifdef USE_GLM template class CommandLineFloatVectorArgument : public CommandLineArgument { public: CommandLineFloatVectorArgument(std::string name, bool onlyOnce = true) : CommandLineArgument(name, vectorType().length(), onlyOnce) { } vectorType getValue(int occurrence = 0) { vectorType value; for(size_t i = 0; i < value.length(); i++) { value[i] = atof(getParameters(occurrence)[i].c_str()); } return value; } }; typedef CommandLineFloatVectorArgument CommandLineVec2Argument; typedef CommandLineFloatVectorArgument CommandLineVec3Argument; typedef CommandLineFloatVectorArgument CommandLineVec4Argument; template class CommandLineIntVectorArgument : public CommandLineArgument { public: CommandLineIntVectorArgument(std::string name, bool onlyOnce = true) : CommandLineArgument(name, vectorType().length(), onlyOnce) { } vectorType getValue(int occurrence = 0) { vectorType value; for(size_t i = 0; i < value.length(); i++) { value[i] = atoi(getParameters(occurrence)[i].c_str()); } return value; } }; typedef CommandLineIntVectorArgument CommandLineIVec2Argument; typedef CommandLineIntVectorArgument CommandLineIVec3Argument; typedef CommandLineIntVectorArgument CommandLineIVec4Argument; #endif // USE_GLM class CommandLineParser : public ReferencedObject { public: void registerArgument(RefPtr argument); void parse(int argc, const char *argv[]); std::vector> getArguments() { return arguments; } std::vector getUnhandledArguments() {return unhandledArguments; } private: std::vector> arguments; std::vector argumentHandled; std::vector unhandledArguments; }; } #endif /*GLTB_COMMANDLINE_H_*/