[Boost-users] [Objective-C++] Specializing boost::lexical_cast

24 views
Skip to first unread message

Kelvin Chung

unread,
Oct 14, 2011, 9:37:02 PM10/14/11
to boost...@lists.boost.org
I'm trying to specialize boost::lexical_cast to convert Objective-C
objects into various standard C++ classes (for example, leveraging
NSNumber::-intValue to create lexical_cast<int>(NSNumber*)). However,
as Objective-C objects exist as pointers, I would imagine that they
would fail the requirements of needing well-defined streaming
operator<<() in order to use the stock boost::lexical_cast. Any words
of advice on how to do this in a way that will do as little damage as
possible?


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Chris Cleeland

unread,
Oct 15, 2011, 3:48:07 PM10/15/11
to boost...@lists.boost.org
On Fri, Oct 14, 2011 at 8:37 PM, Kelvin Chung <kel...@mac.com> wrote:
> I'm trying to specialize boost::lexical_cast to convert Objective-C objects
> into various standard C++ classes (for example, leveraging
> NSNumber::-intValue to create lexical_cast<int>(NSNumber*)).  However, as
> Objective-C objects exist as pointers, I would imagine that they would fail
> the requirements of needing well-defined streaming operator<<() in order to
> use the stock boost::lexical_cast.  Any words of advice on how to do this in
> a way that will do as little damage as possible?

I haven't looked at the impl for lexical_cast, but this comes to mind
off the top of my head...

Perhaps consider a C++ wrapper around the ObjC ptr that knows how to
query the ObjC object for its type and turn it into the correct C++
type. Then provide a partial specialization for lexical_cast<> and
that type.

Kelvin Chung

unread,
Oct 15, 2011, 11:40:28 PM10/15/11
to boost...@lists.boost.org
On 2011-10-15 19:48:07 +0000, Chris Cleeland said:

> On Fri, Oct 14, 2011 at 8:37 PM, Kelvin Chung <kel...@mac.com> wrote:
>> I'm trying to specialize boost::lexical_cast to convert Objective-C objects
>> into various standard C++ classes (for example, leveraging
>> NSNumber::-intValue to create lexical_cast<int>(NSNumber*)).  However, as
>> Objective-C objects exist as pointers, I would imagine that they would fail
>> the requirements of needing well-defined streaming operator<<() in order to
>> use the stock boost::lexical_cast.  Any words of advice on how to do this in
>> a way that will do as little damage as possible?
>
> I haven't looked at the impl for lexical_cast, but this comes to mind
> off the top of my head...
>
> Perhaps consider a C++ wrapper around the ObjC ptr that knows how to
> query the ObjC object for its type and turn it into the correct C++
> type. Then provide a partial specialization for lexical_cast<> and
> that type.

Upon another look at this, operator<<() is defined for generic pointers
as well as const char* et al., and you'd need to explicitly cast to
void* if you wanted to output a const char* as a void*, say. So would
a wrapper class be necessary at all? Would just implementing streaming
operators, like

std::ostream& operator<<(std::ostream& out, id p) { out << [p
description]; return out; }

std::istream& operator>>(std::istream& in, NSString*& str) { ... }

(templates, but you get the point)

be all it takes for lexical_cast to work? Or would it be better to
just explicitly specialize lexical_cast for specific Objective-C
sources and target types? (This comes to play in stuff like the
Objective-C class NSNumber, since conversion to and from integral types
are well defined but an operator>>(std::basic_istream&, NSNumber*)...
not so much)

Reply all
Reply to author
Forward
0 new messages