"Fossies" - the Fresh Open Source Software Archive

Member "netcdf-cxx4-4.3.1/cxx4/ncType.cpp" (11 Sep 2019, 4175 Bytes) of package /linux/misc/netcdf-cxx4-4.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ncType.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <string>
    2 #include "ncType.h"
    3 #include "ncGroup.h"
    4 #include "ncCheck.h"
    5 using namespace std;
    6 
    7 extern int g_ncid;
    8 
    9 namespace netCDF {
   10   //  Global comparator operator ==============
   11   // comparator operator
   12   bool operator<(const NcType& lhs,const NcType& rhs)
   13   {
   14     return false;
   15   }
   16 
   17   // comparator operator
   18   bool operator>(const NcType& lhs,const NcType& rhs)
   19   {
   20     return true;
   21   }
   22 }
   23 
   24 using namespace netCDF;
   25 
   26 // assignment operator
   27 NcType& NcType::operator=(const NcType & rhs)
   28 {
   29   nullObject = rhs.nullObject;
   30   myId= rhs.myId;
   31   groupId = rhs.groupId;
   32   return *this;
   33 }
   34 
   35 // The copy constructor.
   36 NcType::NcType(const NcType& rhs):
   37   nullObject(rhs.nullObject),
   38   myId(rhs.myId),
   39   groupId(rhs.groupId)
   40 {}
   41 
   42 
   43 // Constructor generates a null object.
   44 NcType::NcType() :
   45   nullObject(true)
   46 {}
   47 
   48 // constructor
   49 NcType::NcType(const NcGroup& grp, const string& name) :
   50   nullObject (false)
   51 {
   52   groupId= grp.getId();
   53   NcType typTmp(grp.getType(name,NcGroup::ParentsAndCurrent));
   54   myId = typTmp.getId();
   55 }
   56 
   57 // constructor for a global type
   58 NcType::NcType(nc_type id) :
   59   nullObject(false),
   60   myId(id),
   61   groupId(0)
   62 {
   63 }
   64 
   65 // Constructor for a non-global type
   66 NcType::NcType(const netCDF::NcGroup& grp, nc_type id):
   67   nullObject(false),
   68   myId(id),
   69   groupId(grp.getId())
   70 {}
   71 
   72 
   73 // equivalence operator
   74 bool NcType::operator==(const NcType & rhs) const
   75 {
   76   if(nullObject)
   77     return nullObject == rhs.nullObject;
   78   else
   79     return groupId == rhs.groupId && myId == rhs.myId;
   80 }
   81 
   82 //  !=  operator
   83 bool NcType::operator!=(const NcType & rhs) const
   84 {
   85   return !(*this == rhs);
   86 }
   87 
   88 // Gets parent group.
   89 NcGroup  NcType::getParentGroup() const {
   90   if(groupId == 0) return NcGroup(); else  return NcGroup(groupId);
   91 }
   92 
   93 // Returns the type name.
   94 string  NcType::getName() const{
   95   char charName[NC_MAX_NAME+1];
   96   size_t *sizep=NULL;
   97 
   98   /* We cannot call nc_inq_type without a valid
   99      netcdf file ID (ncid), which is not *groupid*.
  100      Working around this for now. */
  101 
  102   ncCheck(nc_inq_type(g_ncid,myId,charName,sizep),__FILE__,__LINE__);
  103   return string(charName);
  104 
  105 };
  106 
  107 // Returns the size in bytes
  108 size_t NcType::getSize() const{
  109   char* charName=NULL;
  110   size_t sizep;
  111   ncCheck(nc_inq_type(g_ncid,myId,charName,&sizep),__FILE__,__LINE__);
  112   return sizep;
  113 };
  114 
  115 // The type class returned as an enumeration type.
  116 NcType::ncType NcType::getTypeClass() const{
  117   switch (myId) {
  118   case NC_BYTE    : return nc_BYTE;
  119   case NC_UBYTE   : return nc_UBYTE;
  120   case NC_CHAR    : return nc_CHAR;
  121   case NC_SHORT   : return nc_SHORT;
  122   case NC_USHORT  : return nc_USHORT;
  123   case NC_INT     : return nc_INT;
  124   case NC_UINT    : return nc_UINT;
  125   case NC_INT64   : return nc_INT64;
  126   case NC_UINT64  : return nc_UINT64;
  127   case NC_FLOAT   : return nc_FLOAT;
  128   case NC_DOUBLE  : return nc_DOUBLE;
  129   case NC_STRING  : return nc_STRING;
  130   default:
  131     // this is a user defined type
  132     // establish its type class, ie whether it is: NC_VLEN, NC_OPAQUE, NC_ENUM, or NC_COMPOUND.
  133     char* name=NULL;
  134     size_t* sizep=NULL;
  135     nc_type* base_nc_typep=NULL;
  136     size_t* nfieldsp=NULL;
  137     int classp;
  138     ncCheck(nc_inq_user_type(groupId,myId,name,sizep,base_nc_typep,nfieldsp,&classp),__FILE__,__LINE__);
  139     return static_cast<ncType>(classp);
  140   }
  141 }
  142 
  143 // The type class returned as a string.
  144 string NcType::getTypeClassName() const{
  145   ncType typeClass=getTypeClass();
  146   switch (typeClass) {
  147   case nc_BYTE    : return string("nc_BYTE");
  148   case nc_UBYTE   : return string("nc_UBYTE");
  149   case nc_CHAR    : return string("nc_CHAR");
  150   case nc_SHORT   : return string("nc_SHORT");
  151   case nc_USHORT  : return string("nc_USHORT");
  152   case nc_INT     : return string("nc_INT");
  153   case nc_UINT    : return string("nc_UINT");
  154   case nc_INT64   : return string("nc_INT64");
  155   case nc_UINT64  : return string("nc_UINT64");
  156   case nc_FLOAT   : return string("nc_FLOAT");
  157   case nc_DOUBLE  : return string("nc_DOUBLE");
  158   case nc_STRING  : return string("nc_STRING");
  159   case nc_VLEN    : return string("nc_VLEN");
  160   case nc_OPAQUE  : return string("nc_OPAQUE");
  161   case nc_ENUM    : return string("nc_ENUM");
  162   case nc_COMPOUND: return string("nc_COMPOUND");
  163   }
  164   // we never get here!
  165   return "Dummy";
  166 }