Gnash  0.8.10
string_table.h
Go to the documentation of this file.
00001 // string_table.h -- A shared string table for Gnash.
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 #ifndef GNASH_STRING_TABLE_H
00021 #define GNASH_STRING_TABLE_H
00022 
00023 // Thread Status: SAFE, except for group functions.
00024 // The group functions may have strange behavior when trying to automatically
00025 // lowercase the additions.
00026 
00027 #include <boost/multi_index_container.hpp>
00028 #include <boost/multi_index/hashed_index.hpp>
00029 #include <boost/multi_index/identity.hpp>
00030 #include <boost/multi_index/member.hpp>
00031 #include <boost/thread.hpp>
00032 #include <string>
00033 #include <map>
00034 #include "dsodefs.h"
00035 
00036 namespace gnash {
00037 
00038 // So many strings are duplicated (such as standard property names)
00039 // that a string table could give significant memory savings.
00041 class DSOEXPORT string_table
00042 {
00043 public:
00044 
00046         struct svt
00047         {
00048                 svt(const std::string& val, std::size_t i)
00049             :
00050                         value(val),
00051             id(i)
00052         {}
00053 
00054                 std::string value;
00055                 std::size_t id;
00056         };
00057     
00059     struct StringID {};
00060 
00062     struct StringValue {};
00063 
00065     //
00069         typedef boost::multi_index_container<svt,
00070                 boost::multi_index::indexed_by<
00071 
00072                         boost::multi_index::hashed_unique<
00073                 boost::multi_index::tag<StringValue>,
00074                                 boost::multi_index::member<svt, std::string, &svt::value> >,
00075 
00076                         boost::multi_index::hashed_unique<
00077                 boost::multi_index::tag<StringID>,
00078                                 boost::multi_index::member<svt, std::size_t, &svt::id>
00079 
00080         > 
00081         > > table;
00082 
00083         typedef std::size_t key;
00084 
00086     //
00088     //
00095         key find(const std::string& to_find, bool insert_unfound = true);
00096 
00098         //
00102         const std::string& value(key to_find) const
00103         {
00104                 if (_table.empty() || !to_find) return _empty;
00105 
00106                 table::index<StringID>::type::iterator r =
00107             _table.get<StringID>().find(to_find);
00108                 return (r == _table.get<StringID>().end()) ? _empty : r->value;
00109         }
00110 
00112         //
00114         key insert(const std::string& to_insert);
00115 
00117     //
00122         void insert_group(const svt* pList, std::size_t size);
00123 
00125     //
00128         key already_locked_insert(const std::string& to_insert);
00129 
00131         string_table()
00132         :
00133                 _highestKey(0),
00134                 _highestKnownLowercase(0)
00135         {}
00136 
00138     //
00142     key noCase(key a) const;
00143 
00145     //
00146     void setHighestKnownLowercase(std::size_t k);
00147 
00148 private:
00149 
00150         table _table;
00151         static const std::string _empty;
00152         boost::mutex _lock;
00153         std::size_t _highestKey;
00154 
00155     std::map<key, key> _caseTable;
00156     key _highestKnownLowercase;
00157 };
00158 
00160 //
00164 //
00167 //
00173 DSOEXPORT bool equal(string_table& st, string_table::key a, string_table::key b,
00174         bool caseless);
00175 
00176 }
00177 #endif