AAO DRAMA/DRAMA2 C++ Interface
gitarg.hh
Go to the documentation of this file.
1 #ifndef DRAMA2_GITARG_INC
2 #define DRAMA2_GITARG_INC
3 
20 /*
21  * History:
22  04-Jul-2014 - TJF - Original version
23 
24  * The above ID is for Doxygen, this one has the format ACMM is looking for.
25  * "@(#) $Id: ACMM:Drama2/drama/gitarg.hh,v 1.45+ 22-Feb-2016 14:09:57+11 tjf $"
26  */
27 
28 #include "drama.hh"
29 #include "Git.h"
30 #include <string>
31 #include <limits.h>
32 
33 namespace drama {
40  namespace gitarg {
41 
52  enum class Flags {
53  NoFlagSet=0,
54  Upper=GIT_M_ARG_UPPER,
55  Lower=GIT_M_ARG_UPPER,
56  KeepErr=GIT_M_ARG_KEEPERR,
57  Abbrev = GIT_M_ARG_ABBREV,
58  LastBit=GIT_M_ARG_LASTBIT
63  };
70  constexpr Flags operator|(Flags f1, Flags f2) {
71  return Flags(int(f1)|int(f2));
72  }
79  inline bool IsFlagSet(const Flags f1, const Flags f2) {
80  if ((int)(f1) & (int)(f2))
81  return true;
82  else
83  return false;
84  }
85 
86 
87 
103  class EnumLookupClass {
104  public:
113  virtual unsigned int GetMaxValue() const = 0;
129  virtual const char ** GetStringArray() const = 0;
132  virtual ~EnumLookupClass() {}
133  };
134 
162  template <typename LookupClass, typename EnumType> class Enum {
163  private:
164  EnumType _value; // The underlying value and the only memory used by the class.
165  LookupClass lookupObject; // allows us to invoke the methods of this class.
166 
167  /*
168  * Set the value of the object from an int. If out of range,
169  * set to GetMaxValue + 1 (the invalid value).
170  */
171  void SetValue(const unsigned int i) {
172  if (i > lookupObject.GetMaxValue())
173  _value = InvalidValue();
174  else
175  _value = (EnumType)i;
176  }
177  /*
178  * Simply return the invalid value converted to int.
179  */
180  EnumType InvalidValue() const {
181  return (EnumType)(lookupObject.GetMaxValue()+1);
182  }
183  protected:
184  public:
191  Enum() {
192  _value = InvalidValue();
193  }
200  Enum(EnumType InitialValue) : _value(InitialValue) {}
201 
223  Enum(const sds::Id& Id,
224  const std::string & Name="Argument1",
225  const int Position=1,
226  const std::string & Default="",
228  :_value(InvalidValue()) {
229  Get(Id, Name, Position, Default, flags);
230  }
231 
254  void Get(const sds::Id& Id,
255  const std::string & Name="Argument1",
256  const int Position=1,
257  const std::string & Default="",
259  char string[100];
260  int index;
261  const char *myDef;
262  if (Default == "")
263  myDef = 0;
264  else
265  myDef = Default.c_str();
266  StatusType status = STATUS__OK;
267  GitArgGetS((SdsIdType)Id,Name.c_str(),Position,
268  lookupObject.GetStringArray(),
269  myDef,
270  (int)(flags),sizeof(string),string,&index,&status);
271 
272  if (status != STATUS__OK)
273  {
274  DramaTHROW(status,
275  "Failed to fetch Enumerated Argument from SDS");
276  }
277 
278  SetValue(index);
279  };
282  operator EnumType() const {
283  return _value;
284  }
287  operator std::string() const {
288  return lookupObject.GetStringArray()[int(_value)];
289  }
292  virtual ~Enum() {};
293 
294  }; // Class Enum;
295 
296 
297 
322  template <long int MinVal,long int MaxVal, long int DefaultVal=0> class Real {
323  private:
324  // Return the range we need.
325  virtual const double * Range() {
326  static const double range[] = { MinVal, MaxVal };
327  return range;
328  }
329  double _value;
330  public:
336  Real(const double def = (double)DefaultVal) : _value(def) {
337  }
361  Real ( /*Constructor with automatic Get */
362  const sds::Id& Id,
363  const std::string & Name="Argument1",
364  const int Position=1,
365  const double Default = (double)DefaultVal,
366  const Flags flags = Flags::KeepErr) : _value(Default) {
367 
368  Get(Id,Name,Position,Default,flags);
369  }
370  virtual ~Real() {}
371 
392  virtual void Get( /* Get value from Sds structure */
393  const sds::Id& Id,
394  const std::string & Name="Argument1",
395  const int Position=1,
396  const double Default = 0.0,
397  const Flags flags = Flags::KeepErr) {
398 
399  StatusType status = STATUS__OK;
400  GitArgGetD((SdsIdType)Id,Name.c_str(),
401  Position,Range(),Default,
402  (int)(flags),&_value,&status);
403 
404  if (status != STATUS__OK)
405  {
406  DramaTHROW(status, "Failed to fetch real number Argument from SDS");
407  }
408  }
411  operator double() const {
412  return (_value);
413  }
414  }; // Class Real.
415 
435  template <long int MinVal=LONG_MIN,
436  long int MaxVal=LONG_MAX,
437  long int DefaultVal=0> class Int {
438  private:
439  // Return the range we need.
440  virtual const long int * Range() {
441  static const long int range[] = { MinVal, MaxVal };
442  return range;
443  }
444  long _value;
445  public:
451  Int(const long def = DefaultVal) : _value(def) { }
452 
475  Int ( /*Constructor with automatic Get */
476  const sds::Id& Id,
477  const std::string & Name = "Argument1",
478  const int Position=1,
479  const long Default = DefaultVal,
480  const Flags flags = Flags::KeepErr) : _value(Default) {
481 
482  Get(Id,Name,Position,Default,flags);
483  }
484  virtual ~Int() {}
505  virtual void Get( /* Get value from Sds structure */
506  const sds::Id& Id,
507  const std::string & Name="Argument1",
508  const int Position=1,
509  const long int Default = 0,
510  const Flags flags = Flags::KeepErr) {
511 
512  StatusType status = STATUS__OK;
513  GitArgGetI((SdsIdType)Id,Name.c_str(),Position,Range(),Default,
514  (int)(flags),&_value,&status);
515 
516  if (status != STATUS__OK)
517  {
518  DramaTHROW(status, "Failed to fetch integer Argument from SDS");
519  }
520 
521  }
523  operator long int() const {
524  return (_value);
525  }
526 
527 
528  }; // class Int.
529 
543  class String : public std::string {
544  using std::string::string; // Inherit constructors.
545  private:
546  // Enough space for conversion of doubles/long ints.
547  static const unsigned _DefaultStringLength = 100;
548 
549  void RawGet (const sds::Id& Id, const std::string &Name,
550  int Position, const std::string &Default,
551  Flags flags, StatusType * status);
552 
553  public:
575  String( const sds::Id& Id,
576  const std::string &Name = "Argument1",
577  const int Position = 1,
578  const std::string &Default = "",
579  const Flags flags = Flags::KeepErr) {
580  this->Get(Id, Name, Position, Default, flags);
581  }
582 
583 
604  /*
605  * Implementation of this method is in gitarg.cpp.
606  */
607  virtual void Get (const sds::Id& Id,
608  const std::string &Name = "Argument1",
609  const int Position = 1,
610  const std::string &Default = "",
611  const Flags flags = Flags::KeepErr);
612 
613 
618  String operator=(const std::string str) {
619  this->std::string::operator=(str);
620  return *this;
621  }
626  String operator=(const String str) {
627  this->std::string::operator=(str);
628  return *this;
629  }
634  String operator=(const char *s) {
635  this->std::string::operator=(s);
636  return *this;
637  }
644  String& operator=(char c) {
645  this->std::string::operator=(c);
646  return *this;
647  }
648 
649 
650  }; /* String */
651 
670  class Bool {
671  private:
672  bool _value;
673  static const GitLogStrType lookupTable[];
674  protected:
686  virtual const GitLogStrType * Lookup() {
687  return lookupTable;
688  }
689  public:
693  Bool(bool initVal=false) : _value(initVal) { }
694 
716  Bool ( /*Constructor with automatic Get */
717  const sds::Id& Id,
718  const std::string &Name="Argument1",
719  const int Position=1,
720  const bool Default = false,
721  const Flags flags = (Flags::Upper|Flags::Abbrev|Flags::KeepErr)) : _value(false)
722  {
723  Get(Id,Name,Position,Default,flags);
724  }
727  virtual ~Bool() {}; /* Destructor for inheritors */
747  virtual void Get( /* Get value from Sds structure */
748  const sds::Id& Id,
749  const std::string &Name="Argument1",
750  const int Position=1,
751  const bool Default = false,
753 
754  long int actValue = 0;
755  StatusType status = STATUS__OK;
756  GitArgGetL((SdsIdType)Id,Name.c_str(),Position,Lookup(),Default,
757  (int)(flags),&actValue,&status);
758 
759  if (status != STATUS__OK)
760  {
761  DramaTHROW(status,
762  "Failed to fetch boolean Argument from SDS");
763  }
764 
765  _value = actValue ? true : false;
766  };
769  operator bool() const {
770  return _value;
771  };
776  Bool operator=(const bool src) {
777  _value = src;
778  return *this;
779  }
780 
781 
782  }; // class Bool
783 
784 
797  class Id : public sds::Id {
798  private:
799  bool _readFromFile = false; // Was this read from a file.
800  bool _defaultUsed = false; // Was this read from the default.
801  std::string _actualName; // Actual name of file or structure.
802 
803  void UseDefaultIfReq(const std::string &name,
804  int position,
805  const std::string &fileName,
806  Flags flags,
807  StatusType status,
808  const std::string &mess);
809 
810  public:
815  Id() : sds::Id() { }
816 
817 
866  Id(
867  const sds::Id& Id,
868  const std::string &Name="Argument1",
869  const int Position=1,
870  const std::string Default = "",
871  const Flags flags = Flags::KeepErr)
872  {
873  Get(Id,Name,Position,Default,flags);
874  }
877  virtual ~Id();
878 
925  virtual void Get( /* Get value from Sds structure */
926  const sds::Id& Id,
927  const std::string &Name="Argument1",
928  const int Position=1,
929  const std::string Default = "",
930  const Flags flags = (Flags::KeepErr));
931 
932 
941  virtual std::string ActualName() const;
946  virtual bool WasReadFromFile() const;
947 
953  virtual bool WasReadFromDefault() const;
954 
955 
956  }; // class Id
957 
958 
959 
960 
961 
962  } // namespace gitarg
963 
964 } // namespace drama
965 
966 #endif /* define DRAMA2_GITARG_INC */
Convert strings to lower case.
virtual ~Enum()
Destructor.
Definition: gitarg.hh:319
Bool operator=(const bool src)
Assign a bool to this gitarg::Bool item.
Definition: gitarg.hh:803
virtual ~EnumLookupClass()
Destructor.
Definition: gitarg.hh:159
Int(const long def=DefaultVal)
Simple constructor with a default value specified.
Definition: gitarg.hh:478
Allow abbreviations.
constexpr Flags operator|(Flags f1, Flags f2)
Operator to allow Flags values to be or-ed.
Definition: gitarg.hh:97
System default type.
String operator=(const std::string str)
Assign a std::string to this gitarg::String.
Definition: gitarg.hh:645
virtual bool WasReadFromFile() const
Indicates if the structure was read from a file.
virtual void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const std::string Default="", const Flags flags=(Flags::KeepErr))
Get the value of the object from an SDS structure or file.
Real(const double def=(double) DefaultVal)
Simple constructor with a default value specified.
Definition: gitarg.hh:363
Convert strings to upper case.
A class which reads integer values from an SDS argument structure.
Definition: gitarg.hh:464
A C++ Interface to the handling SDS structures.
Definition: sds.hh:412
String(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const std::string &Default="", const Flags flags=Flags::KeepErr)
Constructor with the initial value set from an SDS structure.
Definition: gitarg.hh:602
virtual std::string ActualName() const
Return the actual name of the file or structure.
virtual const char ** GetStringArray() const =0
Return a pointer to an array of enum strings equivalents.
A class which reads real values from an SDS argument structure.
Definition: gitarg.hh:349
virtual ~Bool()
Destructor.
Definition: gitarg.hh:754
A class which reads an SDS structure from DRAMA SDS Command argument structures.
Definition: gitarg.hh:824
Bool(bool initVal=false)
Simple constructor with a default value of false.
Definition: gitarg.hh:720
bool IsFlagSet(const Flags f1, const Flags f2)
Operator to allow determination of if a Flag enum value set.
Definition: gitarg.hh:106
Value indicating no flags set.
virtual bool WasReadFromDefault() const
Indicates if the structure was read from the default.
virtual ~Id()
Destructor.
virtual void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const long int Default=0, const Flags flags=Flags::KeepErr)
Get the value of the object from an SDS structure.
Definition: gitarg.hh:532
virtual void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const bool Default=false, const Flags flags=(Flags::Upper|Flags::Abbrev|Flags::KeepErr))
Get the value of the object from an SDS struture.
Definition: gitarg.hh:774
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition: exception.hh:82
Keep error status on return.
A class which reads a string item from an SDS structure.
Definition: gitarg.hh:570
Only consider Least significant bit of integer values when treating integers as logical.
virtual const GitLogStrType * Lookup()
Return a pointer to an array of enum strings equivalents.
Definition: gitarg.hh:713
virtual unsigned int GetMaxValue() const =0
This function should return the maximum normal value of the enumerated value.
Id()
Default constructor.
Definition: gitarg.hh:842
Flags
The various flags used in GIT operations.
Definition: gitarg.hh:79
Enum()
Default contructor for the object.
Definition: gitarg.hh:218
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition: drama.hh:89
virtual void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const std::string &Default="", const Flags flags=Flags::KeepErr)
Fetch the value of this item from an SDS structure.
DRAMA 2 main include file.
void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const std::string &Default="", const Flags flags=Flags::Upper|Flags::Abbrev|Flags::KeepErr)
Get the value of the object from an SDS structure.
Definition: gitarg.hh:281
virtual void Get(const sds::Id &Id, const std::string &Name="Argument1", const int Position=1, const double Default=0.0, const Flags flags=Flags::KeepErr)
Get the value of the object from an SDS structure.
Definition: gitarg.hh:419

Click here for the DRAMA home page and here for the AAO home page.

For more information, contact tjf@aao.gov.au 

Generated on Mon Feb 22 2016 15:57:52 for AAO DRAMA/DRAMA2 C++ Interface by doxygen 1.8.10