h5cpp 0.7
A modern C++ wrapper for the HDF5 C library
Loading...
Searching...
No Matches
enum.hpp
Go to the documentation of this file.
1//
2// (c) Copyright 2017 DESY,ESS
3//
4// This file is part of h5cpp.
5//
6// This library is free software; you can redistribute it and/or modify it
7// under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation; either version 2.1 of the License, or
9// (at your option) any later version.
10//
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14// License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with this library; if not, write to the
18// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19// Boston, MA 02110-1301 USA
20// ===========================================================================
21//
22// Authors:
23// Eugen Wintersberger <eugen.wintersberger@desy.de>
24// Martin Shetty <martin.shetty@esss.se>
25// Jan Kotanski <jan.kotanski@desy.de>
26// Created on: May 14, 2018
27//
28#pragma once
29
32#include <h5cpp/error/error.hpp>
33#include <sstream>
34
35namespace hdf5
36{
37namespace datatype
38{
39
46#ifdef __clang__
47#pragma clang diagnostic push
48#pragma clang diagnostic ignored "-Wweak-vtables"
49#endif
50class DLL_EXPORT Enum : public Datatype
51{
52 public:
56 Enum() = default;
57
61 explicit Enum(ObjectHandle&& handle);
62
66 explicit Enum(const Datatype& type);
67
71 template<typename T>
72 static Enum create(const T&);
73
77 template<typename T>
78 void insert(const std::string& name, const T& data) const;
79
83 size_t number_of_values() const;
84
88 std::string name(size_t index) const;
89
93 template<typename T>
94 T value(size_t index) const;
95
99 template<typename T>
100 T value(const std::string& name) const;
101
102 // The following functions are low level
103
107 template<typename T>
108 void insert_underlying(const std::string& name, const T& data) const;
109
113 static Enum create_underlying(const Datatype& base_type);
114
118 template<typename T>
119 void underlying_value(size_t index, T& data) const;
120
124 template<typename T>
125 void underlying_value(const std::string& name, T& data) const;
126
127 private:
128 template<typename T>
129 void check_type(const T& data) const;
130
131};
132#ifdef __clang__
133#pragma clang diagnostic pop
134#endif
135
136template<typename T>
137void Enum::check_type(const T& data) const
138{
139 (void) data; // < var unused, only for type inference
140 hdf5::datatype::DatatypeHolder mem_type_holder;
141 if (mem_type_holder.get<T>() != super())
142 {
143 std::stringstream ss;
144 ss << "Attempt to insert enum value of mismatching type";
145 throw (std::runtime_error(ss.str()));
146 }
147}
148
149template<typename T>
151{
152 return create_underlying(datatype::create<typename std::underlying_type<T>::type>());
153}
154
155
156template<typename T>
157void Enum::insert_underlying(const std::string& name, const T& data) const
158{
159 check_type(data);
160
161 if (0 > H5Tenum_insert(static_cast<hid_t>(*this), name.c_str(), &data))
162 {
163 std::stringstream ss;
164 ss << "Could not insert Enum value " << name << "=" << data;
166 }
167}
168
169template<typename T>
170void Enum::insert(const std::string& name, const T& data) const
171{
172 auto data2 = static_cast<typename std::underlying_type<T>::type>(data);
173 insert_underlying(name, data2);
174}
175
176template<typename T>
177void Enum::underlying_value(size_t index, T& data) const
178{
179 check_type(data);
180
181 if (0 > H5Tget_member_value(static_cast<hid_t>(*this), static_cast<uint32_t>(index), &data))
182 {
183 std::stringstream ss;
184 ss << "Could not retrieve Enum value at idx=" << index;
186 }
187}
188
189template<typename T>
190void Enum::underlying_value(const std::string& name, T& data) const
191{
192 check_type(data);
193
194 if (0 > H5Tenum_valueof(static_cast<hid_t>(*this), name.c_str(), &data))
195 {
196 std::stringstream ss;
197 ss << "Could not retrieve Enum value with name=" << name;
199 }
200}
201
202template<typename T>
203T Enum::value(size_t index) const
204{
205 typename std::underlying_type<T>::type t;
206 underlying_value(index, t);
207 return static_cast<T>(t);
208}
209
210template<typename T>
211T Enum::value(const std::string& name) const
212{
213 typename std::underlying_type<T>::type t;
215 return static_cast<T>(t);
216}
217
218
219} // namespace datatype
220} // namespace hdf5
Wrapper for hid_t object identifiers.
Definition object_handle.hpp:67
data type object holder
Definition factory.hpp:54
const Datatype & get(const T &v=T{})
factory holder method for getting reference of data types
Definition factory.hpp:74
base class for all data types
Definition datatype.hpp:42
Datatype super() const
get the base type
compound data type
Definition enum.hpp:51
Enum(const Datatype &type)
construct from generic datatype
std::string name(size_t index) const
get value name
T value(size_t index) const
get value at index (with underlying type deduction)
Definition enum.hpp:203
Enum()=default
default constructor
static Enum create_underlying(const Datatype &base_type)
named constructor from underlying datatype
static Enum create(const T &)
named constructor (with underlying type deduction)
Definition enum.hpp:150
size_t number_of_values() const
return the total number of enum values
void insert(const std::string &name, const T &data) const
insert enum value definition (with underlying type deduction)
Definition enum.hpp:170
void underlying_value(size_t index, T &data) const
get value at index
Definition enum.hpp:177
Enum(ObjectHandle &&handle)
construct from handle
void insert_underlying(const std::string &name, const T &data) const
insert enum value definition
Definition enum.hpp:157
static Singleton & instance()
reference to singleton
Definition error.hpp:59
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
TypeTrait< typenamestd::remove_const< T >::type >::TypeClass create(const T &v=T{})
factory function for creating data types
Definition factory.hpp:39
top-level namespace of the entire library
Definition attribute.hpp:45
#define DLL_EXPORT
Definition windows.hpp:29