Gnash  0.8.11dev
string_table.h
Go to the documentation of this file.
1 // string_table.h -- A shared string table for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef GNASH_STRING_TABLE_H
21 #define GNASH_STRING_TABLE_H
22 
23 // Thread Status: SAFE, except for group functions.
24 // The group functions may have strange behavior when trying to automatically
25 // lowercase the additions.
26 
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/hashed_index.hpp>
29 #include <boost/multi_index/identity.hpp>
30 #include <boost/multi_index/member.hpp>
31 #include <string>
32 #include <map>
33 #include <mutex>
34 #include "dsodefs.h"
35 
36 namespace gnash {
37 
38 // So many strings are duplicated (such as standard property names)
39 // that a string table could give significant memory savings.
42 {
43 public:
44 
46  struct svt
47  {
48  svt(std::string val, std::size_t i)
49  :
50  value(std::move(val)),
51  id(i)
52  {}
53 
54  std::string value;
55  std::size_t id;
56  };
57 
59  struct StringID {};
60 
62  struct StringValue {};
63 
65  //
69  typedef boost::multi_index_container<svt,
70  boost::multi_index::indexed_by<
71 
72  boost::multi_index::hashed_unique<
73  boost::multi_index::tag<StringValue>,
74  boost::multi_index::member<svt, std::string, &svt::value> >,
75 
76  boost::multi_index::hashed_unique<
77  boost::multi_index::tag<StringID>,
78  boost::multi_index::member<svt, std::size_t, &svt::id>
79 
80  >
81  > > table;
82 
83  typedef std::size_t key;
84 
86  //
88  //
95  key find(const std::string& to_find, bool insert_unfound = true);
96 
98  //
102  const std::string& value(key to_find) const
103  {
104  if (_table.empty() || !to_find) return _empty;
105 
106  table::index<StringID>::type::iterator r =
107  _table.get<StringID>().find(to_find);
108  return (r == _table.get<StringID>().end()) ? _empty : r->value;
109  }
110 
112  //
114  key insert(const std::string& to_insert);
115 
117  //
122  void insert_group(const svt* pList, std::size_t size);
123 
125  //
128  key already_locked_insert(const std::string& to_insert);
129 
132  :
133  _highestKey(0),
134  _highestKnownLowercase(0)
135  {}
136 
138  //
142  key noCase(key a) const;
143 
145  //
146  void setHighestKnownLowercase(std::size_t k);
147 
148 private:
149 
150  table _table;
151  static const std::string _empty;
152  std::mutex _lock;
153  std::size_t _highestKey;
154 
155  std::map<key, key> _caseTable;
156  key _highestKnownLowercase;
157 };
158 
160 //
164 //
167 //
173 DSOEXPORT bool equal(string_table& st, string_table::key a, string_table::key b,
174  bool caseless);
175 
176 }
177 #endif
gnash::key::l
@ l
Definition: GnashKey.h:158
gnash::string_table::svt::svt
svt(std::string val, std::size_t i)
Definition: string_table.h:48
string_table.h
gnash::string_table::svt::value
std::string value
Definition: string_table.h:54
gnash::string_table
A general use string table.
Definition: string_table.h:42
gnash::string_table::value
const std::string & value(key to_find) const
Find a string by its key.
Definition: string_table.h:102
dsodefs.h
gnash::key::i
@ i
Definition: GnashKey.h:155
gnash::string_table::svt::id
std::size_t id
Definition: string_table.h:55
gnash::string_table::StringID
A tag to identify the key index.
Definition: string_table.h:59
gnash::string_table::svt
A little helper for indexing.
Definition: string_table.h:47
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::string_table::already_locked_insert
key already_locked_insert(const std::string &to_insert)
Insert a string when you will handle the locking yourself.
Definition: string_table.cpp:102
gnash::key::s
@ s
Definition: GnashKey.h:165
gnash::string_table::string_table
string_table()
Construct the empty string_table.
Definition: string_table.h:131
ts
std::uint32_t ts
Definition: LocalConnection_as.cpp:150
gnash::string_table::table
boost::multi_index_container< svt, boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::tag< StringValue >, boost::multi_index::member< svt, std::string, &svt::value > >, boost::multi_index::hashed_unique< boost::multi_index::tag< StringID >, boost::multi_index::member< svt, std::size_t, &svt::id > > > > table
The container for indexing the strings.
Definition: string_table.h:81
gnash::key::k
@ k
Definition: GnashKey.h:157
gnash::key::t
@ t
Definition: GnashKey.h:166
Stats.h
gnash::key::r
@ r
Definition: GnashKey.h:164
gnash::string_table::insert_group
void insert_group(const svt *pList, std::size_t size)
Insert a group of strings with their ids preset.
Definition: string_table.cpp:73
gnash::key::a
@ a
Definition: GnashKey.h:147
gnash::string_table::StringValue
A tag to identify the string index.
Definition: string_table.h:62
gnash::string_table::insert
key insert(const std::string &to_insert)
Insert a string with auto-assigned id.
Definition: string_table.cpp:66
gnash::stats::KeyLookup
Definition: Stats.h:33
gnash::string_table::find
key find(const std::string &to_find, bool insert_unfound=true)
Find a key for a string.
Definition: string_table.cpp:40
gnash::stats::KeyLookup::check
void check(string_table::key k)
Definition: Stats.h:61
gnashconfig.h
gnash::string_table::setHighestKnownLowercase
void setHighestKnownLowercase(std::size_t k)
Set the highest key value known to correspond to a lowercase name.
Definition: string_table.cpp:138
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::equal
bool equal(string_table &st, string_table::key a, string_table::key b, bool caseless)
Check whether two keys are equivalent.
Definition: string_table.cpp:174
gnash::string_table::key
std::size_t key
Definition: string_table.h:83
gnash::caseless
bool caseless(const as_object &o)
Return whether property matching is caseless.
Definition: as_object.h:924
gnash::key::b
@ b
Definition: GnashKey.h:148
gnash::string_table::noCase
key noCase(key a) const
Return a caseless equivalent of the passed key.
Definition: string_table.cpp:144