owlcpp  v0.3.3~
C++ library for working with OWL ontologies
 All Classes Namespaces Files Functions Macros Pages
map_ns_prefix.hpp
Go to the documentation of this file.
1 
6 #ifndef MAP_NS_PREFIX_HPP_
7 #define MAP_NS_PREFIX_HPP_
8 #include <string>
9 #include "boost/assert.hpp"
10 #include "boost/multi_index_container.hpp"
11 #include "boost/multi_index/ordered_index.hpp"
12 #include "boost/multi_index/member.hpp"
13 
14 #include "owlcpp/ns_id.hpp"
15 #include "owlcpp/rdf/exception.hpp"
16 
17 namespace owlcpp{
18 
22 
23  struct Pref_wrap {
24  Pref_wrap(std::string const& prefix, const Ns_id nsid)
25  :pref(prefix), id(nsid) {}
26  std::string pref;
27  Ns_id id;
28  };
29 
30  typedef boost::multi_index_container<
31  Pref_wrap,
32  boost::multi_index::indexed_by<
33  boost::multi_index::ordered_unique<
34  boost::multi_index::tag<struct pref_tag>,
35  boost::multi_index::member<Pref_wrap, std::string, &Pref_wrap::pref>
36  >,
37  boost::multi_index::ordered_unique<
38  boost::multi_index::tag<struct id_tag>,
39  boost::multi_index::member<Pref_wrap, Ns_id, &Pref_wrap::id>
40  >
41  >
42  > pref_map_t;
43 
44  typedef pref_map_t::index<pref_tag>::type pref_index_t;
45  typedef pref_index_t::const_iterator pref_iterator_t;
46  typedef pref_map_t::index<id_tag>::type id_index_t;
47  typedef id_index_t::const_iterator id_iterator_t;
48 
49 public:
50  struct Err : public Rdf_err {};
51 
52  std::string prefix(const Ns_id id) const {
53  id_index_t const& index = pref_.get<id_tag>();
54  const id_iterator_t iter = index.find(id);
55  if( iter == index.end() ) return "";
56  return iter->pref;
57  }
58 
59  Ns_id const* find(std::string const& pref) const {
60  pref_index_t const& index = pref_.get<pref_tag>();
61  const pref_iterator_t iter = index.find(pref);
62  if( iter == index.end() ) return 0;
63  return &iter->id;
64  }
65 
72  void set(const Ns_id id, std::string const& pref = "") {
73  id_index_t& id_index = pref_.get<id_tag>();
74  const id_iterator_t id_iter = id_index.find(id);
75 
76  if( pref.empty() ) {
77  if( id_iter != id_index.end() ) id_index.erase(id_iter);
78  return;
79  }
80  if( id_iter != id_index.end() ) {
81  if( id_iter->pref != pref ) BOOST_THROW_EXCEPTION(
82  Err()
83  << Err::msg_t("different IRI prefix has been set")
84  << Err::str1_t(pref)
85  << Err::str2_t(id_iter->pref)
86  );
87  return;
88  }
89 
90  pref_index_t const& pref_index = pref_.get<pref_tag>();
91  const pref_iterator_t pref_iter = pref_index.find(pref);
92  if( pref_iter != pref_index.end() ) BOOST_THROW_EXCEPTION(
93  Err()
94  << Err::msg_t("prefix used for different IRI")
95  << Err::str1_t(pref)
96  );
97  pref_.insert(Pref_wrap(pref, id));
98  }
99 
100  void erase(const Ns_id id) {pref_.get<id_tag>().erase(id);}
101 
102  void clear() {pref_.clear();}
103 
104 private:
105  pref_map_t pref_;
106 };
107 
108 }//namespace owlcpp
109 #endif /* MAP_NS_PREFIX_HPP_ */