h5cpp  0.3.3
A modern C++ wrapper for the HDF5 C library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 // Created on: May 14, 2018
26 //
27 #pragma once
28 
31 #include <h5cpp/error/error.hpp>
32 #include <sstream>
33 
34 namespace hdf5
35 {
36 namespace datatype
37 {
38 
45 class DLL_EXPORT Enum : public Datatype
46 {
47  public:
51  Enum() = default;
52 
56  explicit Enum(ObjectHandle&& handle);
57 
61  explicit Enum(const Datatype& type);
62 
66  template<typename T>
67  static Enum create(const T&);
68 
72  template<typename T>
73  void insert(const std::string& name, const T& data) const;
74 
78  size_t number_of_values() const;
79 
83  std::string name(size_t index) const;
84 
88  template<typename T>
89  T value(size_t index) const;
90 
94  template<typename T>
95  T value(const std::string& name) const;
96 
97  // The following functions are low level
98 
102  template<typename T>
103  void insert_underlying(const std::string& name, const T& data) const;
104 
108  static Enum create_underlying(const Datatype& base_type);
109 
113  template<typename T>
114  void underlying_value(size_t index, T& data) const;
115 
119  template<typename T>
120  void underlying_value(const std::string& name, T& data) const;
121 
122  private:
123  template<typename T>
124  void check_type(const T& data) const;
125 
126 };
127 
128 template<typename T>
129 void Enum::check_type(const T& data) const
130 {
131  (void) data; // < var unused, only for type inference
132  auto mem_type = datatype::create<T>();
133  if (mem_type != super())
134  {
135  std::stringstream ss;
136  ss << "Attempt to insert enum value of mismatching type";
137  throw (std::runtime_error(ss.str()));
138  }
139 }
140 
141 template<typename T>
143 {
144  return create_underlying(datatype::create<typename std::underlying_type<T>::type>());
145 }
146 
147 
148 template<typename T>
149 void Enum::insert_underlying(const std::string& name, const T& data) const
150 {
151  check_type(data);
152 
153  if (0 > H5Tenum_insert(static_cast<hid_t>(*this), name.c_str(), &data))
154  {
155  std::stringstream ss;
156  ss << "Could not insert Enum value " << name << "=" << data;
158  }
159 }
160 
161 template<typename T>
162 void Enum::insert(const std::string& name, const T& data) const
163 {
164  auto data2 = static_cast<typename std::underlying_type<T>::type>(data);
165  insert_underlying(name, data2);
166 }
167 
168 template<typename T>
169 void Enum::underlying_value(size_t index, T& data) const
170 {
171  check_type(data);
172 
173  if (0 > H5Tget_member_value(static_cast<hid_t>(*this), static_cast<uint32_t>(index), &data))
174  {
175  std::stringstream ss;
176  ss << "Could not retrieve Enum value at idx=" << index;
178  }
179 }
180 
181 template<typename T>
182 void Enum::underlying_value(const std::string& name, T& data) const
183 {
184  check_type(data);
185 
186  if (0 > H5Tenum_valueof(static_cast<hid_t>(*this), name.c_str(), &data))
187  {
188  std::stringstream ss;
189  ss << "Could not retrieve Enum value with name=" << name;
191  }
192 }
193 
194 template<typename T>
195 T Enum::value(size_t index) const
196 {
197  typename std::underlying_type<T>::type t;
198  underlying_value(index, t);
199  return static_cast<T>(t);
200 }
201 
202 template<typename T>
203 T Enum::value(const std::string& name) const
204 {
205  typename std::underlying_type<T>::type t;
206  underlying_value(name, t);
207  return static_cast<T>(t);
208 }
209 
210 
211 } // namespace datatype
212 } // namespace hdf5
static Singleton & instance()
reference to singleton
Definition: error.hpp:50
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
Wrapper for hid_t object identifiers.
Definition: object_handle.hpp:66
T value(size_t index) const
get value at index (with underlying type deduction)
Definition: enum.hpp:195
compound data type
Definition: enum.hpp:45
void insert_underlying(const std::string &name, const T &data) const
insert enum value definition
Definition: enum.hpp:149
Definition: attribute.hpp:43
void insert(const std::string &name, const T &data) const
insert enum value definition (with underlying type deduction)
Definition: enum.hpp:162
TypeTrait< typename std::remove_const< T >::type >::TypeClass create(const T &v=T())
factory function for creating data types
Definition: factory.hpp:38
#define DLL_EXPORT
Definition: windows.hpp:35
static Enum create(const T &)
named constructor (with underlying type deduction)
Definition: enum.hpp:142
base class for all data types
Definition: datatype.hpp:41
void underlying_value(size_t index, T &data) const
get value at index
Definition: enum.hpp:169