Gnash  0.8.11dev
PropertyList.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_PROPERTYLIST_H
20 #define GNASH_PROPERTYLIST_H
21 
22 #include <set>
23 #include <string> // for use within map
24 #include <cassert> // for inlines
25 #include <utility> // for std::pair
26 #include <boost/cstdint.hpp>
27 #include <boost/multi_index_container.hpp>
28 #include <boost/multi_index/ordered_index.hpp>
29 #include <boost/multi_index/sequenced_index.hpp>
30 #include <boost/multi_index/key_extractors.hpp>
31 #include <boost/noncopyable.hpp>
32 #include <boost/bind.hpp>
33 #include <algorithm>
34 
35 #include "Property.h" // for templated functions
36 
37 // Forward declaration
38 namespace gnash {
39  class as_object;
40  class as_function;
41  struct ObjectURI;
42  class as_value;
43 }
44 
45 namespace gnash {
46 
49 public:
50 
52  virtual bool accept(const ObjectURI& uri, const as_value& val) = 0;
53  virtual ~PropertyVisitor() {}
54 };
55 
57 class KeyVisitor {
58 public:
59 
61  virtual void operator()(const ObjectURI& uri) = 0;
62  virtual ~KeyVisitor() {}
63 };
64 
65 
67 //
71 //
75 //
80 class PropertyList : boost::noncopyable
81 {
82 public:
83 
84  typedef std::set<ObjectURI, ObjectURI::LessThan> PropertyTracker;
86 
88  struct CreationOrder {};
89 
91  typedef boost::multi_index::sequenced<
92  boost::multi_index::tag<CreationOrder> > SequencedIndex;
93 
94  struct KeyExtractor
95  {
96  typedef const ObjectURI& result_type;
97  result_type operator()(const Property& p) const {
98  return p.uri();
99  }
100  };
101 
103  struct Case {};
104 
106  typedef boost::multi_index::ordered_unique<
107  boost::multi_index::tag<Case>,
108  KeyExtractor,
110 
112  struct NoCase {};
113 
115  typedef boost::multi_index::ordered_non_unique<
116  boost::multi_index::tag<NoCase>,
117  KeyExtractor,
119 
121  typedef boost::multi_index_container<
122  value_type,
123  boost::multi_index::indexed_by<SequencedIndex, CaseIndex, NoCaseIndex>
125 
126  typedef container::iterator iterator;
127  typedef container::const_iterator const_iterator;
128 
130  //
132  PropertyList(as_object& obj);
133 
135  //
139  //
144  //
149  template <class U, class V>
150  void visitValues(V& visitor, U cmp = U()) const {
151 
152  for (const_iterator it = _props.begin(), ie = _props.end();
153  it != ie; ++it) {
154 
155  if (!cmp(*it)) continue;
156  as_value val = it->getValue(_owner);
157  if (!visitor.accept(it->uri(), val)) return;
158  }
159  }
160 
162  //
170  void visitKeys(KeyVisitor& v, PropertyTracker& donelist) const;
171 
173  //
187  bool setValue(const ObjectURI& uri, const as_value& value,
188  const PropFlags& flagsIfMissing = 0);
189 
191  //
196  Property* getProperty(const ObjectURI& uri) const;
197 
199  //
209  std::pair<bool,bool> delProperty(const ObjectURI& uri);
210 
212  //
214  //
225  bool addGetterSetter(const ObjectURI& uri, as_function& getter,
226  as_function* setter, const as_value& cacheVal,
227  const PropFlags& flagsIfMissing = 0);
228 
230  //
237  bool addGetterSetter(const ObjectURI& uri, as_c_function_ptr getter,
238  as_c_function_ptr setter, const PropFlags& flagsIfMissing);
239 
241  //
248  bool addDestructiveGetter(const ObjectURI& uri, as_function& getter,
249  const PropFlags& flagsIfMissing = 0);
250 
258  // one is created.
261  bool addDestructiveGetter(const ObjectURI& uri, as_c_function_ptr getter,
262  const PropFlags& flagsIfMissing = 0);
263 
265  //
269  void setFlags(const ObjectURI& uri, int setTrue, int setFalse);
270 
272  //
275  void setFlagsAll(int setTrue, int setFalse);
276 
278  void clear();
279 
281  size_t size() const {
282  return _props.size();
283  }
284 
286  //
289  void dump();
290 
292  //
295  void setReachable() const {
296  std::for_each(_props.begin(), _props.end(),
297  boost::mem_fn(&Property::setReachable));
298  }
299 
300 private:
301 
302  container _props;
303 
304  as_object& _owner;
305 
306 };
307 
308 
309 } // namespace gnash
310 
311 #endif // GNASH_PROPERTYLIST_H