You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Jon Smirl <jo...@mediaone.net> on 2001/01/09 06:10:30 UTC
SAX2 Attributes and const
I'm implementing a SAX2 Attributes interface and I'm having trouble with the const functions. My SAX2 implementation is a wrapper for calling a Mozilla XPCOM interface that implements SAX2.
//class member variable
nsXPIDLString m_temp; // causes a 'new' but will reuse the storage
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
m_atts->GetQName(index, getter_Copies(m_temp));
return m_temp; // implied cast causes a pointer to the internal buffer to be returned
}
How can I implement this with a const function? I need to copy the variable out of the interface into a member variable and then return a pointer to it.
I can always copy the entire attribute array into local storage when my wrapper class is created but this is very inefficient since I will need to allocate lots of temporary variables to hold the strings. Right now I just reuse a single variable over and over.
Jon Smirl
jonsmirl@mediaone.net
Re: SAX2 Attributes and const
Posted by Dean Roddey <dr...@charmedquark.com>.
Make m_temp a mutable member. Then you can change it from a const method.
--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com
"It takes two buttocks to make friction"
- African Proverb
----- Original Message -----
From: Jon Smirl
To: xerces-c-dev@xml.apache.org
Sent: Monday, January 08, 2001 9:43 PM
Subject: Re: SAX2 Attributes and const
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
// this call causes XPCOM to copy the string into the m_temp variable where m_temp is a member variable for the class
// I have to do a copy here since the way XPCOM works. The string may be coming from another process or machine
// It's the copy into m_temp that I can't do, XPCOM only copies accross an interface, you can't pass pointers
// I can can't change member variables since the function is const
m_atts->GetQName(index, getter_Copies(m_temp));
// implied cast causes a pointer to the internal buffer to be returned
// this returns a const pointer to the internal buffer which is already in the correct format
return m_temp;
}
Jon Smirl
jonsmirl@mediaone.net
----- Original Message -----
From: Dean Roddey
To: xerces-c-dev@xml.apache.org
Sent: Tuesday, January 09, 2001 12:21 AM
Subject: Re: SAX2 Attributes and const
Your string class just needs a get_buffer(), or whatever you want to call it, that returns a const pointer to the data. However, if your string doesn't store characters in the format that XMLCh is set up for (UTF-16 usually), then you'll have to make a copy of it and return that pointer. You can't make a local copy, it must also be a member of the class.
--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com
"It takes two buttocks to make friction"
- African Proverb
----- Original Message -----
From: Jon Smirl
To: xerces
Sent: Monday, January 08, 2001 9:10 PM
Subject: SAX2 Attributes and const
I'm implementing a SAX2 Attributes interface and I'm having trouble with the const functions. My SAX2 implementation is a wrapper for calling a Mozilla XPCOM interface that implements SAX2.
file://class member variable
nsXPIDLString m_temp; // causes a 'new' but will reuse the storage
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
m_atts->GetQName(index, getter_Copies(m_temp));
return m_temp; // implied cast causes a pointer to the internal buffer to be returned
}
How can I implement this with a const function? I need to copy the variable out of the interface into a member variable and then return a pointer to it.
I can always copy the entire attribute array into local storage when my wrapper class is created but this is very inefficient since I will need to allocate lots of temporary variables to hold the strings. Right now I just reuse a single variable over and over.
Jon Smirl
jonsmirl@mediaone.net
Re: SAX2 Attributes and const
Posted by Jon Smirl <jo...@mediaone.net>.
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
// this call causes XPCOM to copy the string into the m_temp variable where m_temp is a member variable for the class
// I have to do a copy here since the way XPCOM works. The string may be coming from another process or machine
// It's the copy into m_temp that I can't do, XPCOM only copies accross an interface, you can't pass pointers
// I can can't change member variables since the function is const
m_atts->GetQName(index, getter_Copies(m_temp));
// implied cast causes a pointer to the internal buffer to be returned
// this returns a const pointer to the internal buffer which is already in the correct format
return m_temp;
}
Jon Smirl
jonsmirl@mediaone.net
----- Original Message -----
From: Dean Roddey
To: xerces-c-dev@xml.apache.org
Sent: Tuesday, January 09, 2001 12:21 AM
Subject: Re: SAX2 Attributes and const
Your string class just needs a get_buffer(), or whatever you want to call it, that returns a const pointer to the data. However, if your string doesn't store characters in the format that XMLCh is set up for (UTF-16 usually), then you'll have to make a copy of it and return that pointer. You can't make a local copy, it must also be a member of the class.
--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com
"It takes two buttocks to make friction"
- African Proverb
----- Original Message -----
From: Jon Smirl
To: xerces
Sent: Monday, January 08, 2001 9:10 PM
Subject: SAX2 Attributes and const
I'm implementing a SAX2 Attributes interface and I'm having trouble with the const functions. My SAX2 implementation is a wrapper for calling a Mozilla XPCOM interface that implements SAX2.
file://class member variable
nsXPIDLString m_temp; // causes a 'new' but will reuse the storage
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
m_atts->GetQName(index, getter_Copies(m_temp));
return m_temp; // implied cast causes a pointer to the internal buffer to be returned
}
How can I implement this with a const function? I need to copy the variable out of the interface into a member variable and then return a pointer to it.
I can always copy the entire attribute array into local storage when my wrapper class is created but this is very inefficient since I will need to allocate lots of temporary variables to hold the strings. Right now I just reuse a single variable over and over.
Jon Smirl
jonsmirl@mediaone.net
Re: SAX2 Attributes and const
Posted by Dean Roddey <dr...@charmedquark.com>.
Your string class just needs a get_buffer(), or whatever you want to call it, that returns a const pointer to the data. However, if your string doesn't store characters in the format that XMLCh is set up for (UTF-16 usually), then you'll have to make a copy of it and return that pointer. You can't make a local copy, it must also be a member of the class.
--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com
"It takes two buttocks to make friction"
- African Proverb
----- Original Message -----
From: Jon Smirl
To: xerces
Sent: Monday, January 08, 2001 9:10 PM
Subject: SAX2 Attributes and const
I'm implementing a SAX2 Attributes interface and I'm having trouble with the const functions. My SAX2 implementation is a wrapper for calling a Mozilla XPCOM interface that implements SAX2.
file://class member variable
nsXPIDLString m_temp; // causes a 'new' but will reuse the storage
const XMLCh* AttributesImpl::getQName(const unsigned int index) const
{
m_atts->GetQName(index, getter_Copies(m_temp));
return m_temp; // implied cast causes a pointer to the internal buffer to be returned
}
How can I implement this with a const function? I need to copy the variable out of the interface into a member variable and then return a pointer to it.
I can always copy the entire attribute array into local storage when my wrapper class is created but this is very inefficient since I will need to allocate lots of temporary variables to hold the strings. Right now I just reuse a single variable over and over.
Jon Smirl
jonsmirl@mediaone.net