andres::graph
hdf5/complete-graph.hxx
1 #pragma once
2 #ifndef ANDRES_GRAPH_COMPLETE_GRAPH_HDF5_HXX
3 #define ANDRES_GRAPH_COMPLETE_GRAPH_HDF5_HXX
4 
5 #include <stdexcept>
6 #include <string>
7 
8 #include "hdf5.hxx"
9 #include "../complete-graph.hxx"
10 
11 namespace andres {
12 namespace graph {
13 namespace hdf5 {
14 
15 template<>
16 template<class VISITOR>
17 struct GraphTraitsHDF5<CompleteGraph<VISITOR> > {
18  static const int ID;
19 };
20 template<class VISITOR>
21  const int GraphTraitsHDF5<CompleteGraph<VISITOR> >::ID = 10002;
22 
23 template <class VISITOR>
24 void save(const hid_t, const std::string&, const CompleteGraph<VISITOR>& graph);
25 
26 template <class VISITOR>
27 void load(const hid_t, const std::string&, CompleteGraph<VISITOR>& graph);
28 
29 template <class VISITOR>
30 void
32  const hid_t parentHandle,
33  const std::string& graphName,
34  const CompleteGraph<VISITOR>& graph
35 ) {
37  hdf5::HandleCheck<ANDRES_GRAPH_HDF5_DEBUG> handleCheck;
38  hid_t groupHandle = openGroup(parentHandle, graphName, true);
39  std::string sError;
40  try {
41  save(groupHandle, "number-of-vertices", graph.numberOfVertices());
42  int ID = GraphTraitsHDF5<Graph>::ID;
43  save(groupHandle, "graph-type-id", ID);
44  } catch (std::exception& e) {
45  sError = e.what();
46  }
47  closeGroup(groupHandle);
48  if(!sError.empty()) {
49  throw std::runtime_error("error saving complete graph: " + sError);
50  }
51 }
52 
53 template <class VISITOR>
54 void
56  const hid_t parentHandle,
57  const std::string& graphName,
59 ) {
61  hdf5::HandleCheck<ANDRES_GRAPH_HDF5_DEBUG> handleCheck;
62  hid_t groupHandle = openGroup(parentHandle, graphName);
63  std::string sError;
64  try {
65  {
66  int ID;
67  load(groupHandle, "graph-type-id", ID);
68  if(ID != GraphTraitsHDF5<CompleteGraph>::ID) {
69  sError = "Stored graph type is not a CompleteGraph.";
70  goto cleanup;
71  }
72  }
73  std::size_t numberOfVertices;
74  load(groupHandle, "number-of-vertices", numberOfVertices);
75  graph.assign(numberOfVertices);
76  }
77  catch(std::exception& e) {
78  sError = e.what();
79  }
80 
81 cleanup:
82  closeGroup(groupHandle);
83  if(!sError.empty()) {
84  throw std::runtime_error("error loading complete graph: " + sError);
85  }
86 }
87 
88 } //namespace hdf
89 } //namespace graph
90 } //namespace andres
91 
92 #endif // #ifndef ANDRES_GRAPH_COMPLETE_GRAPH_HDF5_HXX