Correct use of XUTF8Str

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Correct use of XUTF8Str

Post by chennes »

I have the following code:

Code: Select all

Meta::Contact::Contact(const XERCES_CPP_NAMESPACE::DOMElement* e)
{
    auto emailXmlCh = XUTF8Str("email").unicodeForm();
    auto emailAttribute = e->getAttribute(emailXmlCh);
    name = StrXUTF8(e->getTextContent()).str;
    email = StrXUTF8(emailAttribute).str;
}
It's trying to extract the "email" attribute from an XML node that looks like this:

Code: Select all

<author email="jsmith@email.com">Author Name</author>
It doesn't work... the first line gives an empty (null-char-only) string in emailXmlCh. If I step through the XUTF8Str creation everything looks OK to me, and unicodeForm() appears to be populating the internal string object without any issue. So I am clearly misunderstanding something key here. I can get the attribute just fine if I iterate through the list, but that seems stupid for a such a simple use-case. Does anyone see what I'm screwing up?
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Correct use of XUTF8Str

Post by wmayer »

You must write

Code: Select all

Meta::Contact::Contact(const XERCES_CPP_NAMESPACE::DOMElement* e)
{
    auto emailAttribute = e->getAttribute(XUTF8Str("email").unicodeForm());
    ...
}
You cannot keep a pointer to the returned XMLCh array because the XUTF8Str object is a tmp. instance and will be deleted after calling unicodeForm(). So, emailXmlCh will only show garbage.

Alternatives are to make a non-tmp. instance of XUTF8Str that will be destroyed after leaving the function or you can assign the returned value of unicodeForm() to a std::basic_string<XMLCh>
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Correct use of XUTF8Str

Post by chennes »

Perfect, thanks for the explanation. I don't really need the copy, it just felt like a lot of nested function calls.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Correct use of XUTF8Str

Post by chennes »

wmayer wrote: Sat Apr 17, 2021 2:46 pm Alternatives are to make a non-tmp. instance of XUTF8Str that will be destroyed after leaving the function or you can assign the returned value of unicodeForm() to a std::basic_string<XMLCh>
Is it possible to use XUTF8Str outside of Base? I just tried moving my class from Base to App and now I'm getting linker errors about a missing destructor for the transcoder.

Code: Select all

unresolved external symbol "private: static class std::unique_ptr<class xercesc_3_2::XMLTranscoder,struct std::default_delete<class xercesc_3_2::XMLTranscoder> > StrXUTF8::transcoder" (?transcoder@StrXUTF8@@0V?$unique_ptr@VXMLTranscoder@xercesc_3_2@@U?$default_delete@VXMLTranscoder@xercesc_3_2@@@std@@@std@@A)	
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Correct use of XUTF8Str

Post by wmayer »

Is it possible to use XUTF8Str outside of Base?
Yes, but it requires a rework of the class. Currently, except of the static terminate() functions the implementation is inside the header file where the static variable transcoder is accessed. This works well within FreeCADBase where transcoder is defined in XMLTools.cpp.

However, in FreeCADApp or any other modules this will lead to the observed linking errors because transcoder is not linked from FreeCADBase. So, to make it working:
  • Move the implementations of all methods to XMLTools.cpp
  • Add the BaseExport macro (needed for Windows)
wmayer
Founder
Posts: 20302
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Correct use of XUTF8Str

Post by wmayer »

User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Correct use of XUTF8Str

Post by chennes »

wmayer wrote: Wed Apr 28, 2021 9:15 am git commit b5c49a6b77b20
Awesome, that works perfectly. Thank you!!
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
Post Reply