diff --git a/src/h5cpp/CMakeLists.txt b/src/h5cpp/CMakeLists.txt index 3b107b5533..aa8781c5c2 100644 --- a/src/h5cpp/CMakeLists.txt +++ b/src/h5cpp/CMakeLists.txt @@ -17,6 +17,7 @@ add_subdirectory(node) add_subdirectory(property) add_subdirectory(utilities) add_subdirectory(type) +add_subdirectory(memory) add_doxygen_source_deps(${h5cpp_headers}) diff --git a/src/h5cpp/hdf5.hpp b/src/h5cpp/hdf5.hpp index ee1b05c9e9..da7faa2cb6 100644 --- a/src/h5cpp/hdf5.hpp +++ b/src/h5cpp/hdf5.hpp @@ -80,6 +80,9 @@ #include +#include +#include + #include #include #include diff --git a/src/h5cpp/memory/CMakeLists.txt b/src/h5cpp/memory/CMakeLists.txt new file mode 100644 index 0000000000..e00c4a61ad --- /dev/null +++ b/src/h5cpp/memory/CMakeLists.txt @@ -0,0 +1,14 @@ +set(dir ${CMAKE_CURRENT_SOURCE_DIR}) + +set(SOURCES ) + +set(HEADERS + ${dir}/memory_adapter.hpp + ${dir}/integer_adapter.hpp + ) + +install(FILES ${HEADERS} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/h5cpp/memory) + +set(h5cpp_headers ${h5cpp_headers} ${HEADERS} PARENT_SCOPE) +set(h5cpp_sources ${h5cpp_sources} ${SOURCES} PARENT_SCOPE) diff --git a/src/h5cpp/memory/integer_adapter.hpp b/src/h5cpp/memory/integer_adapter.hpp new file mode 100644 index 0000000000..8e0282df5f --- /dev/null +++ b/src/h5cpp/memory/integer_adapter.hpp @@ -0,0 +1,60 @@ +// +// (c) Copyright 2019 DESY,ESS +// +// 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 +// Created on: Sep 1, 2019 +// +#pragma once + +#include + +namespace hdf5 { +namespace memory { + + +//template<> +//class MemoryAdapter +//{ +// 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))); +// } +//}; + + +} +} diff --git a/src/h5cpp/memory/memory_adapter.hpp b/src/h5cpp/memory/memory_adapter.hpp new file mode 100644 index 0000000000..4319eca2fb --- /dev/null +++ b/src/h5cpp/memory/memory_adapter.hpp @@ -0,0 +1,113 @@ +// +// (c) Copyright 2019 Eugen Wintersberger +// +// 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 +// Created on: Sep 1, 2019 +// +#pragma once + +#include +#include + +namespace hdf5 { +namespace memory { + + +template +struct VoidType { + using type = void; +}; + +template<> struct VoidType +{ + using type = const void; +}; + + +//! \brief the basic memory adapter template +//! +//! Memory adapters are the corner stones of h5cpp's IO system. They provide +//! access to the memory occupied by an instance of a particular datatype. +//! Unlike the trait classes which have been there since the beginning +//! these templates allow to store state and thus could be used to provide +//! intermediate buffers required by some in-memory structures not compatible +//! with HDF5. +//! +//! \tparam T the type parameters +//! +template +class MemoryAdapter +{ + private: + T &_reference; + public: + using BaseType = typename std::remove_cv::type; + using DataspaceType = typename dataspace::TypeTrait::DataspaceType; + using DatatypeType = typename datatype::TypeTrait::TypeClass; + using VoidPtrType = typename VoidType::value>::type*; + + explicit MemoryAdapter(T &instance): + _reference(instance) + {} + + MemoryAdapter(const MemoryAdapter &) = delete; + MemoryAdapter(MemoryAdapter &&) = default; + + VoidPtrType pointer() + { + return reinterpret_cast(&_reference); + } + +// VoidPtrType pointer() const +// { +// return reinterpret_cast(&_reference); +// } + + + DataspaceType dataspace() const + { + return dataspace::TypeTrait::create(_reference); + } + + DatatypeType datatype() const + { + return datatype::TypeTrait::create(_reference); + } + + + static BaseType create(const datatype::Datatype& = datatype::Datatype(), + const dataspace::Dataspace & = dataspace::Dataspace()) + { + return BaseType(); + } + +}; + + +template +MemoryAdapter make_adapter(T &instance) +{ + return MemoryAdapter(instance); +} + + +} // end of namespace memory +} // end of namespace hdf5 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fcfd1f0566..f68cd69ba7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -40,6 +40,7 @@ add_subdirectory(node) add_subdirectory(file) add_subdirectory(utilities) add_subdirectory(type) +add_subdirectory(memory) add_executable(unit_tests EXCLUDE_FROM_ALL diff --git a/test/memory/CMakeLists.txt b/test/memory/CMakeLists.txt new file mode 100644 index 0000000000..bdaab0b86a --- /dev/null +++ b/test/memory/CMakeLists.txt @@ -0,0 +1,6 @@ +set(dir ${CMAKE_CURRENT_SOURCE_DIR}) + +set(test_sources + ${test_sources} + ${dir}/integer_adapter_test.cpp + PARENT_SCOPE) diff --git a/test/memory/integer_adapter_test.cpp b/test/memory/integer_adapter_test.cpp new file mode 100644 index 0000000000..3b90f02ec3 --- /dev/null +++ b/test/memory/integer_adapter_test.cpp @@ -0,0 +1,83 @@ +// +// (c) Copyright 2019 Eugen Wintersberger +// +// This file is part of h5pp. +// +// 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 +// Created on: Sep 1, 2019 +// + +#include +#include + +using namespace hdf5; + +template +class IntegerMemoryAdapterTest : public ::testing::Test { }; + +using test_types = ::testing::Types< + unsigned char, char, + unsigned short, short, + unsigned int, int, + unsigned long, long, + unsigned long long, long long>; + +TYPED_TEST_CASE(IntegerMemoryAdapterTest, test_types); + +TYPED_TEST(IntegerMemoryAdapterTest, test_construction_from_none_const) +{ + TypeParam value = TypeParam(); + auto adapter = memory::make_adapter(value); + ::testing::StaticAssertTypeEq, + decltype(adapter)>(); + ::testing::StaticAssertTypeEq(); + ::testing::StaticAssertTypeEq(); + + ::testing::StaticAssertTypeEq(); + ASSERT_EQ(&value, adapter.pointer()); + ASSERT_EQ(value, decltype(adapter)::create()); + + auto space = adapter.dataspace(); + auto type = adapter.datatype(); + ASSERT_EQ(sizeof(TypeParam), type.size()); + ::testing::StaticAssertTypeEq(); + ::testing::StaticAssertTypeEq(); +} + +TYPED_TEST(IntegerMemoryAdapterTest, test_construction_from_const) +{ + const TypeParam value = TypeParam(); + auto adapter = memory::make_adapter(value); + ::testing::StaticAssertTypeEq, + decltype(adapter)>(); + ::testing::StaticAssertTypeEq(); + ::testing::StaticAssertTypeEq(); + ::testing::StaticAssertTypeEq(); + + ASSERT_EQ(&value, adapter.pointer()); + ASSERT_EQ(value, decltype(adapter)::create()); + + auto space = adapter.dataspace(); + auto type = adapter.datatype(); + ASSERT_EQ(sizeof(TypeParam), type.size()); +}