putil: Support vector_uchar to be passed to an event parameter

This commit is contained in:
rdb 2025-12-31 23:01:43 +01:00
parent c688b67e5a
commit cb03554da5
11 changed files with 83 additions and 5 deletions

View File

@ -76,6 +76,8 @@ class EventManager:
return eventParameter.getStringValue()
elif eventParameter.isWstring():
return eventParameter.getWstringValue()
elif hasattr(eventParameter, 'isBytes') and eventParameter.isBytes():
return eventParameter.getBytesValue()
elif eventParameter.isTypedRefCount():
return eventParameter.getTypedRefCountValue()
elif eventParameter.isEmpty():

View File

@ -46,15 +46,15 @@ public:
typedef std::vector<Type, allocator> base_class;
typedef typename base_class::size_type size_type;
explicit pvector(TypeHandle type_handle = pvector_type_handle) : base_class(allocator(type_handle)) { }
explicit pvector(TypeHandle type_handle = get_type_handle(pvector<Type>)) : base_class(allocator(type_handle)) { }
pvector(const pvector<Type> &copy) : base_class(copy) { }
pvector(pvector<Type> &&from) noexcept : base_class(std::move(from)) {};
explicit pvector(size_type n, TypeHandle type_handle = pvector_type_handle) : base_class(n, Type(), allocator(type_handle)) { }
explicit pvector(size_type n, const Type &value, TypeHandle type_handle = pvector_type_handle) : base_class(n, value, allocator(type_handle)) { }
pvector(const Type *begin, const Type *end, TypeHandle type_handle = pvector_type_handle) : base_class(allocator(type_handle)) {
explicit pvector(size_type n, TypeHandle type_handle = get_type_handle(pvector<Type>)) : base_class(n, Type(), allocator(type_handle)) { }
explicit pvector(size_type n, const Type &value, TypeHandle type_handle = get_type_handle(pvector<Type>)) : base_class(n, value, allocator(type_handle)) { }
pvector(const Type *begin, const Type *end, TypeHandle type_handle = get_type_handle(pvector<Type>)) : base_class(allocator(type_handle)) {
this->insert(this->end(), begin, end);
}
pvector(std::initializer_list<Type> init, TypeHandle type_handle = pvector_type_handle) : base_class(std::move(init), allocator(type_handle)) { }
pvector(std::initializer_list<Type> init, TypeHandle type_handle = get_type_handle(pvector<Type>)) : base_class(std::move(init), allocator(type_handle)) { }
pvector<Type> &operator =(const pvector<Type> &copy) {
base_class::operator =(copy);

View File

@ -26,6 +26,7 @@ TypeHandle double_type_handle;
TypeHandle float_type_handle;
TypeHandle string_type_handle;
TypeHandle wstring_type_handle;
TypeHandle vector_uchar_type_handle;
TypeHandle long_p_type_handle;
TypeHandle int_p_type_handle;
@ -59,6 +60,7 @@ void init_system_type_handles() {
register_type(float_type_handle, "float");
register_type(string_type_handle, "string");
register_type(wstring_type_handle, "wstring");
register_type(vector_uchar_type_handle, "vector_uchar");
register_type(int_p_type_handle, "int*");
register_type(short_p_type_handle, "short*");

View File

@ -19,6 +19,9 @@
#include "typeHandle.h"
#include "typeRegistry.h"
template<class T>
class pvector;
/**
* This inline function is just a convenient way to call
* TypeRegistry::register_type(), along with zero to four
@ -88,6 +91,7 @@ extern TypeHandle EXPCL_DTOOL_DTOOLBASE double_type_handle;
extern TypeHandle EXPCL_DTOOL_DTOOLBASE float_type_handle;
extern TypeHandle EXPCL_DTOOL_DTOOLBASE string_type_handle;
extern TypeHandle EXPCL_DTOOL_DTOOLBASE wstring_type_handle;
extern TypeHandle EXPCL_DTOOL_DTOOLBASE vector_uchar_type_handle;
extern TypeHandle long_p_type_handle;
extern TypeHandle int_p_type_handle;
@ -175,6 +179,16 @@ INLINE TypeHandle _get_type_handle(const std::wstring *) {
return wstring_type_handle;
}
template<class T>
INLINE TypeHandle _get_type_handle(const pvector<T> *) {
return pvector_type_handle;
}
template<>
INLINE TypeHandle _get_type_handle(const pvector<unsigned char> *) {
return vector_uchar_type_handle;
}
template<>
INLINE TypeHandle _get_type_handle(const long * const *) {
return long_p_type_handle;

View File

@ -19,3 +19,25 @@
#define NAME vector_uchar
#include "vector_src.cxx"
/**
* Writes a hex representation of the bytes to the output stream.
*/
std::ostream &
operator << (std::ostream &out, const vector_uchar &data) {
static const char nibble_to_hex[] = "0123456789abcdef";
if (data.empty()) {
return out;
}
out << nibble_to_hex[(data[0] & 0xf0) >> 4];
out << nibble_to_hex[data[0] & 0xf];
for (size_t i = 1; i < data.size(); ++i) {
out << ' ';
out << nibble_to_hex[(data[i] & 0xf0) >> 4];
out << nibble_to_hex[data[i] & 0xf];
}
return out;
}

View File

@ -30,4 +30,7 @@
#include "vector_src.h"
EXPCL_DTOOL_DTOOLUTIL std::ostream &
operator << (std::ostream &out, const vector_uchar &data);
#endif

View File

@ -63,6 +63,11 @@ EventParameter(const std::string &value) : _ptr(new EventStoreString(value)) { }
INLINE EventParameter::
EventParameter(const std::wstring &value) : _ptr(new EventStoreWstring(value)) { }
/**
* Defines an EventParameter that stores a bytes value.
*/
INLINE EventParameter::
EventParameter(const vector_uchar &value) : _ptr(new ParamBytes(value)) { }
/**
*
@ -185,6 +190,28 @@ get_wstring_value() const {
return ((const EventStoreWstring *)_ptr.p())->get_value();
}
/**
* Returns true if the EventParameter stores a binary bytes value, false
* otherwise.
*/
INLINE bool EventParameter::
is_bytes() const {
if (is_empty()) {
return false;
}
return _ptr->is_of_type(ParamBytes::get_class_type());
}
/**
* Retrieves the value stored in the EventParameter. It is only valid to call
* this if is_bytes() has already returned true.
*/
INLINE vector_uchar EventParameter::
get_bytes_value() const {
nassertr(is_bytes(), vector_uchar());
return ((const ParamBytes *)_ptr.p())->get_value();
}
/**
* Returns true if the EventParameter stores a TypedReferenceCount pointer,
* false otherwise. Note that a TypedReferenceCount is not exactly the same

View File

@ -41,6 +41,7 @@ PUBLISHED:
INLINE EventParameter(double value);
INLINE EventParameter(const std::string &value);
INLINE EventParameter(const std::wstring &value);
INLINE EventParameter(const vector_uchar &value);
INLINE EventParameter(const EventParameter &copy);
INLINE EventParameter &operator = (const EventParameter &copy);
@ -59,6 +60,8 @@ PUBLISHED:
INLINE std::string get_string_value() const;
INLINE bool is_wstring() const;
INLINE std::wstring get_wstring_value() const;
INLINE bool is_bytes() const;
INLINE vector_uchar get_bytes_value() const;
INLINE bool is_typed_ref_count() const;
INLINE TypedReferenceCount *get_typed_ref_count_value() const;

View File

@ -212,6 +212,7 @@ init_libputil() {
FactoryParam::init_type();
Namable::init_type();
NodeCachedReferenceCount::init_type();
ParamBytes::init_type("ParamBytes");
ParamMatrix3d::init_type("ParamMatrix3d");
ParamMatrix3f::init_type("ParamMatrix3f");
ParamMatrix4d::init_type("ParamMatrix4d");

View File

@ -16,6 +16,7 @@
template class ParamValue<std::string>;
template class ParamValue<std::wstring>;
template class ParamValue<vector_uchar>;
template class ParamValue<LVecBase2d>;
template class ParamValue<LVecBase2f>;

View File

@ -22,6 +22,7 @@
#include "bamReader.h"
#include "bamWriter.h"
#include "luse.h"
#include "vector_uchar.h"
/**
* A non-template base class of ParamValue (below), which serves mainly to
@ -152,6 +153,7 @@ private:
EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<std::string>);
EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<std::wstring>);
EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<vector_uchar>);
EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<LVecBase2d>);
EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<LVecBase2f>);
@ -173,6 +175,7 @@ EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, ParamValue<LMatrix4f
typedef ParamValue<std::string> ParamString;
typedef ParamValue<std::wstring> ParamWstring;
typedef ParamValue<vector_uchar> ParamBytes;
typedef ParamValue<LVecBase2d> ParamVecBase2d;
typedef ParamValue<LVecBase2f> ParamVecBase2f;