You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuscany.apache.org by Geoffrey Winn <ge...@googlemail.com> on 2006/06/06 11:45:30 UTC

[SDO C++] Representing strings as objects rather than C style character pointers

I've been investigating how to modify the C++ variant of SDO to use string
objects (ie something like std::string) rather than C style strings as it
mostly does currently. Internally we can do what we want, but the user
visible
interface needs more care and has implications for the specification. I'd
like
to start a discussion about the options and try to get some agreement over
the
changes that we would like to make to the specification.

Currently I am adding an SDOString class that inherits from std::string.
This
gives us the option to add or modify behaviour and also minimises the
overhead
when we just want the behaviour of standard string.

The easy bit involves parameters to methods. If we have a method that takes
a
C style string as an argument,

     void setName(const char* name_in)

then we can add another version of the same method,

     void setName(const SDOString& name_in)

The old interface is preserved and existing users of it are unaffected, but
anyone who wants to, can use the new one. These extra methods would probably
need to be mentioned in the specification, but we haven't removed or
compromised
anything that is already there.

The more difficult case arises when a method returns a string value. For
example,

     const char* getName()

We can't overload this, so we have two alternatives.

1. Add another method with a different name. For example,

     const SDOString& getNameAsSDOString()

and the new name (whatever it might be) would have to become part of the
specification.

2. Change the return type to a string object. For example,

   const SDOString& getName()

This is neater, but breaks existing users who, as a minimum, will need to
extract the C style string from the string object, for example.

   const char* local_name = someObject.getName().c_str();

We could remove this requirement by providing a user defined conversion
operator to automatically convert from the object to a string pointer, for
example, within SDOString

  operator const char*() const { return c_str(); }

However, there are two potential problems with this.

1. The general objection, as stated in Stroustrup 11.4

"In general, it is wise to be sparing in the introduction of conversion
operators. When used in excess, they lead to ambiguities."

2. The specific problem that methods that mix C style parameters with object
   style parameters can confuse the compiler.

Suppose we have two (overloaded) methods

   1. void fn(const char* first, const char* second)
   2. void fn(const SDOString& first, const SDOString& second)

and we make a call passing a C style string as the first parameter and an
SDOString as the second parameter. Without the conversion operator, the
first
parameter is converted to an SDOString and the second method is called. But,
if the conversion is provided, then the compiler also has the option to
convert the second parameter to a C style string and call the first method -
and it is ambiguous which of these to choose, leading to a compile time
error.

Since there are relatively few users of SDO for C++ at the moment, I think
we
still have the option to change the return type to SDOString and avoid the
need for a conversion operator.

In summary, we need to decide how to handle string valued method returns
and then make the appropriate changes to the specification.

Regards,

Geoff.

Re: [SDO C++] Representing strings as objects rather than C style character pointers

Posted by Geoffrey Winn <ge...@googlemail.com>.
On 06/06/06, Geoffrey Winn <ge...@googlemail.com> wrote:
>
>
> The more difficult case arises when a method returns a string value. For
> example,
>
>      const char* getName()
>
> We can't overload this, so we have two alternatives.
>
>
What I was trying to write in my previous note was ...

I should have known better. There is of course at least one other way.

We could introduce a new overloaded method where the return value is one of
the parameters, for example,

void getName(SDOString& name)

This avoids introducing new method names and leaves existing users
unaffected, but the eventual change to using this type of method will
require a larger code change and it also makes the old and new methods
rather inconsistent.

Regards,

Geoff.

Re: [SDO C++] Representing strings as objects rather than C style character pointers

Posted by Geoffrey Winn <ge...@googlemail.com>.
On 06/06/06, Geoffrey Winn <ge...@googlemail.com> wrote:
>
>
> The more difficult case arises when a method returns a string value. For
> example,
>
>      const char* getName()
>
> We can't overload this, so we have two alternatives.
>

I should have known better. There is of course at least one other way.

We could introduce a new overloaded method where the return value is one of
the parameters, for example,

void