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_AS_PROP_FLAGS_H
20 #define GNASH_AS_PROP_FLAGS_H
21 
22 #include <ostream>
23 #include <cstdint>
24 
25 namespace gnash {
26 
27 /// Flags defining the level of protection of a member
28 class PropFlags
29 {
30 public:
31 
32 	/// Actual flags
33 	enum Flags {
34 
35 		/// Protect from enumeration
36 		dontEnum	= 1 << 0, // 1
37 
38 		/// Protect from deletion
39 		dontDelete	= 1 << 1, // 2
40 
41 		/// Protect from assigning a value
42 		readOnly	= 1 << 2, // 4
43 
44 		/// Only visible by VM initialized for version 6 or higher
45 		onlySWF6Up 	= 1 << 7, // 128
46 
47 		/// Ignore in SWF6-initialized VM
48 		ignoreSWF6	= 1 << 8, // 256
49 
50 		/// Only visible by VM initialized for version 7 or higher
51 		onlySWF7Up 	= 1 << 10, // 1024
52 
53 		/// Only visible by VM initialized for version 8 or higher
54 		onlySWF8Up	= 1 << 12, // 4096
55 
56 		/// Only visible by VM initialized for version 9 or higher
57 		onlySWF9Up	= 1 << 13 // 8192
58 
59 	};
60 
61 	/// Default constructor
PropFlags()62 	PropFlags()
63         :
64         _flags(0)
65 	{
66 	}
67 
68 	/// Constructor
PropFlags(const bool read_only,const bool dont_delete,const bool dont_enum)69 	PropFlags(const bool read_only, const bool dont_delete,
70             const bool dont_enum)
71 		:
72 		_flags(((read_only) ? readOnly : 0) |
73 				((dont_delete) ? dontDelete : 0) |
74 				((dont_enum) ? dontEnum : 0))
75 	{
76 	}
77 
78 	/// Constructor, from numerical value
PropFlags(std::uint16_t flags)79 	PropFlags(std::uint16_t flags)
80 		:
81         _flags(flags)
82 	{
83 	}
84 
85 	bool operator==(const PropFlags& o) const {
86 		return (_flags == o._flags);
87 	}
88 
89 	bool operator!=(const PropFlags& o) const {
90         return !(*this == o);
91 	}
92 
93     template<Flags f>
test()94     bool test() const {
95         return (_flags & f);
96     }
97 
98 	/// Get version-based visibility
get_visible(int swfVersion)99 	bool get_visible(int swfVersion) const {
100 		if (test<onlySWF6Up>() && swfVersion < 6) return false;
101 		if (test<ignoreSWF6>() && swfVersion == 6) return false;
102 		if (test<onlySWF7Up>() && swfVersion < 7) return false;
103 		if (test<onlySWF8Up>() && swfVersion < 8) return false;
104 		if (test<onlySWF9Up>() && swfVersion < 9) return false;
105 		return true;
106 	}
107 
clear_visible(int swfVersion)108 	void clear_visible(int swfVersion) {
109 		if (swfVersion == 6) {
110 			// version 6, so let's forget onlySWF7Up flag!
111 			// we will still set the value though, even if that flag is set
112 			_flags &= ~(onlySWF6Up|ignoreSWF6|onlySWF8Up|onlySWF9Up);
113 		}
114 		else {
115 			_flags &= ~(onlySWF6Up|ignoreSWF6|onlySWF7Up|onlySWF8Up|onlySWF9Up);
116 		}
117 	}
118 
119 	/// accessor to the numerical flags value
get_flags()120     std::uint16_t get_flags() const { return _flags; }
121 
122 	/// set the numerical flags value (return the new value )
123 	/// If unlocked is false, you cannot un-protect from over-write,
124 	/// you cannot un-protect from deletion and you cannot
125 	/// un-hide from the for..in loop construct
126 	///
127 	/// @param setTrue  the set of flags to set
128 	/// @param setFalse the set of flags to clear
129 	/// @return         true on success, false on failure (is protected)
130 	bool set_flags(std::uint16_t setTrue, std::uint16_t setFalse = 0) {
131 		_flags &= ~setFalse;
132 		_flags |= setTrue;
133 		return true;
134 	}
135 
136 private:
137 
138 	/// Numeric flags
139     std::uint16_t _flags;
140 
141 };
142 
143 inline std::ostream&
144 operator<<(std::ostream& os, const PropFlags& fl)
145 {
146 	os << "(";
147 	if (fl.test<PropFlags::readOnly>()) os << " readonly";
148 	if (fl.test<PropFlags::dontDelete>()) os << " nodelete";
149 	if (fl.test<PropFlags::dontEnum>()) os << " noenum";
150 	os << " )";
151 	return os;
152 }
153 
154 
155 
156 } // namespace gnash
157 
158 #endif // GNASH_AS_PROP_FLAGS_H
159