Skip to content

Commit

Permalink
moved STL datatype traits to contrib/stl
Browse files Browse the repository at this point in the history
Update #548
  • Loading branch information
eugenwintersberger committed Dec 24, 2021
1 parent 700f41c commit 84a8713
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 95 deletions.
1 change: 1 addition & 0 deletions src/h5cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ install(FILES ${HEADERS}

set(h5cpp_headers ${HEADERS})
set(h5cpp_sources )
add_subdirectory(contrib)
add_subdirectory(core)
add_subdirectory(attribute)
add_subdirectory(dataspace)
Expand Down
1 change: 1 addition & 0 deletions src/h5cpp/contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(stl)
Empty file.
27 changes: 27 additions & 0 deletions src/h5cpp/contrib/stl/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <h5cpp/datatype/type_trait.hpp>
#include <array>

namespace hdf5 {
namespace datatype {

template<typename T, size_t N>
class TypeTrait<std::array<T, N>> {
public:
using Type = std::array<T, N>;
using TypeClass = typename TypeTrait<T>::TypeClass;
static TypeClass create(const Type & = Type()) {
return TypeTrait<typename std::remove_const<T>::type>::create();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = TypeTrait<typename std::remove_const<T>::type>::create();
return cref_;
}
};
} // end of namespace datatype

namespace dataspace {

} // end of namespace dataspace
} // end of namespace hdf5
60 changes: 60 additions & 0 deletions src/h5cpp/contrib/stl/complex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// (c) Copyright 2021 DESY, ESS
// 2021 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
// ===========================================================================
//
// Authors: Eugen Wintersberger <[email protected]>
// Created on: Dec 23, 2021
//
#pragma once

#include <h5cpp/datatype/type_trait.hpp>
#include <complex>

namespace hdf5 {
namespace datatype {

template<typename T>
class TypeTrait<std::complex<T>>
{
private:
using element_type = TypeTrait<T>;

public:
using Type = std::complex<T>;
using TypeClass = Compound;

static TypeClass create(const Type & = Type())
{
datatype::Compound type = datatype::Compound::create(
sizeof(std::complex<T>));

type.insert("real", 0, element_type::create(T()));
type.insert("imag", alignof(T), element_type::create(T()));

return type;
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};
}
}
32 changes: 32 additions & 0 deletions src/h5cpp/contrib/stl/stl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// (c) Copyright 2017 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
// ===========================================================================
//
// Authors: Eugen Wintersberger <[email protected]>
// Sebastian Koenig <[email protected]>
// Jan Kotanski <[email protected]>
// Created on: Aug 28, 2017
//
#pragma once

#include <h5cpp/contrib/stl/array.hpp>
#include <h5cpp/contrib/stl/string.hpp>
#include <h5cpp/contrib/stl/vector.hpp>
#include <h5cpp/contrib/stl/complex.hpp>
51 changes: 51 additions & 0 deletions src/h5cpp/contrib/stl/string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <h5cpp/datatype/type_trait.hpp>
#include <string>

namespace hdf5 {
namespace datatype {

template<>
class TypeTrait<std::string> {
public:
using Type = std::string;
using TypeClass = String;
static TypeClass create(const Type & = Type()) {
return datatype::String::variable();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};


template<typename CharT>
class TypeTrait<std::basic_string<CharT>> {
private:

public:
using Type = std::basic_string<CharT>;
using TypeClass = String;

static TypeClass create(const Type & = Type()) {
static_assert(std::is_same<CharT, char>::value, "Only support 8Bit characters");

String type = String::variable();
type.encoding(CharacterEncoding::UTF8);
return type;

}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}

};
} // end of namespace datatype

namespace dataspace {

} // end of namespace dataspace
} // end of namespace hdf5
24 changes: 24 additions & 0 deletions src/h5cpp/contrib/stl/vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <vector>
#include <h5cpp/datatype/type_trait.hpp>

namespace hdf5 {
namespace datatype {

template<typename T>
class TypeTrait<std::vector<T>> {
public:
using Type = std::vector<T>;
using TypeClass = typename TypeTrait<T>::TypeClass;
static TypeClass create(const Type & = Type()) {
return TypeTrait<typename std::remove_const<T>::type>::create();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};

} // end of namespace datatype
} // end of namespace hdf5
95 changes: 0 additions & 95 deletions src/h5cpp/datatype/type_trait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
#include <h5cpp/datatype/string.hpp>
#include <h5cpp/datatype/compound.hpp>

#include <vector>
#include <array>
#include <string>
#include <complex>
#include <type_traits>

namespace hdf5 {
Expand Down Expand Up @@ -285,72 +281,6 @@ class TypeTrait<long double> {
return cref_;
}
};

template<>
class TypeTrait<std::string> {
public:
using Type = std::string;
using TypeClass = String;
static TypeClass create(const Type & = Type()) {
return datatype::String::variable();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};

template<typename T>
class TypeTrait<std::vector<T>> {
public:
using Type = std::vector<T>;
using TypeClass = typename TypeTrait<T>::TypeClass;
static TypeClass create(const Type & = Type()) {
return TypeTrait<typename std::remove_const<T>::type>::create();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};

template<typename T, size_t N>
class TypeTrait<std::array<T, N>> {
public:
using Type = std::array<T, N>;
using TypeClass = typename TypeTrait<T>::TypeClass;
static TypeClass create(const Type & = Type()) {
return TypeTrait<typename std::remove_const<T>::type>::create();
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = TypeTrait<typename std::remove_const<T>::type>::create();
return cref_;
}
};

template<typename CharT>
class TypeTrait<std::basic_string<CharT>> {
private:

public:
using Type = std::basic_string<CharT>;
using TypeClass = String;

static TypeClass create(const Type & = Type()) {
static_assert(std::is_same<CharT, char>::value, "Only support 8Bit characters");

String type = String::variable();
type.encoding(CharacterEncoding::UTF8);
return type;

}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}

};

template<>
class TypeTrait<bool> {
public:
Expand All @@ -366,31 +296,6 @@ class TypeTrait<bool> {
}
};

template<typename T>
class TypeTrait<std::complex<T>>
{
private:
using element_type = TypeTrait<T>;

public:
using Type = std::complex<T>;
using TypeClass = Compound;

static TypeClass create(const Type & = Type())
{
datatype::Compound type = datatype::Compound::create(
sizeof(std::complex<T>));

type.insert("real", 0, element_type::create(T()));
type.insert("imag", alignof(T), element_type::create(T()));

return type;
}
const static TypeClass & get(const Type & = Type()) {
const static TypeClass & cref_ = create();
return cref_;
}
};

} // namespace datatype
} // namespace hdf5

0 comments on commit 84a8713

Please sign in to comment.