Update with current status
[gnash.git] / libcore / CharacterProxy.h
blobe4264da9b69cf9c64f2733776f8ac4b614a2c2b6
1 // CharacterProxy.h - rebindable DisplayObject reference, 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
20 #ifndef GNASH_CHARACTER_PROXY_H
21 #define GNASH_CHARACTER_PROXY_H
23 #include <string>
24 #include "dsodefs.h"
26 // Forward declarations
27 namespace gnash {
28 class DisplayObject;
29 class movie_root;
32 namespace gnash {
34 DisplayObject* findDisplayObjectByTarget(const std::string& target,
35 movie_root& mr);
37 /// A proxy for DisplayObject pointers.
39 /// The proxy will store a pointer to a DisplayObject until the
40 /// DisplayObject is destroyed, in which case it will only store the original
41 /// target path of it and always use that for rebinding when needed.
42 ///
43 class CharacterProxy
45 public:
47 /// Construct a CharacterProxy pointing to the given sprite
48 CharacterProxy(DisplayObject* sp, movie_root& mr)
50 _ptr(sp),
51 _mr(&mr)
53 checkDangling();
56 /// Construct a copy of the given CharacterProxy
58 /// @param sp
59 /// The CharacterProxy to make a copy of.
60 /// NOTE: if the given proxy is dangling, this proxy
61 /// will also be dangling. If you want to
62 /// create a non-dangling proxy you can
63 /// use the constructor taking a DisplayObject
64 /// as in CharacterProxy newProxy(oldProxy.get())
65 ///
66 CharacterProxy(const CharacterProxy& sp)
68 _mr(sp._mr)
70 sp.checkDangling();
71 _ptr = sp._ptr;
72 if (!_ptr) _tgt = sp._tgt;
75 /// Make this proxy a copy of the given one
77 /// @param sp
78 /// The CharacterProxy to make a copy of.
79 /// NOTE: if the given proxy is dangling, this proxy
80 /// will also be dangling. If you want to
81 /// create a non-dangling proxy you can
82 /// use the constructor taking a DisplayObject
83 /// as in CharacterProxy newProxy(oldProxy.get())
84 CharacterProxy& operator=(const CharacterProxy& sp)
86 sp.checkDangling();
87 _ptr = sp._ptr;
88 if (!_ptr) _tgt = sp._tgt;
89 _mr = sp._mr;
90 return *this;
93 /// Get the pointed sprite, either original or rebound
95 /// @return the currently bound sprite, NULL if none
96 ///
97 DisplayObject* get(bool skipRebinding = false) const
99 if (skipRebinding) return _ptr;
101 // set _ptr to NULL and _tgt to original target if destroyed
102 checkDangling();
103 if (_ptr) return _ptr;
104 return findDisplayObjectByTarget(_tgt, *_mr);
107 /// Get the sprite target, either current (if not dangling) or
108 /// bound one.
109 std::string getTarget() const;
111 /// Return true if this sprite is dangling
113 /// Dangling means that it doesn't have a pointer to the original
114 /// sprite anymore, not that it doesn't point to anything.
115 /// To know if it points to something or not use get(), which will
116 /// return NULL if it doesn't point to anyhing.
118 bool isDangling() const
120 checkDangling();
121 return !_ptr;
124 /// \brief
125 /// Two sprite_proxies are equal if they point to the
126 /// same sprite
128 bool operator==(const CharacterProxy& sp) const
130 return get() == sp.get();
133 /// Set the original sprite (if any) as reachable
135 /// NOTE: if this value is dangling, we won't keep anything
136 /// alive.
138 void setReachable() const;
140 private:
142 /// If we still have a sprite pointer check if it was destroyed
143 /// in which case we drop the pointer and only keep the target.
144 DSOEXPORT void checkDangling() const;
146 mutable DisplayObject* _ptr;
148 mutable std::string _tgt;
150 movie_root* _mr;
154 } // end namespace gnash
156 #endif // GNASH_CHARACTER_PROXY_H