diff --git a/panda/src/express/config_express.N b/panda/src/express/config_express.N index 4756f75635..8bf5c3f0bc 100644 --- a/panda/src/express/config_express.N +++ b/panda/src/express/config_express.N @@ -33,3 +33,9 @@ forcetype ConfigVariableString forcetype istream forcetype ostream forcetype iostream + +forcetype PointerToArray +forcetype ConstPointerToArray + +renametype PointerToArray PTAUchar +renametype ConstPointerToArray CPTAUchar diff --git a/panda/src/express/pointerToArray.I b/panda/src/express/pointerToArray.I index cff5ca488b..96818490d6 100644 --- a/panda/src/express/pointerToArray.I +++ b/panda/src/express/pointerToArray.I @@ -26,7 +26,7 @@ pvector ConstPointerToArray::_empty_array; //////////////////////////////////////////////////////////////////// // Function: PointerToArray::Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -39,7 +39,7 @@ PointerToArray(TypeHandle type_handle) : //////////////////////////////////////////////////////////////////// // Function: PointerToArray::empty_array -// Access: Published, Static +// Access: Public, Static // Description: Return an empty array of size n //////////////////////////////////////////////////////////////////// template @@ -55,7 +55,7 @@ PointerToArray::empty_array(size_type n, TypeHandle type_handle) { //////////////////////////////////////////////////////////////////// // Function: PointerToArray::Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -70,7 +70,7 @@ PointerToArray(size_type n, const Element &value, TypeHandle type_handle) : //////////////////////////////////////////////////////////////////// // Function: PointerToArray::Copy Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -312,7 +312,7 @@ erase(iterator first, iterator last) { #if !defined(WIN32_VC) //////////////////////////////////////////////////////////////////// // Function: PointerToArray::Indexing operator -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -330,7 +330,7 @@ operator [](size_type n) const { //////////////////////////////////////////////////////////////////// // Function: PointerToArray::Indexing operator -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -340,40 +340,9 @@ operator [](int n) const { } #endif -//////////////////////////////////////////////////////////////////// -// Function: PointerToArray::get_element -// Access: Published -// Description: This method exists mainly to access the elements of -// the array easily from a high-level language such as -// Python, especially on Windows, where the above index -// element accessor methods can't be defined because of -// a confusion with the pointer typecast operator. -//////////////////////////////////////////////////////////////////// -template -INLINE const Element &PointerToArray:: -get_element(size_type n) const { - return (*this)[n]; -} - -//////////////////////////////////////////////////////////////////// -// Function: PointerToArray::set_element -// Access: Published -// Description: This method exists mainly to access the elements of -// the array easily from a high-level language such as -// Python, especially on Windows, where the above index -// element accessor methods can't be defined because of -// a confusion with the pointer typecast operator. -//////////////////////////////////////////////////////////////////// -template -INLINE void PointerToArray:: -set_element(size_type n, const Element &value) { - nassertv(n < ((To *)(this->_void_ptr))->size()); - (*this)[n] = value; -} - //////////////////////////////////////////////////////////////////// // Function: PointerToArray::push_back -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -387,7 +356,7 @@ push_back(const Element &x) { //////////////////////////////////////////////////////////////////// // Function: PointerToArray::pop_back -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -402,7 +371,7 @@ pop_back() { //////////////////////////////////////////////////////////////////// // Function: PointerToArray::make_empty -// Access: Published +// Access: Public // Description: Empties the array pointed to. This is different from // clear(), which reassigns the pointer to a NULL // pointer. @@ -462,6 +431,123 @@ v() const { return *((To *)(this->_void_ptr)); } +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::get_element +// Access: Published +// Description: This method exists mainly to access the elements of +// the array easily from a high-level language such as +// Python, especially on Windows, where the above index +// element accessor methods can't be defined because of +// a confusion with the pointer typecast operator. +//////////////////////////////////////////////////////////////////// +template +INLINE const Element &PointerToArray:: +get_element(size_type n) const { + return (*this)[n]; +} + +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::set_element +// Access: Published +// Description: This method exists mainly to access the elements of +// the array easily from a high-level language such as +// Python, especially on Windows, where the above index +// element accessor methods can't be defined because of +// a confusion with the pointer typecast operator. +//////////////////////////////////////////////////////////////////// +template +INLINE void PointerToArray:: +set_element(size_type n, const Element &value) { + nassertv(n < ((To *)(this->_void_ptr))->size()); + (*this)[n] = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::get_data +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It returns the entire contents of the vector as a +// block of raw data in a string. +//////////////////////////////////////////////////////////////////// +template +INLINE string PointerToArray:: +get_data() const { + return get_subdata(0, size()); +} + +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::set_data +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It replaces the entire contents of the vector from a +// block of raw data in a string. +//////////////////////////////////////////////////////////////////// +template +INLINE void PointerToArray:: +set_data(const string &data) { + set_subdata(0, size(), data); +} + +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::get_subdata +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It returns the contents of a portion of the +// vector--from element (n) through element (n + count - +// 1)--as a block of raw data in a string. +//////////////////////////////////////////////////////////////////// +template +INLINE string PointerToArray:: +get_subdata(size_type n, size_type count) const { + n = min(n, size()); + count = max(count, n); + count = min(count, size() - n); + return string((const char *)(p() + n), sizeof(Element) * count); +} + +//////////////////////////////////////////////////////////////////// +// Function: PointerToArray::set_subdata +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It replaces the contents of a portion of the +// vector--from element (n) through element (n + count - +// 1)--as a block of raw data in a string. The length +// of the string must be an even multiple of Element +// size bytes. The array may be expanded or truncated +// if the length of the string does not correspond to +// exactly count elements. +//////////////////////////////////////////////////////////////////// +template +INLINE void PointerToArray:: +set_subdata(size_type n, size_type count, const string &data) { + nassertv((data.length() % sizeof(Element)) == 0); + size_type ncount = data.length() / sizeof(Element); + if (ncount < count) { + // Reduce the array. + erase(begin() + n + ncount, begin() + n + count); + } else if (count < ncount) { + // Expand the array. + insert(begin() + n + count, ncount - count, Element()); + } + + // Now boldly replace the data. Hope there aren't any constructors + // or destructors involved here. The user better know what she is + // doing. + memcpy(p() + n, data.data(), sizeof(Element) * ncount); +} + //////////////////////////////////////////////////////////////////// // Function: PointerToArray::get(this->_void_ptr) // Access: Public @@ -575,7 +661,7 @@ clear() { //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -588,7 +674,7 @@ ConstPointerToArray(TypeHandle type_handle) : //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Copy Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -601,7 +687,7 @@ ConstPointerToArray(const PointerToArray ©) : //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Copy Constructor -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -670,7 +756,7 @@ rend() const { //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::size -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -755,7 +841,7 @@ back() const { #ifndef WIN32_VC //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Indexing operator -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -773,7 +859,7 @@ operator [](size_type n) const { //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Indexing operator -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// template @@ -783,21 +869,6 @@ operator [](int n) const { } #endif -//////////////////////////////////////////////////////////////////// -// Function: ConstPointerToArray::get_element -// Access: Published -// Description: This method exists mainly to access the elements of -// the array easily from a high-level language such as -// Python, especially on Windows, where the above index -// element accessor methods can't be defined because of -// a confusion with the pointer typecast operator. -//////////////////////////////////////////////////////////////////// -template -INLINE const Element &ConstPointerToArray:: -get_element(size_type n) const { - return (*this)[n]; -} - //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::Typecast operator // Access: Public @@ -843,6 +914,57 @@ v() const { return *(To *)(this->_void_ptr); } +//////////////////////////////////////////////////////////////////// +// Function: ConstPointerToArray::get_element +// Access: Published +// Description: This method exists mainly to access the elements of +// the array easily from a high-level language such as +// Python, especially on Windows, where the above index +// element accessor methods can't be defined because of +// a confusion with the pointer typecast operator. +//////////////////////////////////////////////////////////////////// +template +INLINE const Element &ConstPointerToArray:: +get_element(size_type n) const { + return (*this)[n]; +} + +//////////////////////////////////////////////////////////////////// +// Function: ConstPointerToArray::get_data +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It returns the entire contents of the vector as a +// block of raw data in a string. +//////////////////////////////////////////////////////////////////// +template +INLINE string ConstPointerToArray:: +get_data() const { + return get_subdata(0, size()); +} + +//////////////////////////////////////////////////////////////////// +// Function: ConstPointerToArray::get_subdata +// Access: Published +// Description: This method exists mainly to access the data of +// the array easily from a high-level language such as +// Python. +// +// It returns the contents of a portion of the +// vector--from element (n) through element (n + count - +// 1)--as a block of raw data in a string. +//////////////////////////////////////////////////////////////////// +template +INLINE string ConstPointerToArray:: +get_subdata(size_type n, size_type count) const { + n = min(n, size()); + count = max(count, n); + count = min(count, size() - n); + return string((const char *)(p() + n), sizeof(Element) * count); +} + //////////////////////////////////////////////////////////////////// // Function: ConstPointerToArray::get_ref_count // Access: Public diff --git a/panda/src/express/pointerToArray.h b/panda/src/express/pointerToArray.h index 8b8e63e166..a25dbd46d8 100644 --- a/panda/src/express/pointerToArray.h +++ b/panda/src/express/pointerToArray.h @@ -102,9 +102,25 @@ class ConstPointerToArray; template class PointerToArray : public PointerToArrayBase { public: - // By hiding this template from interrogate, we improve compile-time - // speed and memory utilization. -#ifndef CPPPARSER + // By hiding this template from interrogate, we would improve + // compile-time speed and memory utilization. However, we do want + // to export a minimal subset of this class. So we define just the + // exportable interface here. +#ifdef CPPPARSER +PUBLISHED: + typedef TYPENAME pvector::size_type size_type; + INLINE PointerToArray(TypeHandle type_handle = get_type_handle(Element)); + INLINE PointerToArray(const PointerToArray ©); + INLINE size_type size() const; + INLINE const Element &get_element(size_type n) const; + INLINE void set_element(size_type n, const Element &value); + INLINE string get_data() const; + INLINE void set_data(const string &data); + INLINE string get_subdata(size_type n, size_type count) const; + INLINE void set_subdata(size_type n, size_type count, const string &data); + +#else // CPPPARSER + // This is the actual, complete interface. typedef TYPENAME PointerToArrayBase::To To; typedef TYPENAME pvector::value_type value_type; typedef TYPENAME pvector::reference reference; @@ -162,8 +178,6 @@ public: INLINE reference operator [](size_type n) const; INLINE reference operator [](int n) const; #endif - INLINE const Element &get_element(size_type n) const; - INLINE void set_element(size_type n, const Element &value); INLINE void push_back(const Element &x); INLINE void pop_back(); @@ -173,6 +187,14 @@ public: INLINE Element *p() const; INLINE pvector &v() const; + // Methods to help out Python and other high-level languages. + INLINE const Element &get_element(size_type n) const; + INLINE void set_element(size_type n, const Element &value); + INLINE string get_data() const; + INLINE void set_data(const string &data); + INLINE string get_subdata(size_type n, size_type count) const; + INLINE void set_subdata(size_type n, size_type count, const string &data); + //These functions are only to be used in Reading through BamReader. //They are designed to work in pairs, so that you register what is //returned by get_void_ptr with BamReader and when you are setting @@ -218,9 +240,20 @@ private: template class ConstPointerToArray : public PointerToArrayBase { public: - // By hiding this template from interrogate, we improve compile-time - // speed and memory utilization. -#ifndef CPPPARSER + // By hiding this template from interrogate, we would improve + // compile-time speed and memory utilization. However, we do want + // to export a minimal subset of this class. So we define just the + // exportable interface here. +#ifdef CPPPARSER +PUBLISHED: + typedef TYPENAME pvector::size_type size_type; + INLINE size_type size() const; + INLINE const Element &get_element(size_type n) const; + INLINE string get_data() const; + INLINE string get_subdata(size_type n, size_type count) const; + +#else // CPPPARSER + // This is the actual, complete interface. typedef TYPENAME PointerToArrayBase::To To; typedef TYPENAME pvector::value_type value_type; typedef TYPENAME pvector::const_reference reference; @@ -264,12 +297,16 @@ public: INLINE reference operator [](size_type n) const; INLINE reference operator [](int n) const; #endif - INLINE const Element &get_element(size_type n) const; INLINE operator const Element *() const; INLINE const Element *p() const; INLINE const pvector &v() const; + // Methods to help out Python and other high-level languages. + INLINE const Element &get_element(size_type n) const; + INLINE string get_data() const; + INLINE string get_subdata(size_type n, size_type count) const; + INLINE int get_ref_count() const; INLINE int get_node_ref_count() const;