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 //
174  bool caseless);
175 
176 }
177 #endif
Definition: GnashKey.h:147
svt(std::string val, std::size_t i)
Definition: string_table.h:48
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
const std::string & value(key to_find) const
Find a string by its key.
Definition: string_table.h:102
Definition: GnashKey.h:157
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
A general use string table.
Definition: string_table.h:41
Definition: GnashKey.h:164
string_table()
Construct the empty string_table.
Definition: string_table.h:131
std::size_t id
Definition: string_table.h:55
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
A tag to identify the key index.
Definition: string_table.h:59
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
A tag to identify the string index.
Definition: string_table.h:62
bool caseless(const as_object &o)
Return whether property matching is caseless.
Definition: as_object.h:924
Definition: GnashKey.h:155
std::string value
Definition: string_table.h:54
std::size_t key
Definition: string_table.h:83
A little helper for indexing.
Definition: string_table.h:46