fintp_utils
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CacheManager.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef CACHEMANAGER_H
22 #define CACHEMANAGER_H
23 
24 #include <string>
25 #include <map>
26 #include <sstream>
27 #include "WorkItemPool.h"
28 
29 using namespace std;
30 
31 // Scenario : Add to cache
32 // // assume member CacheManager< int, DataParameterBase > m_ParamCache of DataStatement class
33 // // DataStatement statement;
34 // // in statement.AddParameter( const unsigned int index, DataParameterBase param )
35 //
36 // m_ParamCache.Add( index, param );
37 
38 // Scenario : Get from cache
39 //
40 // DataParamBase param;
41 // param.setDimension( statement.ParamCache[ index ].getDimension() );
42 
43 // Scenario : Get from cache, or add and return
44 // // in statement.AddParameter( const unsigned int index, DataParameterBase param )
45 //
46 // if ( m_ParamCache.Contains( index ) )
47 // ...
48 // else
49 // ...
50 
51 namespace FinTP
52 {
53  template< class K, class V >
55  {
56  private :
57  map< K, V > m_Cache;
58 
59  public:
60 
61  typedef typename map< K, V >::const_iterator const_iterator;
62  const_iterator begin() const { return m_Cache.begin(); }
63  const_iterator end() const { return m_Cache.end(); }
64 
66  {
67  //INIT_COUNTER( CACHE_MISSES );
68  //INIT_COUNTER( CACHE_HITS );
69  }
70 
71  virtual ~CacheManager()
72  {
73  /*try
74  {
75  DESTROY_COUNTER( CACHE_MISSES );
76  }catch( ... ){};
77  try
78  {
79  DESTROY_COUNTER( CACHE_HITS );
80  }catch( ... ){};*/
81  }
82 
83  void Add( const K& key, V value )
84  {
85  ( void )m_Cache.insert( pair< K, V >( key, value ) );
86  }
87 
88  map< K, V >& data()
89  {
90  return m_Cache;
91  }
92 
93  void clear()
94  {
95  m_Cache.clear();
96  }
97 
98  unsigned int size() const
99  {
100  return m_Cache.size();
101  }
102 
103  bool Contains( const K& key )
104  {
105  typename map< K, V >::const_iterator cacheFinder = m_Cache.find( key );
106  typename map< K, V >::const_iterator cacheEnd = m_Cache.end();
107  bool retValue = ( cacheFinder != cacheEnd );
108  /*if ( retValue )
109  INCREMENT_COUNTER( CACHE_HITS );
110  else
111  INCREMENT_COUNTER( CACHE_MISSES );*/
112 
113  return retValue;
114  }
115 
116  const V& operator[]( const K& key ) const
117  {
118  typename map< K, V >::const_iterator cacheFinder = m_Cache.find( key );
119  typename map< K, V >::const_iterator cacheEnd = m_Cache.end();
120  if( cacheFinder != cacheEnd )
121  return cacheFinder->second;
122 
123  stringstream errorMessage;
124  errorMessage << "Value not found in cache [" << key << "]";
125  throw logic_error( errorMessage.str() );
126  }
127  };
128 }
129 
130 #endif //CACHEMANAGER_H