2017-01-21 9 views
0

私は別のライブラリで生成されたhdf5ファイルを読み込むC++ライブラリを作成しています。C++ HDF5は複合データ型の1メンバーを抽出します

これらのhdf5ファイルには、さまざまな複合データ型を持つ多くの複合データセットが含まれています。各複合データ型をC++構造体に変換したいと思います。

文字列(可変長または固定サイズの文字配列)では、C++構造体でstd :: stringを使用します。

現在、中間C構造(char*またはchar[]変数を使用)を使用して、最終的なC++構造に変換します。しかし、これにより多くの定型コードが生成されます。 dは複合データセットをある

std::string name = extract<std::string>(d,"name"); 

:私はメンバーによるデータメンバーを抽出することができれば

、私のような何かを行うことができます。

可能ですか

+0

抽出するメンバーは常に文字列ですか?あるいは、彼らは多くの異なるデータ型を持っていますか? – bnaecker

+0

メンバーには種類が混在しています。 – Thomas

答えて

0

解決策が見つかりました。私はここに投稿します、多分誰かがそれを見つけるでしょう有用です。この考え方は、化合物全体が読み込まれるバッファを含むCompoundExtractorオブジェクトを作成することです。次に、テンプレート抽出法を用いて1つずつメンバーを抽出することができる。この段階では、適切な特殊化(ここでは報告されていません)により、文字列の適切な処理が可能になります。 よろしく、

struct MADNEX_VISIBILITY_EXPORT CompoundExtractor 
{ 
    /*! 
    * \brief constructor 
    * \param[in] d: data set 
    */ 
    CompoundExtractor(const DataSet&); 
    //! destructor 
    ~CompoundExtractor(); 
    /*! 
    * \return a member of the compound of the given type 
    * \param[in] n: member name 
    */ 
    template<typename T> 
    T extract(const std::string&) const; 
    /*! 
    * \return a member of the compound of the given type 
    * \param[in] n: member name 
    */ 
    template<typename T> 
    T extract(const char *) const; 
private: 
    //! the description of the compound data type 
    H5::CompType ctype; 
    //! an intermediate storage for the compound data type 
    std::vector<char> data; 
}; // end of CompoundExtractor 

template<typename T> 
T CompoundExtractor::extract(const char *n) const{ 
    const auto i = this->ctype.getMemberIndex(n); 
    const auto o = this->ctype.getMemberOffset(i); 
    return *(reinterpret_cast<const T *>(this->data.data()+o)); 
} // end of CompoundExtractor::extract 

template<typename T> 
T CompoundExtractor::extract(const std::string& n) const{ 
    const auto i = this->ctype.getMemberIndex(n); 
    const auto o = this->ctype.getMemberOffset(i); 
    return *(reinterpret_cast<const T *>(this->data.data()+o)); 
} // end of CompoundExtractor::extract 

CompoundExtractor::CompoundExtractor(const DataSet& d) 
{ 
    const auto dtype = d.getDataType(); 
    if(dtype.getClass()!=H5T_COMPOUND){ 
    throw(std::runtime_error("CompoundExtractor: invalid data set")); 
    } 
    this->ctype = H5::CompType(d); 
    this->data.resize(ctype.getSize()); 
    d.read(this->data.data(),ctype); 
} 

CompoundExtractor::~CompoundExtractor() = default; 
関連する問題