SymbolMap.hh

00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
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