Skip to content

Commit

Permalink
Added the first version of a memory adapter
Browse files Browse the repository at this point in the history
Update #414
  • Loading branch information
eugenwintersberger committed Sep 1, 2019
1 parent bec22e3 commit 6240cb4
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/h5cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
3 changes: 3 additions & 0 deletions src/h5cpp/hdf5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@

#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>
#include <h5cpp/node/group.hpp>
Expand Down
14 changes: 14 additions & 0 deletions src/h5cpp/memory/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}/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)
60 changes: 60 additions & 0 deletions src/h5cpp/memory/integer_adapter.hpp
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)));
// }
//};


}
}
113 changes: 113 additions & 0 deletions src/h5cpp/memory/memory_adapter.hpp
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
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/memory/CMakeLists.txt
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)
83 changes: 83 additions & 0 deletions test/memory/integer_adapter_test.cpp
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());
}

0 comments on commit 6240cb4

Please sign in to comment.