AGX Dynamics  2.37.3.2
ArgumentParser.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXIO_ARGUMENT_PARSER_H
18 #define AGXIO_ARGUMENT_PARSER_H
19 
20 #include <string>
21 #include <sstream>
22 #include <agx/HashVector.h>
23 #include <agx/Referenced.h>
24 #include <agx/String.h>
25 #include <iostream>
26 #include <agx/agxCore_export.h>
27 
28 namespace agxIO
29 {
30 
39  {
40 
41  public:
47  {
48  public:
50  Argument();
51 
53  Argument (const std::string& argumentName);
54 
56  std::string getName() const;
57 
59  bool getHandled() const;
60 
62  void setHandled( bool handled);
63 
65  bool isOption() const;
66 
67 
68  private:
69  agx::String m_argumentName;
70  bool m_handled;
71  };
72 
74 
75  public:
76 
79 
80 #ifndef SWIG
81 
87  ArgumentParser( int argc, char** argv );
88 #endif
89 
93  ArgumentParser(const agx::StringVector& arguments);
94 
103  int find( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true);
104 
113  bool read( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true );
114 
125  template <typename T>
126  bool read1( const std::string& key, T& value, size_t startPosition = 0, bool findAlsoHandled = true );
127 
140  template <typename T1, typename T2>
141  bool read2( const std::string& key, T1& value1, T2& value2, size_t startPosition = 0, bool findAlsoHandled = true );
142 
157  template <typename T1, typename T2, typename T3>
158  bool read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition = 0, bool findAlsoHandled = true );
159 
164  void addArgument( const char* argumentName);
165 
176  template <typename T>
177  T defaultRead(const std::string& key, const T& defaultValue, size_t startPosition = 0, bool findAlsoHandled = true);
178 
184  bool reportUnreadArguments( std::ostream& out = std::cout );
185 
187  size_t getNumArguments() const;
188 
190  Argument* getArgument( size_t index );
191 
196  char** getArgv();
197 
198  protected:
199  virtual ~ArgumentParser();
200 
201  private:
202  // Relies on std streams to cast values in strings to templated type.
203  template<typename T>
204  static bool assignStringContent (const std::string& key, T& type);
205 
206  static bool assignStringContent (const std::string& key, std::string& type);
207 
208 
211  ArgumentParser& operator=(const ArgumentParser&);
212 
213  private:
214  ArgumentVector m_arguments;
215  char** m_argv;
216 
217  };
218 
219 
220 // IMPLEMENTATIONS //
221  template <typename T>
222  bool ArgumentParser::read1(const std::string& key, T& value, size_t startPosition /*= 0*/,
223  bool findAlsoHandled /*= true*/)
224  {
225  int pos = this->find(key, startPosition, findAlsoHandled);
226  if (pos >= 0 && pos + 1 < (int)m_arguments.size()) {
227  if (!assignStringContent (m_arguments[pos + 1].getName(), value))
228  return false;
229  m_arguments[pos].setHandled( true ); // key handled
230  m_arguments[pos + 1].setHandled( true ); // value handled
231  return true;
232  }
233  return false;
234  }
235 
236 
237  template <typename T1, typename T2>
238  bool ArgumentParser::read2( const std::string& key, T1& value1, T2& value2, size_t startPosition /*= 0*/,
239  bool findAlsoHandled /*= true*/)
240  {
241  int pos = find(key, startPosition, findAlsoHandled);
242  if (pos >= 0 && size_t(pos + 2) < m_arguments.size()) {
243  if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
244  !assignStringContent (m_arguments[pos + 2].getName(), value2))
245  return false;
246  m_arguments[pos].setHandled( true ); // key handled
247  m_arguments[pos + 1].setHandled( true ); // value1 handled
248  m_arguments[pos + 2].setHandled( true ); // value2 handled
249  return true;
250  }
251  return false;
252  }
253 
254 
255  template <typename T1, typename T2, typename T3>
256  bool ArgumentParser::read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition /*= 0*/,
257  bool findAlsoHandled /*= true*/)
258  {
259  int pos = this->find(key, startPosition, findAlsoHandled);
260  if (pos >= 0 && pos + 3 < (int)m_arguments.size()) {
261  if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
262  !assignStringContent (m_arguments[pos + 2].getName(), value2) ||
263  !assignStringContent (m_arguments[pos + 3].getName(), value3))
264  return false;
265  m_arguments[pos].setHandled( true ); // key handled
266  m_arguments[pos + 1].setHandled( true ); // value1 handled
267  m_arguments[pos + 2].setHandled( true ); // value2 handled
268  m_arguments[pos + 3].setHandled( true ); // value3 handled
269  return true;
270  }
271  return false;
272  }
273 
274 
275  template <typename T>
276  T ArgumentParser::defaultRead(const std::string& key, const T& defaultValue, size_t startPosition /*= 0*/,
277  bool findAlsoHandled /*= true*/)
278  {
279  T ret;
280  if (this->read1( key, ret, startPosition, findAlsoHandled ))
281  return ret;
282  return defaultValue;
283  }
284 
285 
286  template <typename T>
287  bool ArgumentParser::assignStringContent (const std::string& key, T& type)
288  {
289  std::stringstream ss( key );
290  ss >> type;
291  return (ss.good() || ss.rdstate() == std::ios_base::eofbit);
292  }
293 
294  template <>
295  bool AGXCORE_EXPORT ArgumentParser::assignStringContent<std::string>(const std::string& key, std::string& type);
296 
297  template <>
298  bool AGXCORE_EXPORT ArgumentParser::assignStringContent<agx::String>(const std::string& key, agx::String& type);
299 
301 }
302 
303 #endif
304 
bool AGXPHYSICS_EXPORT read(const std::string &filename, agxStream::SerializablePtrVector &readObjects)
Open and read serializable objects from a file on disk with previously stored objects.
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:41
agx::Vector< Argument > ArgumentVector
Help class for handling arguments.
A small argument parser class.
Base class providing referencing counted objects.
Definition: Referenced.h:119
bool read3(const std::string &key, T1 &value1, T2 &value2, T3 &value3, size_t startPosition=0, bool findAlsoHandled=true)
Reads three argument into variables.
T defaultRead(const std::string &key, const T &defaultValue, size_t startPosition=0, bool findAlsoHandled=true)
Reads an argument into a variable.
bool read1(const std::string &key, T &value, size_t startPosition=0, bool findAlsoHandled=true)
Looks for an argument, and reads it into a variable if found.
agx::ref_ptr< ArgumentParser > ArgumentParserRef
#define AGXCORE_EXPORT
bool read2(const std::string &key, T1 &value1, T2 &value2, size_t startPosition=0, bool findAlsoHandled=true)
Reads two argument into variables.