-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first version of a memory adapter
Update #414
- Loading branch information
1 parent
bec22e3
commit 6240cb4
Showing
8 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
// 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))); | ||
// } | ||
//}; | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// (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 <type_traits> | ||
#include <h5cpp/hdf5.hpp> | ||
|
||
namespace hdf5 { | ||
namespace memory { | ||
|
||
|
||
template<bool is_const_type> | ||
struct VoidType { | ||
using type = void; | ||
}; | ||
|
||
template<> struct VoidType<true> | ||
{ | ||
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<typename T> | ||
class MemoryAdapter | ||
{ | ||
private: | ||
T &_reference; | ||
public: | ||
using BaseType = typename std::remove_cv<T>::type; | ||
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) | ||
{} | ||
|
||
MemoryAdapter(const MemoryAdapter<T> &) = delete; | ||
MemoryAdapter(MemoryAdapter<T> &&) = default; | ||
|
||
VoidPtrType pointer() | ||
{ | ||
return reinterpret_cast<VoidPtrType>(&_reference); | ||
} | ||
|
||
// VoidPtrType pointer() const | ||
// { | ||
// return reinterpret_cast<VoidPtrType>(&_reference); | ||
// } | ||
|
||
|
||
DataspaceType dataspace() const | ||
{ | ||
return dataspace::TypeTrait<BaseType>::create(_reference); | ||
} | ||
|
||
DatatypeType datatype() const | ||
{ | ||
return datatype::TypeTrait<BaseType>::create(_reference); | ||
} | ||
|
||
|
||
static BaseType create(const datatype::Datatype& = datatype::Datatype(), | ||
const dataspace::Dataspace & = dataspace::Dataspace()) | ||
{ | ||
return BaseType(); | ||
} | ||
|
||
}; | ||
|
||
|
||
template<typename T> | ||
MemoryAdapter<T> make_adapter(T &instance) | ||
{ | ||
return MemoryAdapter<T>(instance); | ||
} | ||
|
||
|
||
} // end of namespace memory | ||
} // end of namespace hdf5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
set(dir ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
set(test_sources | ||
${test_sources} | ||
${dir}/integer_adapter_test.cpp | ||
PARENT_SCOPE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// (c) Copyright 2019 Eugen Wintersberger <[email protected]> | ||
// | ||
// 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 <[email protected]> | ||
// Created on: Sep 1, 2019 | ||
// | ||
|
||
#include <h5cpp/hdf5.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
using namespace hdf5; | ||
|
||
template<typename T> | ||
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<memory::MemoryAdapter<TypeParam>, | ||
decltype(adapter)>(); | ||
::testing::StaticAssertTypeEq<dataspace::Scalar, | ||
typename decltype(adapter)::DataspaceType>(); | ||
::testing::StaticAssertTypeEq<datatype::Integer, | ||
typename decltype(adapter)::DatatypeType>(); | ||
|
||
::testing::StaticAssertTypeEq<void*, decltype(adapter.pointer())>(); | ||
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<datatype::Integer, decltype(type)>(); | ||
::testing::StaticAssertTypeEq<dataspace::Scalar, decltype(space)>(); | ||
} | ||
|
||
TYPED_TEST(IntegerMemoryAdapterTest, test_construction_from_const) | ||
{ | ||
const TypeParam value = TypeParam(); | ||
auto adapter = memory::make_adapter(value); | ||
::testing::StaticAssertTypeEq<memory::MemoryAdapter<const TypeParam>, | ||
decltype(adapter)>(); | ||
::testing::StaticAssertTypeEq<dataspace::Scalar, | ||
typename decltype(adapter)::DataspaceType>(); | ||
::testing::StaticAssertTypeEq<datatype::Integer, | ||
typename decltype(adapter)::DatatypeType>(); | ||
::testing::StaticAssertTypeEq<const void*, decltype(adapter.pointer())>(); | ||
|
||
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()); | ||
} |