Gnash  0.8.10
PropertyList.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_PROPERTYLIST_H
00020 #define GNASH_PROPERTYLIST_H
00021 
00022 #include <set> 
00023 #include <string> // for use within map 
00024 #include <cassert> // for inlines
00025 #include <utility> // for std::pair
00026 #include <boost/cstdint.hpp> 
00027 #include <boost/multi_index_container.hpp>
00028 #include <boost/multi_index/ordered_index.hpp>
00029 #include <boost/multi_index/sequenced_index.hpp>
00030 #include <boost/multi_index/key_extractors.hpp>
00031 #include <boost/noncopyable.hpp>
00032 #include <boost/bind.hpp>
00033 #include <algorithm>
00034 
00035 #include "Property.h" // for templated functions
00036 
00037 // Forward declaration
00038 namespace gnash {
00039     class as_object;
00040     class as_function;
00041     struct ObjectURI;
00042     class as_value;
00043 }
00044 
00045 namespace gnash {
00046 
00048 class PropertyVisitor {
00049 public:
00050 
00052     virtual bool accept(const ObjectURI& uri, const as_value& val) = 0;
00053     virtual ~PropertyVisitor() {}
00054 };
00055 
00057 class KeyVisitor {
00058 public:
00059 
00061     virtual void operator()(const ObjectURI& uri) = 0;
00062     virtual ~KeyVisitor() {}
00063 };
00064 
00065 
00067 //
00071 //
00075 //
00080 class PropertyList : boost::noncopyable
00081 {
00082 public:
00083 
00084     typedef std::set<ObjectURI, ObjectURI::LessThan> PropertyTracker;
00085     typedef Property value_type;
00086 
00088     struct CreationOrder {};
00089 
00091     typedef boost::multi_index::sequenced<
00092         boost::multi_index::tag<CreationOrder> > SequencedIndex;
00093     
00094     struct KeyExtractor
00095     {
00096         typedef const ObjectURI& result_type;
00097         result_type operator()(const Property& p) const {
00098             return p.uri();
00099         }
00100     };
00101 
00103     struct Case {};
00104     
00106     typedef boost::multi_index::ordered_unique<
00107         boost::multi_index::tag<Case>,
00108         KeyExtractor,
00109         ObjectURI::LessThan> CaseIndex;
00110 
00112     struct NoCase {};
00113     
00115     typedef boost::multi_index::ordered_non_unique<
00116         boost::multi_index::tag<NoCase>,
00117         KeyExtractor,
00118         ObjectURI::CaseLessThan> NoCaseIndex;
00119 
00121     typedef boost::multi_index_container<
00122         value_type,
00123         boost::multi_index::indexed_by<SequencedIndex, CaseIndex, NoCaseIndex>
00124         > container;
00125 
00126     typedef container::iterator iterator;
00127     typedef container::const_iterator const_iterator;
00128 
00130     //
00132     PropertyList(as_object& obj);
00133 
00135     //
00139     //
00144     //
00149     template <class U, class V>
00150     void visitValues(V& visitor, U cmp = U()) const {
00151 
00152         for (const_iterator it = _props.begin(), ie = _props.end();
00153                 it != ie; ++it) {
00154 
00155             if (!cmp(*it)) continue;
00156             as_value val = it->getValue(_owner);
00157             if (!visitor.accept(it->uri(), val)) return;
00158         }
00159     }
00160 
00162     //
00170     void visitKeys(KeyVisitor& v, PropertyTracker& donelist) const;
00171 
00173     //
00187     bool setValue(const ObjectURI& uri, const as_value& value,
00188             const PropFlags& flagsIfMissing = 0);
00189 
00191     //
00196     Property* getProperty(const ObjectURI& uri) const;
00197 
00199     //
00209     std::pair<bool,bool> delProperty(const ObjectURI& uri);
00210 
00212     //
00214     //
00225     bool addGetterSetter(const ObjectURI& uri, as_function& getter,
00226         as_function* setter, const as_value& cacheVal,
00227         const PropFlags& flagsIfMissing = 0);
00228 
00230     //
00237     bool addGetterSetter(const ObjectURI& uri, as_c_function_ptr getter,
00238         as_c_function_ptr setter, const PropFlags& flagsIfMissing);
00239 
00241     //
00248     bool addDestructiveGetter(const ObjectURI& uri, as_function& getter,
00249         const PropFlags& flagsIfMissing = 0);
00250 
00258     //                          one is created.
00261     bool addDestructiveGetter(const ObjectURI& uri, as_c_function_ptr getter, 
00262         const PropFlags& flagsIfMissing = 0);
00263 
00265     //
00269     void setFlags(const ObjectURI& uri, int setTrue, int setFalse);
00270 
00272     //
00275     void setFlagsAll(int setTrue, int setFalse);
00276 
00278     void clear();
00279 
00281     size_t size() const {
00282         return _props.size();
00283     }
00284 
00286     //
00289     void dump();
00290 
00292     //
00295     void setReachable() const {
00296         std::for_each(_props.begin(), _props.end(),
00297                 boost::mem_fn(&Property::setReachable));
00298     }
00299 
00300 private:
00301 
00302     container _props;
00303 
00304     as_object& _owner;
00305 
00306 };
00307 
00308 
00309 } // namespace gnash
00310 
00311 #endif // GNASH_PROPERTYLIST_H