Changeset 1596 for trunk/bindings


Ignore:
Timestamp:
30/06/11 14:54:13 (11 months ago)
Author:
Janik Zikovsky
Message:

Added methods to C++ API called getDataCoerce() to convert from differently-sized ints or doubles to a vector of doubles or a vector of ints. Added tests for those methods. Fixes #277.

Location:
trunk/bindings/cpp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/cpp/NeXusFile.cpp

    r1590 r1596  
    644644  this->getData(&(data[0])); 
    645645} 
     646 
     647 
     648void File::getDataCoerce(vector<int> &data) 
     649{ 
     650  Info info = this->getInfo(); 
     651  if (info.type == INT8) 
     652  { 
     653    vector<int8_t> result; 
     654    this->getData(result); 
     655    data.assign(result.begin(), result.end()); 
     656  } 
     657  else if (info.type == UINT8) 
     658  { 
     659    vector<uint8_t> result; 
     660    this->getData(result); 
     661    data.assign(result.begin(), result.end()); 
     662  } 
     663  else if (info.type == INT16) 
     664  { 
     665    vector<int16_t> result; 
     666    this->getData(result); 
     667    data.assign(result.begin(), result.end()); 
     668  } 
     669  else if (info.type == UINT16) 
     670  { 
     671    vector<uint16_t> result; 
     672    this->getData(result); 
     673    data.assign(result.begin(), result.end()); 
     674  } 
     675  else if (info.type == INT32) 
     676  { 
     677    vector<int32_t> result; 
     678    this->getData(result); 
     679    data.assign(result.begin(), result.end()); 
     680  } 
     681  else if (info.type == UINT32) 
     682  { 
     683    vector<uint32_t> result; 
     684    this->getData(result); 
     685    data.assign(result.begin(), result.end()); 
     686  } 
     687  else 
     688  { 
     689    throw Exception("NexusFile::getDataCoerce(): Could not coerce to int."); 
     690  } 
     691} 
     692 
     693void File::getDataCoerce(vector<double> &data) 
     694{ 
     695  Info info = this->getInfo(); 
     696  if (info.type == INT8) 
     697  { 
     698    vector<int8_t> result; 
     699    this->getData(result); 
     700    data.assign(result.begin(), result.end()); 
     701  } 
     702  else if (info.type == UINT8) 
     703  { 
     704    vector<uint8_t> result; 
     705    this->getData(result); 
     706    data.assign(result.begin(), result.end()); 
     707  } 
     708  else if (info.type == INT16) 
     709  { 
     710    vector<int16_t> result; 
     711    this->getData(result); 
     712    data.assign(result.begin(), result.end()); 
     713  } 
     714  else if (info.type == UINT16) 
     715  { 
     716    vector<uint16_t> result; 
     717    this->getData(result); 
     718    data.assign(result.begin(), result.end()); 
     719  } 
     720  else if (info.type == INT32) 
     721  { 
     722    vector<int32_t> result; 
     723    this->getData(result); 
     724    data.assign(result.begin(), result.end()); 
     725  } 
     726  else if (info.type == UINT32) 
     727  { 
     728    vector<uint32_t> result; 
     729    this->getData(result); 
     730    data.assign(result.begin(), result.end()); 
     731  } 
     732  else if (info.type == FLOAT32) 
     733  { 
     734    vector<float> result; 
     735    this->getData(result); 
     736    data.assign(result.begin(), result.end()); 
     737  } 
     738  else if (info.type == FLOAT64) 
     739  { 
     740    this->getData(data); 
     741  } 
     742  else 
     743  { 
     744    throw Exception("NexusFile::getDataCoerce(): Could not coerce to double."); 
     745  } 
     746} 
     747 
     748bool File::isDataInt() 
     749{ 
     750  Info info = this->getInfo(); 
     751  switch(info.type) 
     752  { 
     753  case INT8: 
     754  case UINT8: 
     755  case INT16: 
     756  case UINT16: 
     757  case INT32: 
     758  case UINT32: 
     759    return true; 
     760  default: 
     761    return false; 
     762  } 
     763} 
     764 
     765 
    646766 
    647767string File::getStrData() { 
  • trunk/bindings/cpp/NeXusFile.hpp

    r1590 r1596  
    494494    void getData(std::vector<NumT>& data); 
    495495 
     496    /** Get data and coerce into an int vector. 
     497     * 
     498     * @throw Exception if the data is actually a float or 
     499     *    another type that cannot be coerced to an int. 
     500     * @param data :: vector to be filled. 
     501     */ 
     502    void getDataCoerce(std::vector<int> &data); 
     503 
     504    /** Get data and coerce into a vector of doubles. 
     505     * 
     506     * @throw Exception if the data cannot be coerced to a double. 
     507     * @param data :: vector to be filled. 
     508     */ 
     509    void getDataCoerce(std::vector<double> &data); 
     510 
     511    /** Return true if the data opened is of one of the 
     512     * int data types, 32 bits or less. 
     513     */ 
     514    bool isDataInt(); 
     515 
    496516    /** 
    497517     * \return String data from the file. 
Note: See TracChangeset for help on using the changeset viewer.