Garmin Fleet Management Controller  2.19.0
FileBackedMap.h
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * HEADER NAME:
4 * FileBackedMap.h
5 *
6 * Copyright 2008-2012 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 
11 #ifndef _FILEBACKEDMAP_H
12 #define _FILEBACKEDMAP_H
13 
14 #include <map>
15 #include <vector>
16 
17 //----------------------------------------------------------------------
30 //----------------------------------------------------------------------
31 template <class T>
33 {
34 public:
36  typedef typename T::key_type key_type;
37 
38  //--------------------------------------------------------------------
44  //--------------------------------------------------------------------
46  (
47  const char * aFileName
48  ) : mFileName( aFileName )
49  {
50  load();
51  }
52 
54  typedef typename std::map<key_type, T>::const_iterator const_iterator;
55 
56  //--------------------------------------------------------------------
58  //--------------------------------------------------------------------
59  const_iterator begin()
60  {
61  return mMap.begin();
62  }
63 
64  //--------------------------------------------------------------------
66  //--------------------------------------------------------------------
67  const_iterator end()
68  {
69  return mMap.end();
70  }
71 
72  //--------------------------------------------------------------------
76  //--------------------------------------------------------------------
77  bool contains
78  (
79  const key_type & aKey
80  )
81  {
82  return mMap.end() != mMap.find( aKey );
83  }
84 
85  //--------------------------------------------------------------------
87  //--------------------------------------------------------------------
88  void clear()
89  {
90  mMap.clear();
91  save();
92  }
93 
94  //--------------------------------------------------------------------
100  //--------------------------------------------------------------------
101  T & get
102  (
103  const key_type & aKey
104  )
105  {
106  T& item = mMap[aKey];
107  item.setId( aKey );
108  item.setParent( this );
109  return item;
110  }
111 
112  //--------------------------------------------------------------------
117  //--------------------------------------------------------------------
118  const key_type& getKeyAt
119  (
120  uint32 aIndex
121  ) const
122  {
123  ASSERT( aIndex < mKeyList.size() );
124 
125  return mKeyList.at( aIndex );
126  }
127 
128  //--------------------------------------------------------------------
131  //--------------------------------------------------------------------
132  void put
133  (
134  T & aValue
135  )
136  {
137  aValue.setParent( this );
138  mMap[ aValue.getId() ] = aValue;
139  save();
140  }
141 
142  //--------------------------------------------------------------------
145  //--------------------------------------------------------------------
146  void remove
147  (
148  const key_type & aKey
149  )
150  {
151  std::map<key_type, T>::iterator iter = mMap.find( aKey );
152  if( iter != mMap.end() )
153  {
154  mMap.erase( iter );
155  save();
156  }
157  }
158 
159  //--------------------------------------------------------------------
162  //--------------------------------------------------------------------
163  bool empty()
164  {
165  return mMap.empty();
166  }
167 
168  //--------------------------------------------------------------------
170  //--------------------------------------------------------------------
171  void save()
172  {
173  std::ofstream outputStream;
174  std::map<key_type, T>::iterator iter;
175 
176  outputStream.open( mFileName, std::ios::out );
177 
178  if( outputStream.good() )
179  {
180  mKeyList.clear();
181  for( iter = mMap.begin(); iter != mMap.end(); ++iter )
182  {
183  if( iter->second.isValid() )
184  {
185  iter->second.writeToStream( outputStream );
186  mKeyList.push_back( iter->first );
187  }
188  }
189  }
190  outputStream.close();
191  }
192 
193  //--------------------------------------------------------------------
196  //--------------------------------------------------------------------
197  uint32 count() const
198  {
199  return mMap.size();
200  }
201 
202  //--------------------------------------------------------------------
205  //--------------------------------------------------------------------
207  {
208  return (uint32) mKeyList.size();
209  }
210 
211 private:
212  //--------------------------------------------------------------------
215  //--------------------------------------------------------------------
216  void load()
217  {
218  std::ifstream inputStream( mFileName, std::ios_base::in );
219  mKeyList.clear();
220  if( inputStream.good() )
221  {
222  inputStream.peek(); //need to do a read before eof registers
223  while( !inputStream.eof() && !inputStream.fail() )
224  {
225  typename T item;
226  item.setParent( this );
227  item.readFromStream( inputStream );
228  mMap[ item.getId() ] = item;
229  if( item.isValid() )
230  {
231  mKeyList.push_back( item.getId() );
232  }
233  inputStream.peek();
234  }
235  }
236  inputStream.close();
237  }
238 
240  const char* mFileName;
241 
243  std::map<key_type, T> mMap;
244 
246  std::vector<key_type> mKeyList;
247 };
248 
249 #endif
std::vector< key_type > mKeyList
Map of indexes to keys.
const_iterator begin()
Iterator positioned at the first element in the map.
Definition: FileBackedMap.h:59
uint32 count() const
The number of items in the map.
void put(T &aValue)
Add (or replace) an item in the map.
std::map< key_type, T >::const_iterator const_iterator
Iterator for read-only traversal through the map.
Definition: FileBackedMap.h:54
const_iterator end()
Iterator positioned after the last element in the map.
Definition: FileBackedMap.h:67
bool contains(const key_type &aKey)
Check whether the specified key is in the map.
Definition: FileBackedMap.h:78
FileBackedMap(const char *aFileName)
Create a new FileBackedMap.
Definition: FileBackedMap.h:46
void clear()
Remove all elements from the map.
Definition: FileBackedMap.h:88
bool empty()
Check whether the map is empty.
void load()
Read the contents of the backing file into the map.
const char * mFileName
The name of the file that this map is saved to.
std::map< key_type, T > mMap
The underlying map in memory.
uint32 validCount() const
The number of valid items in the map.
const key_type & getKeyAt(uint32 aIndex) const
Get the key for the item at a given list index.
Map whose contents are also saved to a file.
Definition: FileBackedMap.h:32
unsigned long int uint32
32-bit unsigned integer
Definition: garmin_types.h:66
void save()
Save the map to disk.
T::key_type key_type
key type
Definition: FileBackedMap.h:36