Skip to content

Commit

Permalink
Moved the complex trait class to the STL directory
Browse files Browse the repository at this point in the history
Udpate #414
  • Loading branch information
eugenwintersberger committed Sep 1, 2019
1 parent 6240cb4 commit 13dccb8
Show file tree
Hide file tree
Showing 16 changed files with 555 additions and 80 deletions.
4 changes: 3 additions & 1 deletion src/h5cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(dir ${CMAKE_CURRENT_SOURCE_DIR})

set(HEADERS ${dir}/hdf5.hpp)
set(HEADERS ${dir}/hdf5.hpp
${dir}/stl.hpp)
install(FILES ${HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/h5cpp)

Expand All @@ -18,6 +19,7 @@ add_subdirectory(property)
add_subdirectory(utilities)
add_subdirectory(type)
add_subdirectory(memory)
add_subdirectory(stl)

add_doxygen_source_deps(${h5cpp_headers})

Expand Down
96 changes: 77 additions & 19 deletions src/h5cpp/datatype/compound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
namespace hdf5 {
namespace datatype {

//!
//! \brief compound data type
//!
//! A compound data type which can be used for data elements of heterogeneous
//! type like C-structures or a C++ class.
//!
/**
* @brief compound data type
*
* A compound data type which can be used for data elements of heterogeneous
* type like C-structures or a C++ class.
*
*/
class DLL_EXPORT Compound : public Datatype {
public:
//!
Expand All @@ -55,38 +56,95 @@ class DLL_EXPORT Compound : public Datatype {
Compound(const Datatype &type);

//!
//! \brief constructor
//! \brief factory function
//!
static Compound create(size_t size);

//! This factory function create a new base instance for a compound data
//! type. Once this is created one still has to insert the fields
//! comprising the type.
//!
//! \brief return the datatype for index element
//! \param size the size of the total compound type in bits
//! \return new Compound instance
//!
static Compound create(size_t size);

/**
* @brief get the data type of a field
*
* @param index the index of the field
* @return new instance of the HDF5 data type
*/
Datatype operator[](size_t index) const;

//!
//! \brief return the datatype for element name
//!
/**
* @brief get the data type of a field
*
* @param name the name of the field
* @return new instance of the HDF5 data type
*/
Datatype operator[](const std::string &name) const;

//!
//! \brief return the index of a type element
//!
/**
* @brief index of a field
*
* @param name the name of the field for which to obtain the index
* @return field index
*/
size_t field_index(const std::string &name) const;

//!
//! \brief return the name of a field
//!
//! Return the name of a field determined by index.
//!
//! \param index the numeric index of the field
//! \return the name of the field
//!
std::string field_name(size_t index) const;

//!
//! \brief return offset of a field
//!
//! Returns the offset of a particular field determined by its name in bits.
//!
//! \param name the name of the field
//! \return the offset of the field in bits
//!
size_t field_offset(const std::string &name) const;

//!
//! \brief return offset of a field in
size_t field_offset(size_t index) const;

/**
* @brief get the type class for a field
*
* @param name the name of the field
* @return a new instance of a type class
*/
Class field_class(const std::string &name) const;

/**
* @brief get the type class for a field
*
* @param index the index of the field
* @return a new instance of a type class
*/
Class field_class(size_t index) const;

//!
//! \brief return the total number of elements of the type
//!
/**
* @brief the number of fields
*
* @return the number of fields comprising the compound type
*/
size_t number_of_fields() const;

/**
* @brief insert a new field into the type
*
* @param name the name of the new field
* @param offset the offset in bits of the field
* @param type the HDF5 data type of the field
*/
void insert(const std::string &name, size_t offset, const Datatype &type) const;

void pack() const;
Expand Down
23 changes: 12 additions & 11 deletions src/h5cpp/datatype/trait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ namespace datatype {
//!
//! \tparam T type for which to create a new HDF5 data type
template<typename T>
class Trait {
public:
//!
//! \brief subtype of Datatype which will be used
//!
using TypeClass = Datatype;
class Trait
{
public:
//!
//! \brief subtype of Datatype which will be used
//!
using TypeClass = Datatype;

//!
//! \brief create the new type instance
//!
static TypeClass create(const T & = T());
};
//!
//! \brief create the new type instance
//!
static TypeClass create (const T& = T ());
};

template<>
class Trait<char> {
Expand Down
1 change: 0 additions & 1 deletion src/h5cpp/hdf5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
#include <h5cpp/type/trait.hpp>

#include <h5cpp/memory/memory_adapter.hpp>
#include <h5cpp/memory/integer_adapter.hpp>

#include <h5cpp/node/dataset.hpp>
#include <h5cpp/node/group_view.hpp>
Expand Down
1 change: 0 additions & 1 deletion src/h5cpp/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ set(SOURCES )

set(HEADERS
${dir}/memory_adapter.hpp
${dir}/integer_adapter.hpp
)

install(FILES ${HEADERS}
Expand Down
23 changes: 13 additions & 10 deletions src/h5cpp/memory/memory_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ class MemoryAdapter
private:
T &_reference;
public:
using BaseType = typename std::remove_cv<T>::type;
using BaseType = T;
using DataspaceType = typename dataspace::TypeTrait<BaseType>::DataspaceType;
using DatatypeType = typename datatype::TypeTrait<BaseType>::TypeClass;
using VoidPtrType = typename VoidType<std::is_const<T>::value>::type*;

explicit MemoryAdapter(T &instance):
_reference(instance)
Expand All @@ -71,15 +70,15 @@ class MemoryAdapter
MemoryAdapter(const MemoryAdapter<T> &) = delete;
MemoryAdapter(MemoryAdapter<T> &&) = default;

VoidPtrType pointer()
void* pointer()
{
return reinterpret_cast<VoidPtrType>(&_reference);
return reinterpret_cast<void*>(&_reference);
}

// VoidPtrType pointer() const
// {
// return reinterpret_cast<VoidPtrType>(&_reference);
// }
const void* pointer() const
{
return reinterpret_cast<const void*>(&_reference);
}


DataspaceType dataspace() const
Expand All @@ -103,11 +102,15 @@ class MemoryAdapter


template<typename T>
MemoryAdapter<T> make_adapter(T &instance)
MemoryAdapter<typename std::remove_const<T>::type> make_adapter(T &instance)
{
return MemoryAdapter<T>(instance);
using AdapterType = typename std::remove_const<T>::type;
return MemoryAdapter<AdapterType>(const_cast<AdapterType&>(instance));
}





} // end of namespace memory
} // end of namespace hdf5
40 changes: 5 additions & 35 deletions src/h5cpp/memory/integer_adapter.hpp → src/h5cpp/stl.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// (c) Copyright 2019 DESY,ESS
// (c) Copyright 2019 Eugen Wintersberger <[email protected]>
//
// This file is part of h5cpp.
//
Expand All @@ -19,42 +19,12 @@
// Boston, MA 02110-1301 USA
// ===========================================================================
//
// Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
// Author: Eugen Wintersberger <eugen.wintersberger@gmail.com>
// Created on: Sep 1, 2019
//
#pragma once

#include <h5cpp/memory/memory_adapter.hpp>

namespace hdf5 {
namespace memory {


//template<>
//class MemoryAdapter<int>
//{
// public:
// using DataspaceType = dataspace::Scalar;
// using DatatypeType = datatype::Integer;
//
//
// static int create(const datatype::Datatype & = datatype::Datatype(),
// const dataspace::Dataspace & = dataspace::Dataspace())
// {
// return int();
// }
//
// DataspaceType dataspace() const
// {
// return dataspace::Scalar();
// }
//
// DatatypeType datatype() const
// {
// return datatype::Integer(ObjectHandle(H5Tcopy(H5T_NATIVE_INT32)));
// }
//};


}
}
#include <h5cpp/stl/vector_memory_adapter.hpp>
#include <h5cpp/stl/complex_memory_adapter.hpp>
#include <h5cpp/stl/complex_datatype_trait.hpp>
14 changes: 14 additions & 0 deletions src/h5cpp/stl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(dir ${CMAKE_CURRENT_SOURCE_DIR})

set(SOURCES )

set(HEADERS
${dir}/vector_memory_adapter.hpp
${dir}/complex_memory_adapter.hpp
)

install(FILES ${HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/h5cpp/stl)

set(h5cpp_headers ${h5cpp_headers} ${HEADERS} PARENT_SCOPE)
set(h5cpp_sources ${h5cpp_sources} ${SOURCES} PARENT_SCOPE)
79 changes: 79 additions & 0 deletions src/h5cpp/stl/complex_datatype_trait.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// (c) Copyright 2019 Eugen Wintersberger <[email protected]>
//
// This file is part of h5cpp.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
// Boston, MA 02110-1301 USA
// ===========================================================================
//
// Author: Eugen Wintersberger <[email protected]>
// Created on: Sep 1, 2019
//
#pragma once

#include <h5cpp/hdf5.hpp>
#include <complex>

namespace hdf5 {
namespace datatype {


/**
* @brief datatype trait for complex numbers
*
* This trait creates a HDF5 datatype to store complex numbers. Complex
* numbers are stored in a compound data type with to fields of name
*
* - real - storing the real part
* - imag - storing the imaginary part
*
* \tparam T the base type for the C++ complex number type
*/
template<typename T>
class Trait<std::complex<T>>
{
private:
/**
* internal data structure equivalent of a complex number class
* - used to determine the memory layout.
*/
struct complex_struct {
T real;
T imag;
};
public:
using Type = std::complex<T>;
using TypeClass = Compound;

/**
* @brief create a compound type for complex numbers
*
* @return a compound data type for complex numbers
*/
static TypeClass create(const Type& = Type())
{
auto base_type= Trait<T>::create();
auto complex = Compound::create(sizeof(complex_struct));
complex.insert("real", HOFFSET(complex_struct, real), base_type);
complex.insert("imag", HOFFSET(complex_struct, imag), base_type);
return complex;
}

};


} // end of namespace datatype
} // end of namespace hdf5
Loading

0 comments on commit 13dccb8

Please sign in to comment.