echelon  0.8.0
hdf5/attribute_repository.hpp
1 // Copyright (c) 2012-2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_HDF5_ATTRIBUTE_REPOSITORY_HPP
7 #define ECHELON_HDF5_ATTRIBUTE_REPOSITORY_HPP
8 
9 #include <echelon/hdf5/type.hpp>
10 #include <echelon/hdf5/type_factory.hpp>
11 #include <echelon/hdf5/attribute.hpp>
12 
13 #include <echelon/hdf5/broken_contract_exception.hpp>
14 
15 #include <memory>
16 #include <map>
17 #include <string>
18 #include <exception>
19 #include <utility>
20 
21 namespace echelon
22 {
23 namespace hdf5
24 {
25 class non_existing_attribute_exception : public std::exception
26 {
27 public:
28  non_existing_attribute_exception(std::string what_) : what_(std::move(what_))
29  {
30  }
31 
32  ~non_existing_attribute_exception() noexcept
33  {
34  }
35 
36  const char* what() const noexcept override
37  {
38  return what_.c_str();
39  }
40 
41 private:
42  std::string what_;
43 };
44 
51 template <typename Parent>
53 {
54 public:
55  explicit attribute_repository(Parent parent_) : parent_(std::move(parent_))
56  {
57  }
58 
66  attribute create(const std::string& name, const type& datatype)
67  {
68  return attribute(object(parent_), name, datatype);
69  }
70 
80  template <typename T>
81  attribute create(const std::string& name)
82  {
83  return create(name, get_hdf5_type<T>());
84  }
85 
96  template <typename T>
97  attribute create(const std::string& name, const T& value)
98  {
99  attribute attr = create<T>(name);
100 
101  attr <<= value;
102 
103  return attr;
104  }
105 
112  attribute operator[](const std::string& name) const
113  {
114  return attribute(object(parent_), name);
115  }
116 
123  bool exists(const std::string& name) const
124  {
125  return hdf5::precursor::is_attribute_existing(hdf5::precursor::object(parent_.id()), name);
126  }
127 
150  attribute require(const std::string& name, const type& datatype)
151  {
152  if (exists(name))
153  {
154  attribute attr(object(parent_), name);
155 
156  if (attr.datatype() != datatype)
157  throw broken_contract_exception("The required datatype doesn't "
158  "match the datatype of the attribute.");
159 
160  return attr;
161  }
162  else
163  {
164  return create(name, datatype);
165  }
166  }
167 
192  template <typename T>
193  attribute require(const std::string& name)
194  {
195  return require(name, get_hdf5_type<T>());
196  }
197 
224  template <typename T>
225  attribute require(const std::string& name, const T& value)
226  {
227  type datatype = get_hdf5_type<T>();
228 
229  if (exists(name))
230  {
231  attribute attr(object(parent_), name);
232 
233  if (attr.datatype() != datatype)
234  throw broken_contract_exception("The required datatype doesn't "
235  "match the datatype of the attribute.");
236 
237  return attr;
238  }
239  else
240  {
241  attribute attr = create(name, datatype);
242 
243  attr <<= value;
244 
245  return attr;
246  }
247  }
248 
249 private:
250  Parent parent_;
251 };
252 }
253 }
254 
255 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
A handle to an HDF5 attribute.
Definition: hdf5/attribute.hpp:24
attribute create(const std::string &name, const type &datatype)
Creates a new attribute.
Definition: hdf5/attribute_repository.hpp:66
attribute operator[](const std::string &name) const
Accessor function for this attribute repository.
Definition: hdf5/attribute_repository.hpp:112
attribute create(const std::string &name)
Creates a new attribute.
Definition: hdf5/attribute_repository.hpp:81
attribute require(const std::string &name, const T &value)
Returns the requested attribute, if it already exists, otherwise a new attribute is created...
Definition: hdf5/attribute_repository.hpp:225
type datatype() const
The value type of the attribute.
Definition: hdf5/attribute.cpp:29
attribute require(const std::string &name, const type &datatype)
Returns the requested attribute, if it already exists, otherwise a new attribute is created...
Definition: hdf5/attribute_repository.hpp:150
A handle to an HDF5 type.
Definition: hdf5/type.hpp:23
bool exists(const std::string &name) const
Tests, if an attribute exists.
Definition: hdf5/attribute_repository.hpp:123
Attribute manager, which should be embedded into a parent object, which supports attributes.
Definition: hdf5/attribute_repository.hpp:52
attribute require(const std::string &name)
Returns the requested attribute, if it already exists, otherwise the attribute is created...
Definition: hdf5/attribute_repository.hpp:193
attribute create(const std::string &name, const T &value)
Creates a new attribute and initializes it with a given value.
Definition: hdf5/attribute_repository.hpp:97
Exception, which is thrown, if a require_*-type method can&#39;t fulfill the contract.
Definition: broken_contract_exception.hpp:20