Overview
Features
Download
Documentation
Community
Add-Ons & Services

wxString in a Poco::DynamicAny

Please post support and help requests here.

wxString in a Poco::DynamicAny

Postby fbraem » 29 Oct 2009, 22:41

I'm working with a lot of wxWidgets code and I want to make wxString available in a dynamic way.
What's the easiest way to make wxString dynamic? An example:

Code: Select all
 wxString wx = "Hello";

 Poco::Dynamic any = wx;

 std::string s = any;
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 104
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: wxString in a Poco::DynamicAny

Postby alex » 10 Nov 2009, 00:20

You'll have to provide Poco::Dynamic::VarHolderImpl<wxString> (must be in Poco::Dynamic namespace) specialization.

See Poco::Dynamic::VarHolderImpl<BLOB> and Poco::Dynamic::VarHolderImpl<CLOB> specializations in Poco/Data/LOB.h for example how to do it. Of course, you'll want to implement conversions that make sense for your type.
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: wxString in a Poco::DynamicAny

Postby fbraem » 10 Nov 2009, 21:56

And is this the correct way (by specialization of the cast operator) to make it possible to assign a DynamicAny to wxString? Is this enough?

Code: Select all
  template <>
  DynamicAny::operator wxString () const
  {
    if (!_pHolder)
      throw InvalidAccessException("Can not convert empty value.");

    std::string result;
    _pHolder->convert(result);
    return wxString(result.c_str(), wxConvUTF8);
  }
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 104
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: wxString in a Poco::DynamicAny

Postby alex » 11 Nov 2009, 03:49

fbraem wrote:And is this the correct way (by specialization of the cast operator) to make it possible to assign a DynamicAny to wxString? Is this enough

I've never thought of that before, although the question of what to do about types not known up-front, for which there is no convert() overload did occur to me. AFAICT it should work. Function template specialization is frowned upon by C++ community. In this case it appears to be an elegant way to circumvent the problem of not knowing up-front all the UDTs that may be specialized by users. I'm too tired right now to refresh myself on the issue, but if you stumble over any problems, let us know.
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: wxString in a Poco::DynamicAny

Postby alex » 12 Nov 2009, 02:25

fbraem wrote:And is this the correct way (by specialization of the cast operator) to make it possible to assign a DynamicAny to wxString?

You may want to do this:
Code: Select all
template <> Var::operator wxString () const
{
   if (!_pHolder) throw InvalidAccessException("Can not convert empty value.");
 
   if (typeid(wxString) == _pHolder->type()) return extract<wxString>();
   else
   {
      std::string result;
      _pHolder->convert(result);
      return wxString(result.c_str(), wxConvUTF8);
   }
}

It saves you the unnecessary conversion when assigning Var holding wxString.
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: wxString in a Poco::DynamicAny

Postby alex » 13 Nov 2009, 02:15

I have added support and tests for Var assignment to the Var-enabled Poco::Data datatypes (Date, Time and LOB)to the trunk SVN.
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: wxString in a Poco::DynamicAny

Postby alex » 13 Nov 2009, 16:02

Update: unless explicitly casted to target type, Sun CC attempts to match the cast to the templated code only and disregards the specialization. But, if you cast explicitly, then it takes the specialized one. Go figure. Unfortunately, a workaround as the one for Data::Date and Data::Time is not possible for a template class like LOB. Anyway, long story short, if you want to assign a Dynamic::Var to a template class and remain portable, you'll have to explicitly cast it to the target type.
For non-template classes adding assignment operator to the class alleviates this:
Code: Select all
operator = (const Var&)

to the class. Most recent trunk revision is 1281
alex
 
Posts: 1120
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron