SymbolMap.hh

00001 
00019 #ifndef avro_SymbolMap_hh__
00020 #define avro_SymbolMap_hh__
00021 
00022 #include <map>
00023 #include <boost/noncopyable.hpp>
00024 
00025 #include "Node.hh"
00026 #include "Schema.hh"
00027 
00028 namespace avro {
00029 
00035 
00036 class SymbolMap : private boost::noncopyable
00037 {
00038 
00039   public:
00040 
00041     SymbolMap()
00042     {}
00043 
00044     bool registerSymbol(const NodePtr &node) {
00045 
00046         if(node->type() == AVRO_SYMBOLIC) {
00047             throw Exception("Node must not be a symbolic name");
00048         }
00049         const std::string name = node->name();
00050         if(name.empty()) {
00051             throw Exception("Node must have a name to be registered");
00052         }
00053         bool added = false;
00054         MapImpl::iterator lb = map_.lower_bound(name);
00055 
00056         if(lb == map_.end() || map_.key_comp()(name, lb->first)) {
00057             map_.insert(lb, std::make_pair(name, node));
00058             added = true; 
00059         }
00060         return added;
00061     }
00062 
00063     bool hasSymbol(const std::string &name) const {
00064         return map_.find(name) != map_.end();
00065     }
00066 
00067     NodePtr locateSymbol(const std::string &name) const {
00068         MapImpl::const_iterator iter = map_.find(name);
00069         return (iter == map_.end()) ? NodePtr() : iter->second;
00070     }
00071 
00072   private:
00073 
00074     typedef std::map<std::string, NodePtr> MapImpl;
00075 
00076     MapImpl map_;
00077 };
00078 
00079 
00080 } // namespace avro
00081 
00082 #endif