1#ifndef C_GEN_MAP
2#define C_GEN_MAP
3/*
4Copyright (c) 2015-2015 Cisco Systems, Inc.
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19
20#include <stdint.h>
21#include <map>
22#include <string>
23
24template<class KEY, class VAL>
25class CGenericMap   {
26public:
27    typedef std::map<KEY, VAL*,  std::less<KEY> > gen_map_t;
28    typename gen_map_t::iterator gen_map_iter_t;
29    typedef void (free_map_object_func_t)(VAL *p);
30
31
32    bool Create(void){
33        return(true);
34    }
35    void Delete(){
36        //remove_all();
37    }
38    VAL * remove(KEY  key ){
39        VAL *lp = lookup(key);
40        if ( lp ) {
41            m_map.erase(key);
42            return (lp);
43        }else{
44            return(0);
45        }
46    }
47
48
49    void remove_no_lookup(KEY  key ){
50         m_map.erase(key);
51    }
52
53
54    VAL * lookup(KEY  key ){
55        typename gen_map_t::iterator iter;
56        iter = m_map.find(key);
57        if (iter != m_map.end() ) {
58            return ( (*iter).second );
59        }else{
60            return (( VAL*)0);
61        }
62    }
63    void add(KEY  key,VAL * val){
64        m_map.insert(typename gen_map_t::value_type(key,val));
65    }
66
67
68    void remove_all(free_map_object_func_t func){
69        if ( m_map.empty() )
70            return;
71
72        typename gen_map_t::iterator it;
73        for (it= m_map.begin(); it != m_map.end(); ++it) {
74            VAL *lp = it->second;
75            func(lp);
76        }
77        m_map.clear();
78    }
79
80    void dump_all(FILE *fd){
81        typename gen_map_t::iterator it;
82        for (it= m_map.begin(); it != m_map.end(); ++it) {
83            VAL *lp = it->second;
84            lp->Dump(fd);
85        }
86    }
87
88    uint64_t count(void){
89        return ( m_map.size());
90    }
91
92public:
93    gen_map_t  m_map;
94};
95
96#endif
97