AAO DRAMA/DRAMA2 C++ Interface
DRAMA C++11 and later interface
argtest.cpp
/*
* Program name:
argtest.cpp
* Function:
A program to test the drama::sds "Arg" interfaces
* Description:
Tests the various methods of drama::sds used to constructor and access
traditional DRAMA command line arguments - such as supported by the
C level "Arg" series of methods.
* Methods not yet tested.
* Language:
C++
* Support: Tony Farrell, AAO
* History:
29-Feb-2014 - TJF - Original version
* Copyright (c) Anglo-Australian Telescope Board, 2005
Not to be used for commercial purposes without AATB permission.
* @(#) $Id$
*/
static const char *rcsId="@(#) $Id$";
static void *use_rcsId = (0 ? (void *)(&use_rcsId) : (void *) &rcsId);
//#include "drama/util.hh"
//#include "drama/sds.hh"
#include "drama.hh"
#include "drama.h"
#include "mess.h"
#include "sds_err_msgt.h"
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <unistd.h>
static void Construct(drama::sds::Id *topLevel);
static void Examine(drama::sds::Id &topLevel);
int main(int , const char *[])
{
/*
* Allow us to translate error codes
*/
MessPutFacility(&MessFac_SDS);
try
{
drama::sds::Id topLevel;
/*
* Construct a structure and write it out to a file.
*/
Construct(&topLevel);
drama::SafePrintf(std::cerr,"Converted to string = \"%\"\n",
topLevel.toString());
topLevel.Write("argtest.sds");
topLevel.List();
/*
* Read the structure back in and examine it.
*/
drama::sds::Id topLevel2(drama::sds::Id::FromFile("argtest.sds"));
Examine(topLevel);
Examine(topLevel2);
topLevel2 = topLevel.Copy();
}
catch (const drama::Exception &e)
{
e.Print();
return 1;
}
/*
* Get rid of the file.
*/
unlink("argtest.sds");
return 0;
}
static void Construct(drama::sds::Id *topLevel)
{
/*
* Create a top level structure.
*/
/*
* Put a bunch of items (covering each "Put()" method).
*/
topLevel->Put("BoolItem", true);
topLevel->Put("SimpleIntItem", 2);
topLevel->Put("CharItem", (char)(3));
topLevel->Put("ShortItem", (short)(4));
topLevel->Put("UShortItem", (unsigned short)(5));
topLevel->Put("INT32Item", (INT32)(6));
topLevel->Put("UINT32Item", (UINT32)(7));
topLevel->Put("INT64Item", (INT64)(8));
topLevel->Put("UINT64Item", (UINT64)(9));
topLevel->Put("FloatItem", (float)(10));
topLevel->Put("DoubleItem", (double)(11));
topLevel->Put("StringItem1", "12");
std::string test13("13");
topLevel->Put("StringItem2", test13);
}
/*
* This template method will fetch an item from the SDS structure
* and compare it to an expected value, throwing an exception if
* the value it not correct.
*
* The template parameter (executeValue parameter type) should be
* one of the standard SDS types.
*/
template <typename T>
void CheckItem(
const std::string &name,
T expectedValue)
{
T actualValue;
top.Get(name, &actualValue);
if (actualValue != expectedValue)
{
std::cerr << "Failed to fetch correct value from item \""
<< name
<< "\""
<< std::endl
<< " Expected \""
<< expectedValue
<< "\" Got \""
<< actualValue
<< "\""
<< std::endl;
DramaTHROW(SDS__TESTERR, "Test error");
return;
}
}
static void Examine(drama::sds::Id &topLevel)
{
/*
* Check each of the expected values in the structure.
*/
CheckItem(topLevel, "BoolItem", true);
CheckItem(topLevel, "SimpleIntItem", 2);
CheckItem(topLevel, "CharItem", (char)(3));
CheckItem(topLevel, "ShortItem", (short)(4));
CheckItem(topLevel, "UShortItem", (unsigned short)(5));
CheckItem(topLevel, "INT32Item", (INT32)(6));
CheckItem(topLevel, "UINT32Item", (UINT32)(7));
CheckItem(topLevel, "INT64Item", (INT64)(8));
CheckItem(topLevel, "UINT64Item", (UINT64)(9));
CheckItem(topLevel, "FloatItem", (float)(10));
CheckItem(topLevel, "DoubleItem", (double)(11));
CheckItem(topLevel, "StringItem1", std::string("12"));
CheckItem(topLevel, "StringItem2", std::string("13"));
}
virtual void Print(FILE *file=stderr, bool include_stacktrace=false) const
Print exception details to a file.
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:164
static Id FromFile(const std::string &filename)
Factory constructor method that reads an SDS structure from a file.
virtual void Write(const std::string &filename) const
Write the contents of the structure to a file.
Definition sds.hh:2191
virtual void List() const
List the contents of the structure to standard output.
Definition sds.hh:2099
void Get(const unsigned long length, T *const data, unsigned long *actlen=nullptr, const unsigned long offset=0) const
Get data from an SDS item.
Definition sds.hh:1547
virtual Id Copy() const
Factory constructor method Id Copy constructor.
void Put(const unsigned long length, const T *const data, const unsigned long offset=0)
Put data into an SDS item.
Definition sds.hh:1942
static Id CreateArgStruct(const std::string &name="ArgStructure")
Factory constructor which creates a new "Arg" style SDS structure.
virtual std::string toString(int maxlen=200)
Convert the structure to a string.
Definition sds.hh:2918
A C++ Interface to the handling SDS structures.
Definition sds.hh:428
DRAMA 2 main include file.
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:93
void SafePrintf(std::ostream &ostream, const char *str)
Safe formatted write to a stream.