Fix test for bug #32625
[gnash.git] / libcore / CharacterProxy.h
blob7a78b63e20292957846dc39f04efd5fb995eb306
1 // CharacterProxy.h - rebindable DisplayObject reference, for Gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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 <cassert>
25 #include "dsodefs.h"
27 // Forward declarations
28 namespace gnash {
29 class DisplayObject;
30 class movie_root;
33 namespace gnash {
35 DisplayObject* findDisplayObjectByTarget(const std::string& target,
36 movie_root& mr);
38 /// A proxy for DisplayObject pointers.
40 /// The proxy will store a pointer to a DisplayObject until the
41 /// DisplayObject is destroyed, in which case it will only store the original
42 /// target path of it and always use that for rebinding when needed.
43 ///
44 class CharacterProxy
46 public:
48 /// Construct a CharacterProxy pointing to the given sprite
49 CharacterProxy(DisplayObject* sp, movie_root& mr)
51 _ptr(sp),
52 _mr(&mr)
54 checkDangling();
57 /// Construct a copy of the given CharacterProxy
59 /// @param sp
60 /// The CharacterProxy to make a copy of.
61 /// NOTE: if the given proxy is dangling, this proxy
62 /// will also be dangling. If you want to
63 /// create a non-dangling proxy you can
64 /// use the constructor taking a DisplayObject
65 /// as in CharacterProxy newProxy(oldProxy.get())
66 ///
67 CharacterProxy(const CharacterProxy& sp)
69 _mr(sp._mr)
71 sp.checkDangling();
72 _ptr = sp._ptr;
73 if (!_ptr) _tgt = sp._tgt;
76 /// Make this proxy a copy of the given one
78 /// @param sp
79 /// The CharacterProxy to make a copy of.
80 /// NOTE: if the given proxy is dangling, this proxy
81 /// will also be dangling. If you want to
82 /// create a non-dangling proxy you can
83 /// use the constructor taking a DisplayObject
84 /// as in CharacterProxy newProxy(oldProxy.get())
85 CharacterProxy& operator=(const CharacterProxy& sp)
87 sp.checkDangling();
88 _ptr = sp._ptr;
89 if (!_ptr) _tgt = sp._tgt;
90 _mr = sp._mr;
91 return *this;
94 /// Get the pointed sprite, either original or rebound
96 /// @return the currently bound sprite, NULL if none
97 ///
98 DisplayObject* get(bool skipRebinding = false) const
100 if (skipRebinding) return _ptr;
102 // set _ptr to NULL and _tgt to original target if destroyed
103 checkDangling();
104 if (_ptr) return _ptr;
105 return findDisplayObjectByTarget(_tgt, *_mr);
108 /// Get the sprite target, either current (if not dangling) or
109 /// bound one.
110 std::string getTarget() const;
112 /// Return true if this sprite is dangling
114 /// Dangling means that it doesn't have a pointer to the original
115 /// sprite anymore, not that it doesn't point to anything.
116 /// To know if it points to something or not use get(), which will
117 /// return NULL if it doesn't point to anyhing.
119 bool isDangling() const
121 checkDangling();
122 return !_ptr;
125 /// \brief
126 /// Two sprite_proxies are equal if they point to the
127 /// same sprite
129 bool operator==(const CharacterProxy& sp) const
131 return get() == sp.get();
134 /// Set the original sprite (if any) as reachable
136 /// NOTE: if this value is dangling, we won't keep anything
137 /// alive.
139 void setReachable() const;
141 private:
143 /// If we still have a sprite pointer check if it was destroyed
144 /// in which case we drop the pointer and only keep the target.
145 DSOEXPORT void checkDangling() const;
147 mutable DisplayObject* _ptr;
149 mutable std::string _tgt;
151 movie_root* _mr;
155 } // end namespace gnash
157 #endif // GNASH_CHARACTER_PROXY_H