Panda3D

eventParameter.I
Go to the documentation of this file.
1 /**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file eventParameter.I
10 * @author drose
11 * @date 1999-02-08
12 */
13
14 /**
15 * Defines an EventParameter that stores a pointer to any kind of
16 * TypedWritableReferenceCount object. This is the most general constructor.
17 *
18 * This accepts a const pointer, even though it stores (and eventually
19 * returns) a non-const pointer. This is just the simplest way to allow both
20 * const and non-const pointers to be stored, but it does lose the constness.
21 * Be careful.
22 */
23 INLINE EventParameter::
24 EventParameter(const TypedWritableReferenceCount *ptr) : _ptr((TypedWritableReferenceCount *)ptr) { }
25
26 /**
27 * Defines an EventParameter that stores a pointer to a TypedReferenceCount
28 * object. Note that a TypedReferenceCount is not the same kind of pointer as
29 * a TypedWritableReferenceCount, hence we require both constructors.
30 *
31 * This accepts a const pointer, even though it stores (and eventually
32 * returns) a non-const pointer. This is just the simplest way to allow both
33 * const and non-const pointers to be stored, but it does lose the constness.
34 * Be careful.
35 */
36 INLINE EventParameter::
37 EventParameter(const TypedReferenceCount *ptr) : _ptr(new EventStoreTypedRefCount((TypedReferenceCount *)ptr)) { }
38
39
40 /**
41 * Defines an EventParameter that stores an integer value.
42 */
43 INLINE EventParameter::
44 EventParameter(int value) : _ptr(new EventStoreInt(value)) { }
45
46
47 /**
48 * Defines an EventParameter that stores a floating-point value.
49 */
50 INLINE EventParameter::
51 EventParameter(double value) : _ptr(new EventStoreDouble(value)) { }
52
53
54 /**
55 * Defines an EventParameter that stores a string value.
56 */
57 INLINE EventParameter::
58 EventParameter(const std::string &value) : _ptr(new EventStoreString(value)) { }
59
60 /**
61 * Defines an EventParameter that stores a wstring value.
62 */
63 INLINE EventParameter::
64 EventParameter(const std::wstring &value) : _ptr(new EventStoreWstring(value)) { }
65
66
67 /**
68 *
69 */
70 INLINE EventParameter::
71 EventParameter(const EventParameter &other) : _ptr(other._ptr) { }
72
73
74 /**
75 *
76 */
77 INLINE EventParameter &EventParameter::
78 operator = (const EventParameter &other) {
79 _ptr = other._ptr;
80 return *this;
81 }
82
83 /**
84 *
85 */
86 INLINE EventParameter::
87 ~EventParameter() {
88 }
89
90 /**
91 * Returns true if the EventParameter is the empty parameter, storing nothing,
92 * or false otherwise.
93 */
94 INLINE bool EventParameter::
95 is_empty() const {
96 return (_ptr == nullptr);
97 }
98
99 /**
100 * Returns true if the EventParameter stores an integer value, false
101 * otherwise.
102 */
103 INLINE bool EventParameter::
104 is_int() const {
105 if (is_empty()) {
106 return false;
107 }
108 return _ptr->is_of_type(EventStoreInt::get_class_type());
109 }
110
111 /**
112 * Retrieves the value stored in the EventParameter. It is only valid to call
113 * this if is_int() has already returned true.
114 */
115 INLINE int EventParameter::
116 get_int_value() const {
117 nassertr(is_int(), 0);
118 // We can't use DCAST, because EventStoreValue::init_type() breaks
119 // convention and takes a parameter. But the above assertion should protect
120 // us.
121 return ((const EventStoreInt *)_ptr.p())->get_value();
122 }
123
124 /**
125 * Returns true if the EventParameter stores a double floating-point value,
126 * false otherwise.
127 */
128 INLINE bool EventParameter::
129 is_double() const {
130 if (is_empty()) {
131 return false;
132 }
133 return _ptr->is_of_type(EventStoreDouble::get_class_type());
134 }
135
136 /**
137 * Retrieves the value stored in the EventParameter. It is only valid to call
138 * this if is_double() has already returned true.
139 */
140 INLINE double EventParameter::
141 get_double_value() const {
142 nassertr(is_double(), 0.0);
143 return ((const EventStoreDouble *)_ptr.p())->get_value();
144 }
145
146 /**
147 * Returns true if the EventParameter stores a string value, false otherwise.
148 */
149 INLINE bool EventParameter::
150 is_string() const {
151 if (is_empty()) {
152 return false;
153 }
154 return _ptr->is_of_type(EventStoreString::get_class_type());
155 }
156
157 /**
158 * Retrieves the value stored in the EventParameter. It is only valid to call
159 * this if is_string() has already returned true.
160 */
161 INLINE std::string EventParameter::
162 get_string_value() const {
163 nassertr(is_string(), "");
164 return ((const EventStoreString *)_ptr.p())->get_value();
165 }
166
167 /**
168 * Returns true if the EventParameter stores a wstring value, false otherwise.
169 */
170 INLINE bool EventParameter::
171 is_wstring() const {
172 if (is_empty()) {
173 return false;
174 }
175 return _ptr->is_of_type(EventStoreWstring::get_class_type());
176 }
177
178 /**
179 * Retrieves the value stored in the EventParameter. It is only valid to call
180 * this if is_wstring() has already returned true.
181 */
182 INLINE std::wstring EventParameter::
183 get_wstring_value() const {
184 nassertr(is_wstring(), std::wstring());
185 return ((const EventStoreWstring *)_ptr.p())->get_value();
186 }
187
188 /**
189 * Returns true if the EventParameter stores a TypedReferenceCount pointer,
190 * false otherwise. Note that a TypedReferenceCount is not exactly the same
191 * kind of pointer as a TypedWritableReferenceCount, hence the need for this
192 * separate call.
193 */
194 INLINE bool EventParameter::
195 is_typed_ref_count() const {
196 if (is_empty()) {
197 return false;
198 }
199 return _ptr->is_of_type(EventStoreTypedRefCount::get_class_type());
200 }
201
202 /**
203 * Retrieves the value stored in the EventParameter. It is only valid to call
204 * this if is_typed_ref_count() has already returned true.
205 */
206 INLINE TypedReferenceCount *EventParameter::
207 get_typed_ref_count_value() const {
208 nassertr(is_typed_ref_count(), nullptr);
209 return ((const EventStoreTypedRefCount *)_ptr.p())->get_value();
210 }
211
212 /**
213 * Retrieves a pointer to the actual value stored in the parameter. The
214 * TypeHandle of this pointer may be examined to determine the actual type of
215 * parameter it contains. This is the only way to retrieve the value when it
216 * is not one of the above predefined types.
217 */
218 INLINE TypedWritableReferenceCount *EventParameter::
219 get_ptr() const {
220 return _ptr;
221 }
222
223 INLINE std::ostream &
224 operator << (std::ostream &out, const EventParameter &param) {
225 param.output(out);
226 return out;
227 }
EventParameter
An optional parameter associated with an event.
Definition: eventParameter.h:35
EventParameter::get_int_value
int get_int_value() const
Retrieves the value stored in the EventParameter.
Definition: eventParameter.I:116
EventParameter::is_double
bool is_double() const
Returns true if the EventParameter stores a double floating-point value, false otherwise.
Definition: eventParameter.I:129
EventParameter::get_wstring_value
std::wstring get_wstring_value() const
Retrieves the value stored in the EventParameter.
Definition: eventParameter.I:183
EventParameter::is_empty
bool is_empty() const
Returns true if the EventParameter is the empty parameter, storing nothing, or false otherwise.
Definition: eventParameter.I:95
EventParameter::is_string
bool is_string() const
Returns true if the EventParameter stores a string value, false otherwise.
Definition: eventParameter.I:150
EventParameter::get_typed_ref_count_value
TypedReferenceCount * get_typed_ref_count_value() const
Retrieves the value stored in the EventParameter.
Definition: eventParameter.I:207
EventParameter::get_ptr
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
Definition: eventParameter.I:219
EventParameter::is_typed_ref_count
bool is_typed_ref_count() const
Returns true if the EventParameter stores a TypedReferenceCount pointer, false otherwise.
Definition: eventParameter.I:195
EventParameter::is_int
bool is_int() const
Returns true if the EventParameter stores an integer value, false otherwise.
Definition: eventParameter.I:104
EventParameter::get_string_value
std::string get_string_value() const
Retrieves the value stored in the EventParameter.
Definition: eventParameter.I:162
EventParameter::is_wstring
bool is_wstring() const
Returns true if the EventParameter stores a wstring value, false otherwise.
Definition: eventParameter.I:171
EventParameter::get_double_value
double get_double_value() const
Retrieves the value stored in the EventParameter.
Definition: eventParameter.I:141
ParamTypedRefCount
A class object for storing specifically objects of type TypedReferenceCount, which is different than ...
Definition: paramValue.h:62
ParamValue
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
TypedReferenceCount
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
Definition: typedReferenceCount.h:31
TypedWritableReferenceCount
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
Definition: typedWritableReferenceCount.h:31