basic wrapping of Annotations
[mussa.git] / alg / annotations.hpp
1 #ifndef ANNOTATIONS_HPP_
2 #define ANNOTATIONS_HPP_
3
4 #include <string>
5 #include <map>
6
7 #include <boost/shared_ptr.hpp>
8
9 #include "mussa_exceptions.hpp"
10
11 class Annotations;
12 typedef boost::shared_ptr<Annotations> AnnotationsRef;
13
14 /**
15  * \brief Annotations provides a simple "dictionary" of name value pairs
16  * 
17  * The intent is that this is to be used as a mix-in class to add
18  * a uniform way of storing annotation metadata.
19  */
20 class Annotations {
21 protected:
22   static const std::string name_str;
23
24 public:
25   typedef std::string key_type;
26   typedef std::string mapped_type;
27   typedef std::map<key_type, mapped_type> metadata_map;
28   typedef metadata_map::iterator iterator;
29   typedef metadata_map::const_iterator const_iterator;
30   typedef metadata_map::size_type size_type;
31   
32   Annotations();
33   Annotations(const std::string n);
34   Annotations(const Annotations&);
35   Annotations(const AnnotationsRef);
36
37   //! make a shared pointer copy of our object
38   AnnotationsRef copy() const;
39
40   mapped_type& operator[](const key_type& k) { return metadata[k]; }
41     
42   //! provide a special case for accessing a name
43   std::string name() const { return metadata.find(name_str)->second; }
44   //! special case for setting name
45   void setName(const std::string& n) { metadata[name_str] = n; }
46   
47   //! Generic metadata handling
48   //@{
49   //! begin iterator
50   iterator begin() { return metadata.begin(); }
51   //! const begin iterator
52   const_iterator begin() const { return metadata.begin(); }
53   //! end iterator
54   iterator end() { return metadata.end(); }
55   //! const end iterator
56   const_iterator end() const { return metadata.end(); }
57   //! remove some key from our "dictionary"
58   void erase(const std::string& key);  
59   //! find an element in the map
60   iterator find( const key_type& key) { return metadata.find(key); }
61   const_iterator find( const key_type& key) const { return metadata.find(key); }
62   //! set a metadata element 
63   void set(const std::string key, const std::string value );
64   //! look for a key, \throws annotation_key_error if it doesn't find it
65   std::string get(const std::string key) const;
66   //! look for a key, if not found return default
67   std::string getdefault(const std::string key, std::string default_value) const;
68   //! check for a key
69   bool has_key(const std::string key) const;
70   //! return number of items in the map
71   size_type size() const { return metadata.size(); }
72   //@}
73 protected:
74   metadata_map metadata;
75 };
76 #endif /*ANNOTATIONS_HPP_*/