Skip to content

Commit

Permalink
Added some experimental code
Browse files Browse the repository at this point in the history
Update #414
  • Loading branch information
eugenwintersberger committed Oct 29, 2019
1 parent cb894f2 commit dc01d55
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/h5cpp/attribute/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <h5cpp/core/types.hpp>
#include <h5cpp/node/link.hpp>
#include <h5cpp/error/error.hpp>
#include <h5cpp/memory/memory_adapter.hpp>
#include <initializer_list>

namespace hdf5 {
Expand Down Expand Up @@ -257,6 +258,7 @@ class DLL_EXPORT Attribute
}
}


template<typename T>
void read_fixed_length_string(T &data,
const datatype::Datatype &mem_type) const
Expand Down Expand Up @@ -354,9 +356,16 @@ void Attribute::write(const T &data,const datatype::Datatype &mem_type) const
template<typename T>
void Attribute::write(const T &data) const
{
auto mem_type = datatype::create<T>(data);

write(data,mem_type);
//using AdapterType = memory::MemoryAdapter<T>;
const auto adapter = memory::make_adapter(data);
//auto mem_type = datatype::create<T>(data);
//const void *ptr = dataspace::cptr(data);
if(H5Awrite(static_cast<hid_t>(handle_),
static_cast<hid_t>(adapter.datatype()),
adapter.pointer())<0)
{
error::Singleton::instance().throw_with_stack("Failure to write data to attribute!");
}
}

template<typename T>
Expand Down
20 changes: 18 additions & 2 deletions src/h5cpp/memory/memory_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#pragma once

#include <type_traits>
#include <h5cpp/hdf5.hpp>
#include <h5cpp/dataspace/type_trait.hpp>
#include <h5cpp/datatype/trait.hpp>

namespace hdf5 {
namespace memory {
Expand Down Expand Up @@ -98,9 +99,24 @@ class MemoryAdapter
return BaseType();
}

};
/**
* @brief update the target data structure
*
* After every write operation on the buffer provided by the adapter,
* which means after every read operation from the view point of HDF5,
* the update method ensures that all data is copied to the original
* data structure.
*/
void update() {}

};

/**
* @brief construct a memory adapter from a given instance of arbitrary type
*
* @param instance reference to the instance for which to construct the adapter
* @return new instance of a memory adapter
*/
template<typename T>
MemoryAdapter<typename std::remove_const<T>::type> make_adapter(T &instance)
{
Expand Down
1 change: 1 addition & 0 deletions src/h5cpp/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
#include <h5cpp/stl/complex_memory_adapter.hpp>
#include <h5cpp/stl/complex_datatype_trait.hpp>
#include <h5cpp/stl/string_types.hpp>
#include <h5cpp/stl/variable_length_string_memory_adapter.hpp>
105 changes: 105 additions & 0 deletions src/h5cpp/stl/variable_length_string_memory_adapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// (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 8, 2019
//
#pragma once

#include <h5cpp/memory/memory_adapter.hpp>
#include <h5cpp/stl/string_types.hpp>

namespace hdf5 {
namespace memory {

//! \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<type::VariableUTF8String>
{
private:
type::VariableUTF8String &_reference;
std::vector<char*> _buffer;
public:
using InstanceType = type::VariableUTF8String;
using DataspaceType = typename dataspace::TypeTrait<InstanceType>::DataspaceType;
using DatatypeType = typename datatype::TypeTrait<InstanceType>::TypeClass;

explicit MemoryAdapter(InstanceType &instance):
_reference(instance),
_buffer(1)
{}

MemoryAdapter(const MemoryAdapter<type::VariableUTF8String> &) = delete;
MemoryAdapter(MemoryAdapter<type::VariableUTF8String> &&) = default;

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

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


DataspaceType dataspace() const
{
return dataspace::TypeTrait<InstanceType>::create(_reference);
}

DatatypeType datatype() const
{
return datatype::TypeTrait<InstanceType>::create(_reference);
}


static InstanceType create(const datatype::Datatype& = datatype::Datatype(),
const dataspace::Dataspace & = dataspace::Dataspace())
{
return InstanceType();
}

/**
* @brief update the target data structure
*
* After every write operation on the buffer provided by the adapter,
* which means after every read operation from the view point of HDF5,
* the update method ensures that all data is copied to the original
* data structure.
*/
void update() {}

};


} // end of namespace memory
} // end of namespace hdf5
1 change: 1 addition & 0 deletions test/attribute/attribute_multidim_io_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cstdint>
#include <vector>
#include <array>
#include <h5cpp/stl.hpp>

using namespace hdf5;

Expand Down
1 change: 1 addition & 0 deletions test/attribute/attribute_scalar_io_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cstdint>
#include <vector>
#include "../fixture.hpp"
#include <h5cpp/stl.hpp>

using namespace hdf5;

Expand Down
2 changes: 1 addition & 1 deletion test/attribute/attribute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <h5cpp/attribute/attribute.hpp>
#include <h5cpp/dataspace/type.hpp>
#include <h5cpp/datatype/types.hpp>

#include <h5cpp/stl.hpp>
#include "../fixture.hpp"

using namespace hdf5;
Expand Down

0 comments on commit dc01d55

Please sign in to comment.