Pol  Revision:f37d500
boostutils.h
Go to the documentation of this file.
1 // Debug Flyweight container
2 // hash factory with public estimateSize method
3 // each factory registers in boost_utils::debug_flyweight_queries
4 
5 
6 #ifndef BOOSTUTILS_H
7 #define BOOSTUTILS_H
8 
9 #include "pol_global_config.h"
10 
11 #ifdef _MSC_VER
12 #pragma warning( disable : 4503 ) // decorated name length exceeded
13 #endif
14 
15 #ifdef DEBUG_FLYWEIGHT
16 #include <boost/flyweight/factory_tag.hpp>
17 #include <boost/flyweight/hashed_factory_fwd.hpp>
18 #include <boost/foreach.hpp>
19 #include <boost/mpl/aux_/lambda_support.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/multi_index/hashed_index.hpp>
22 #include <boost/multi_index/identity.hpp>
23 #include <boost/multi_index_container.hpp>
24 #include <string>
25 #include <vector>
26 
27 #include "compilerspecifics.h"
28 #endif
29 #include <boost/flyweight.hpp>
30 
31 namespace Pol
32 {
33 namespace boost_utils
34 {
35 #ifdef DEBUG_FLYWEIGHT
36 class bucket_query
37 {
38 public:
39  typedef std::size_t size_type;
40  virtual ~bucket_query(){};
41  virtual size_type bucket_count() const = 0;
42  virtual size_type max_bucket_count() const = 0;
43  virtual size_type bucket_size( size_type n ) const = 0;
44  virtual size_type estimateSize() const = 0;
45 };
46 
47 extern std::vector<bucket_query*> debug_flyweight_queries;
48 
49 template <typename Entry, typename Key, typename Hash = boost::mpl::na,
50  typename Pred = boost::mpl::na, typename Allocator = boost::mpl::na>
51 class accessible_hashed_factory_class : public boost::flyweights::factory_marker,
52  public bucket_query
53 {
54  struct index_list
55  : boost::mpl::vector1<boost::multi_index::hashed_unique<
56  boost::multi_index::identity<Entry>,
57  typename boost::mpl::if_<boost::mpl::is_na<Hash>, boost::hash<Key>, Hash>::type,
58  typename boost::mpl::if_<boost::mpl::is_na<Pred>, std::equal_to<Key>, Pred>::type>>
59  {
60  };
61 
62  typedef boost::multi_index::multi_index_container<
63  Entry, index_list,
64  typename boost::mpl::if_<boost::mpl::is_na<Allocator>, std::allocator<Entry>,
65  Allocator>::type>
66  container_type;
67 
68 public:
69  typedef const Entry* handle_type;
70 
71  accessible_hashed_factory_class() { debug_flyweight_queries.push_back( this ); }
72  handle_type insert( const Entry& x ) { return &*cont.insert( x ).first; }
73  void erase( handle_type h ) { cont.erase( cont.iterator_to( *h ) ); }
74  static const Entry& entry( handle_type h ) { return *h; }
75  typedef std::size_t size_type;
76 
77  virtual size_type bucket_count() const POL_OVERRIDE { return cont.bucket_count(); }
78  virtual size_type max_bucket_count() const POL_OVERRIDE { return cont.max_bucket_count(); }
79  virtual size_type bucket_size( size_type n ) const POL_OVERRIDE { return cont.bucket_size( n ); }
80  virtual size_type estimateSize() const POL_OVERRIDE
81  {
82  size_type size = 0;
83  BOOST_FOREACH ( const Entry& e, cont )
84  {
85  size += (int)( 4.5 * sizeof( long ) ); // ~overhead
86  size += ( (std::string)e ).capacity();
87  }
88  return size;
89  }
90 
91 private:
92  container_type cont;
93 
94 public:
95  typedef accessible_hashed_factory_class type;
96  BOOST_MPL_AUX_LAMBDA_SUPPORT( 5, accessible_hashed_factory_class,
97  ( Entry, Key, Hash, Pred, Allocator ) )
98 };
99 
100 template <typename Hash = boost::mpl::na, typename Pred = boost::mpl::na,
101  typename Allocator = boost::mpl::na BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION>
102 struct accessible_hashed_factory : boost::flyweights::factory_marker
103 {
104  template <typename Entry, typename Key>
105  struct apply
106  : boost::mpl::apply2<
107  accessible_hashed_factory_class<boost::mpl::_1, boost::mpl::_2, Hash, Pred, Allocator>,
108  Entry, Key>
109  {
110  };
111 };
112 
113 #define FLYWEIGHT_HASH_FACTORY boost_utils::accessible_hashed_factory<>
114 #else
115 #define FLYWEIGHT_HASH_FACTORY boost::flyweights::hashed_factory<>
116 #endif
117 
118 // definition of the different Flyweight factories
119 // WARNING: do not forget to add new types to struct flyweight_initializers
121 {
122 };
123 typedef boost::flyweight<std::string, boost::flyweights::tag<cprop_name_tag>,
127 {
128 };
129 typedef boost::flyweight<std::string, boost::flyweights::tag<cprop_value_tag>,
133 {
134 };
135 typedef boost::flyweight<std::string, boost::flyweights::tag<cfg_key_tag>, FLYWEIGHT_HASH_FACTORY>
138 {
139 };
140 typedef boost::flyweight<std::string, boost::flyweights::tag<object_name_tag>,
144 {
145 };
146 typedef boost::flyweight<std::string, boost::flyweights::tag<script_name_tag>,
150 {
151 };
152 typedef boost::flyweight<std::string, boost::flyweights::tag<npctemplate_name_tag>,
156 {
157 };
158 typedef boost::flyweight<std::string, boost::flyweights::tag<function_name_tag>,
161 
168 {
169  cprop_name_flystring::initializer fwInit_cprop_name;
170  cprop_value_flystring::initializer fwInit_cprop_value;
171  cfg_key_flystring::initializer fwInit_cfg_key;
172  object_name_flystring::initializer fwInit_obj_name;
173  script_name_flystring::initializer fwInit_script_name;
174  npctemplate_name_flystring::initializer fwInit_npctemplate_name;
175  function_name_flystring::initializer fwInit_func_name;
176 };
177 }
178 }
179 
180 #endif
#define POL_OVERRIDE
boost::flyweight< std::string, boost::flyweights::tag< cfg_key_tag >, FLYWEIGHT_HASH_FACTORY > cfg_key_flystring
Definition: boostutils.h:136
cprop_value_flystring::initializer fwInit_cprop_value
Definition: boostutils.h:170
script_name_flystring::initializer fwInit_script_name
Definition: boostutils.h:173
npctemplate_name_flystring::initializer fwInit_npctemplate_name
Definition: boostutils.h:174
boost::flyweight< std::string, boost::flyweights::tag< cprop_name_tag >, FLYWEIGHT_HASH_FACTORY > cprop_name_flystring
Definition: boostutils.h:125
cfg_key_flystring::initializer fwInit_cfg_key
Definition: boostutils.h:171
function_name_flystring::initializer fwInit_func_name
Definition: boostutils.h:175
cprop_name_flystring::initializer fwInit_cprop_name
Definition: boostutils.h:169
boost::flyweight< std::string, boost::flyweights::tag< script_name_tag >, FLYWEIGHT_HASH_FACTORY > script_name_flystring
Definition: boostutils.h:148
object_name_flystring::initializer fwInit_obj_name
Definition: boostutils.h:172
#define FLYWEIGHT_HASH_FACTORY
Definition: boostutils.h:115
boost::flyweight< std::string, boost::flyweights::tag< cprop_value_tag >, FLYWEIGHT_HASH_FACTORY > cprop_value_flystring
Definition: boostutils.h:131
boost::flyweight< std::string, boost::flyweights::tag< object_name_tag >, FLYWEIGHT_HASH_FACTORY > object_name_flystring
Definition: boostutils.h:142
Definition: berror.cpp:12
boost::flyweight< std::string, boost::flyweights::tag< npctemplate_name_tag >, FLYWEIGHT_HASH_FACTORY > npctemplate_name_flystring
Definition: boostutils.h:154
boost::flyweight< std::string, boost::flyweights::tag< function_name_tag >, FLYWEIGHT_HASH_FACTORY > function_name_flystring
Definition: boostutils.h:160