Panda3D
Loading...
Searching...
No Matches
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 */
23INLINE EventParameter::
24EventParameter(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 */
36INLINE EventParameter::
37EventParameter(const TypedReferenceCount *ptr) : _ptr(new EventStoreTypedRefCount((TypedReferenceCount *)ptr)) { }
38
39
40/**
41 * Defines an EventParameter that stores an integer value.
42 */
43INLINE EventParameter::
44EventParameter(int value) : _ptr(new EventStoreInt(value)) { }
45
46
47/**
48 * Defines an EventParameter that stores a floating-point value.
49 */
50INLINE EventParameter::
51EventParameter(double value) : _ptr(new EventStoreDouble(value)) { }
52
53
54/**
55 * Defines an EventParameter that stores a string value.
56 */
57INLINE EventParameter::
58EventParameter(const std::string &value) : _ptr(new EventStoreString(value)) { }
59
60/**
61 * Defines an EventParameter that stores a wstring value.
62 */
63INLINE EventParameter::
64EventParameter(const std::wstring &value) : _ptr(new EventStoreWstring(value)) { }
65
66
67/**
68 *
69 */
70INLINE EventParameter::
71EventParameter(const EventParameter &other) : _ptr(other._ptr) { }
72
73
74/**
75 *
76 */
77INLINE EventParameter &EventParameter::
78operator = (const EventParameter &other) {
79 _ptr = other._ptr;
80 return *this;
81}
82
83/**
84 *
85 */
86INLINE EventParameter::
87~EventParameter() {
88}
89
90/**
91 * Returns true if the EventParameter is the empty parameter, storing nothing,
92 * or false otherwise.
93 */
95is_empty() const {
96 return (_ptr == nullptr);
97}
98
99/**
100 * Returns true if the EventParameter stores an integer value, false
101 * otherwise.
102 */
104is_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 */
116get_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 */
129is_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 */
140INLINE double EventParameter::
141get_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 */
150is_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 */
161INLINE std::string EventParameter::
162get_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 */
171is_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 */
182INLINE std::wstring EventParameter::
183get_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 */
195is_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 */
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 */
219get_ptr() const {
220 return _ptr;
221}
222
223INLINE std::ostream &
224operator << (std::ostream &out, const EventParameter &param) {
225 param.output(out);
226 return out;
227}
An optional parameter associated with an event.
int get_int_value() const
Retrieves the value stored in the EventParameter.
bool is_double() const
Returns true if the EventParameter stores a double floating-point value, false otherwise.
std::wstring get_wstring_value() const
Retrieves the value stored in the EventParameter.
bool is_empty() const
Returns true if the EventParameter is the empty parameter, storing nothing, or false otherwise.
bool is_string() const
Returns true if the EventParameter stores a string value, false otherwise.
TypedReferenceCount * get_typed_ref_count_value() const
Retrieves the value stored in the EventParameter.
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
bool is_typed_ref_count() const
Returns true if the EventParameter stores a TypedReferenceCount pointer, false otherwise.
bool is_int() const
Returns true if the EventParameter stores an integer value, false otherwise.
std::string get_string_value() const
Retrieves the value stored in the EventParameter.
bool is_wstring() const
Returns true if the EventParameter stores a wstring value, false otherwise.
double get_double_value() const
Retrieves the value stored in the EventParameter.
A class object for storing specifically objects of type TypedReferenceCount, which is different than ...
Definition paramValue.h:62
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition paramValue.h:103
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.