18b52a31eSHanoh Haim#ifndef C_GEN_MAP
28b52a31eSHanoh Haim#define C_GEN_MAP
38b52a31eSHanoh Haim/*
48b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
58b52a31eSHanoh Haim
68b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
78b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
88b52a31eSHanoh HaimYou may obtain a copy of the License at
98b52a31eSHanoh Haim
108b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
118b52a31eSHanoh Haim
128b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
138b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
148b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158b52a31eSHanoh HaimSee the License for the specific language governing permissions and
168b52a31eSHanoh Haimlimitations under the License.
178b52a31eSHanoh Haim*/
188b52a31eSHanoh Haim
198b52a31eSHanoh Haim
208b52a31eSHanoh Haim#include <stdint.h>
218b52a31eSHanoh Haim#include <map>
228b52a31eSHanoh Haim#include <string>
238b52a31eSHanoh Haim
248b52a31eSHanoh Haimtemplate<class KEY, class VAL>
258b52a31eSHanoh Haimclass CGenericMap   {
268b52a31eSHanoh Haimpublic:
278b52a31eSHanoh Haim    typedef std::map<KEY, VAL*,  std::less<KEY> > gen_map_t;
288b52a31eSHanoh Haim    typename gen_map_t::iterator gen_map_iter_t;
298b52a31eSHanoh Haim    typedef void (free_map_object_func_t)(VAL *p);
308b52a31eSHanoh Haim
318b52a31eSHanoh Haim
328b52a31eSHanoh Haim    bool Create(void){
338b52a31eSHanoh Haim        return(true);
348b52a31eSHanoh Haim    }
358b52a31eSHanoh Haim    void Delete(){
368b52a31eSHanoh Haim        //remove_all();
378b52a31eSHanoh Haim    }
388b52a31eSHanoh Haim    VAL * remove(KEY  key ){
398b52a31eSHanoh Haim        VAL *lp = lookup(key);
408b52a31eSHanoh Haim        if ( lp ) {
418b52a31eSHanoh Haim            m_map.erase(key);
428b52a31eSHanoh Haim            return (lp);
438b52a31eSHanoh Haim        }else{
448b52a31eSHanoh Haim            return(0);
458b52a31eSHanoh Haim        }
468b52a31eSHanoh Haim    }
478b52a31eSHanoh Haim
488b52a31eSHanoh Haim
498b52a31eSHanoh Haim    void remove_no_lookup(KEY  key ){
508b52a31eSHanoh Haim         m_map.erase(key);
518b52a31eSHanoh Haim    }
528b52a31eSHanoh Haim
538b52a31eSHanoh Haim
548b52a31eSHanoh Haim    VAL * lookup(KEY  key ){
558b52a31eSHanoh Haim        typename gen_map_t::iterator iter;
568b52a31eSHanoh Haim        iter = m_map.find(key);
578b52a31eSHanoh Haim        if (iter != m_map.end() ) {
588b52a31eSHanoh Haim            return ( (*iter).second );
598b52a31eSHanoh Haim        }else{
608b52a31eSHanoh Haim            return (( VAL*)0);
618b52a31eSHanoh Haim        }
628b52a31eSHanoh Haim    }
638b52a31eSHanoh Haim    void add(KEY  key,VAL * val){
648b52a31eSHanoh Haim        m_map.insert(typename gen_map_t::value_type(key,val));
658b52a31eSHanoh Haim    }
668b52a31eSHanoh Haim
678b52a31eSHanoh Haim
688b52a31eSHanoh Haim    void remove_all(free_map_object_func_t func){
698b52a31eSHanoh Haim        if ( m_map.empty() )
708b52a31eSHanoh Haim            return;
718b52a31eSHanoh Haim
728b52a31eSHanoh Haim        typename gen_map_t::iterator it;
738b52a31eSHanoh Haim        for (it= m_map.begin(); it != m_map.end(); ++it) {
748b52a31eSHanoh Haim            VAL *lp = it->second;
758b52a31eSHanoh Haim            func(lp);
768b52a31eSHanoh Haim        }
778b52a31eSHanoh Haim        m_map.clear();
788b52a31eSHanoh Haim    }
798b52a31eSHanoh Haim
808b52a31eSHanoh Haim    void dump_all(FILE *fd){
818b52a31eSHanoh Haim        typename gen_map_t::iterator it;
828b52a31eSHanoh Haim        for (it= m_map.begin(); it != m_map.end(); ++it) {
838b52a31eSHanoh Haim            VAL *lp = it->second;
848b52a31eSHanoh Haim            lp->Dump(fd);
858b52a31eSHanoh Haim        }
868b52a31eSHanoh Haim    }
878b52a31eSHanoh Haim
888b52a31eSHanoh Haim    uint64_t count(void){
898b52a31eSHanoh Haim        return ( m_map.size());
908b52a31eSHanoh Haim    }
918b52a31eSHanoh Haim
928b52a31eSHanoh Haimpublic:
938b52a31eSHanoh Haim    gen_map_t  m_map;
948b52a31eSHanoh Haim};
958b52a31eSHanoh Haim
968b52a31eSHanoh Haim#endif
97