Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unit_const_cache.h
Go to the documentation of this file.
1 #ifndef __UNIT_CONST_CACHE_H
2 #define __UNIT_CONST_CACHE_H
3 #include "config.h"
4 #include <string>
5 #include <gnuhash.h>
6 #ifndef WIN32
7 class ConstHasher;
8 #endif
10 {
11  friend class ConstHasher;
12  std::string key;
13  int fac;
14 public: StringIntKey( std::string k, int f )
15  {
16  key = k;
17  fac = f;
18  }
19  bool operator==( const StringIntKey &b ) const
20  {
21  return fac == b.fac && key == b.key;
22  }
23  bool operator<( const StringIntKey &b ) const
24  {
25  if (fac != b.fac)
26  return fac < b.fac;
27  return key < b.key;
28  }
29  operator size_t() const {
31  }
32 };
33 
34 #if !defined (_WIN32) && __GNUC__ != 2
36 {
37 public:
38  template < class T >
39  size_t operator()( const T &key ) const
40  {
41  return vsHash< T > () ( key );
42  }
43  size_t operator()( const StringIntKey &key ) const
44  {
45  return vsHash< std::string > () ( key.key )^vsHash< size_t > () ( (size_t) key.fac );
46  }
47 };
48 #endif
49 
50 template < class Typ, class Key >
52 {
53 #if !defined (_WIN32) && __GNUC__ != 2
54  static vsUMap< Key, Typ*, ConstHasher >unit_cache;
55 #else
56  static vsUMap< Key, Typ* >unit_cache;
57 #endif
58 public:
59  static const Typ * getCachedConst( Key k )
60  {
61  return getCachedMutable( k );
62  }
63  static Typ * getCachedMutable( const Key &k )
64  {
65 #if !defined (_WIN32) && __GNUC__ != 2
66  typename vsUMap< Key, Typ*, ConstHasher >::iterator i = unit_cache.find( k );
67 #else
68  typename vsUMap< Key, Typ* >::iterator i = unit_cache.find( k );
69 #endif
70  if ( i != unit_cache.end() )
71  return (*i).second;
72  return NULL;
73  }
74  static Typ * setCachedMutable( const Key &k, Typ *un )
75  {
76  unit_cache.insert( std::pair< Key, Typ* > ( k, un ) );
77  return un;
78  }
79  static const Typ * setCachedConst( const Key &k, Typ *un )
80  {
81  return setCachedMutable( k, un );
82  }
83  static void purgeCache( void (*Kill)( Typ *un ) )
84  {
85  typename vsUMap< Key, Typ*, ConstHasher >::iterator i = unit_cache.begin();
86  for (; i != unit_cache.end(); ++i)
87  (*Kill)( (*i).second );
88  unit_cache.clear();
89  }
90 };
91 
92 #if (defined (__GNUC__) && ( ( __GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ > 3 ) )
93 template < class Typ, class Key >
94 vsUMap< Key, Typ*, ConstHasher >ClassCache< Typ, Key >::unit_cache;
95 #endif
96 
99 
100 #endif
101