RMF
ConstAttributes.h
Go to the documentation of this file.
1/**
2 * \file RMF/HDF5/ConstAttributes.h
3 * \brief Handle read/write of Model data from/to files.
4 *
5 * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef RMF_HDF5_CONST_ATTRIBUTES_H
10#define RMF_HDF5_CONST_ATTRIBUTES_H
11
12#include "RMF/config.h"
13#include "handle.h"
14#include "types.h"
16#include <hdf5.h>
17
18RMF_ENABLE_WARNINGS
19
20namespace RMF {
21namespace HDF5 {
22
23/** Wrap getting the attributes from an HDF5 Object. Each data set or
24 group can have associated data. These data should be relatively small
25 (a few k at most). See MutableAttributes for the methods to change
26 the attribute data.
27 */
28template <class Base>
29class ConstAttributes : public Base {
30#ifndef SWIG
31 protected:
32 ConstAttributes(std::shared_ptr<SharedHandle> h) : Base(h) {}
33 ConstAttributes() {}
34#else
35 private:
36 ConstAttributes();
37#endif
38 public:
39 /** \name Template attribute methods
40 When manipulating attributes from C++ you can use these
41 templated methods.
42 @{
43 */
44 template <class TypeTraits>
45 typename TypeTraits::Types get_attribute(std::string name) const {
46 if (!H5Aexists(Base::get_shared_handle()->get_hid(), name.c_str())) {
47 return typename TypeTraits::Types();
48 } else {
49 RMF_HDF5_HANDLE(a, H5Aopen(Base::get_shared_handle()->get_hid(),
50 name.c_str(), H5P_DEFAULT),
51 &H5Aclose);
52 RMF_HDF5_HANDLE(s, H5Aget_space(a), &H5Sclose);
53 hsize_t dim, maxdim;
54 RMF_HDF5_CALL(H5Sget_simple_extent_dims(s, &dim, &maxdim));
55 typename TypeTraits::Types ret =
56 TypeTraits::read_values_attribute(a, dim);
57 return ret;
58 }
59 }
60 /** @} */
61 template <class CT, class CF>
62 CT copy_to(const CF& cf) const {
63 return CT(cf.begin(), cf.end());
64 }
65 bool get_has_attribute(std::string nm) const {
66 return H5Aexists_by_name(Base::get_handle(), ".", nm.c_str(), H5P_DEFAULT) >
67 0;
68 }
69/** \name Nontemplated attributes
70 When using python, call the non-template versions of the
71 attribute manipulation methods.
72 @{
73 */
74#define RMF_HDF5_CONST_ATTRIBUTE(lcname, UCName, PassValue, ReturnValue, \
75 PassValues, ReturnValues) \
76 ReturnValues get_##lcname##_attribute(std::string nm) const { \
77 return get_attribute<UCName##Traits>(nm); \
78 }
79
80 RMF_FOREACH_SIMPLE_TYPE(RMF_HDF5_CONST_ATTRIBUTE);
81 RMF_HDF5_CONST_ATTRIBUTE(char, Char, char, char, std::string, std::string);
82 /** @} */
83};
84
85} /* namespace HDF5 */
86} /* namespace RMF */
87
88RMF_DISABLE_WARNINGS
89
90#endif /* RMF_HDF5_CONST_ATTRIBUTES_H */
Various general useful macros for IMP.
#define RMF_HDF5_CALL(v)
Handle read/write of Model data from/to files.
Handle read/write of Model data from/to files.
#define RMF_FOREACH_SIMPLE_TYPE(macroname)
The namespace for all HDF5 functionality in the library.
char Char
Definition HDF5/types.h:52
The namespace for all functionality in the library.