Add getdefault to Annotations
authorDiane Trout <diane@caltech.edu>
Fri, 6 Apr 2007 00:09:25 +0000 (00:09 +0000)
committerDiane Trout <diane@caltech.edu>
Fri, 6 Apr 2007 00:09:25 +0000 (00:09 +0000)
and dont use std::string& for parameters as a string reference
doesn't work with char * literals.

alg/annotations.cpp
alg/annotations.hpp
alg/test/test_annotations.cpp

index ff84ffe4f06a2c4f881019df30dcfd6259facd87..996e314b4eecb6378f4cbf31773bf9e044a75e67 100644 (file)
@@ -7,7 +7,7 @@ Annotations::Annotations()
   metadata[name_str] = ""; 
 }
 
-Annotations::Annotations(const std::string &n)
+Annotations::Annotations(const std::string n)
 { 
   metadata[name_str] = n; 
 }
@@ -38,12 +38,12 @@ void Annotations::erase(const std::string& key)
   }
 }
   
-void Annotations::set(const std::string& key, const std::string &value )
+void Annotations::set(const std::string key, const std::string value )
 {
   metadata[key] = value;
 }
 
-std::string Annotations::get(const std::string& key) const
+std::string Annotations::get(const std::string key) const
 {
   metadata_map::const_iterator map_i(metadata.find(key));
   if (map_i == metadata.end()) {
@@ -53,7 +53,16 @@ std::string Annotations::get(const std::string& key) const
   }  
 }
 
-bool Annotations::has_key(const std::string& key) const
+std::string Annotations::getdefault(const std::string key, std::string default_value) const
+{
+  try {
+    return get(key);
+  } catch(annotations_key_error e) {
+    return default_value;
+  }
+}
+
+bool Annotations::has_key(const std::string key) const
 {
   metadata_map::const_iterator map_i(metadata.find(key));
   return map_i != metadata.end();
index 203456b19da3b3f49465b80a3d211736e5872cc5..607d6cb46d9ad81417f350fe9c950f11a21e8c24 100644 (file)
@@ -26,7 +26,7 @@ public:
   typedef metadata_map::size_type size_type;
   
   Annotations();
-  Annotations(const std::string& n);
+  Annotations(const std::string n);
   Annotations(const Annotations&);
   Annotations(const AnnotationsRef);
 
@@ -43,11 +43,13 @@ public:
   //! remove some key from our "dictionary"
   void erase(const std::string& key);  
   //! set a metadata element 
-  void set(const std::string& key, const std::string &value );
+  void set(const std::string key, const std::string value );
   //! look for a key, \throws annotation_key_error if it doesn't find it
-  std::string get(const std::string& key) const;
+  std::string get(const std::string key) const;
+  //! look for a key, if not found return default
+  std::string getdefault(const std::string key, std::string default_value) const;
   //! check for a key
-  bool has_key(const std::string& key) const;
+  bool has_key(const std::string key) const;
   //! return all the keys of our metadata map
   //std::list<std::string> keys() const;
   size_type size() const { return metadata.size(); }
index 8bb0bea6407778ae365ecd61dc757a3f1dcd58e1..970d516ee93613e1166be1ef2a701650b661aab8 100644 (file)
@@ -53,6 +53,16 @@ BOOST_AUTO_TEST_CASE( annotations_get_metadata )
   BOOST_CHECK_THROW( asp->get("not there"), annotations_key_error ); 
 }
 
+
+BOOST_AUTO_TEST_CASE( annotations_getdefault_metadata )
+{
+  boost::shared_ptr<Annotations> asp(new Annotations("asp"));
+  asp->set("header", "> amsp");
+  
+  BOOST_CHECK_EQUAL( asp->size(), 2 );
+  BOOST_CHECK_EQUAL( asp->getdefault("header", "foo"), "> amsp" );
+  BOOST_CHECK_EQUAL( asp->getdefault("not there", "foo"), "foo" ); 
+}
 BOOST_AUTO_TEST_CASE( annotations_has_key )
 {
   boost::shared_ptr<Annotations> asp(new Annotations("asp"));