Conversion between string types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Matthews

    Conversion between string types

    Hi,

    I'm working with Borland C++ Builder 6.2.
    My project uses the std::string class. However, Borland in
    its infinite wisdom has its own string class, AnsiString.

    To make my life easier, I want to create a function,
    preferably a conversion operator, to convert from a
    std::string to an AnsiString. Please note that I don't
    want to change either std::string or AnsiString.

    The Background
    --------------
    In using Borland's Database Engine (BDE), there is
    a variant type "TVariant" which it uses for its
    field types. The TVariant is a union:
    class TVarRec

    {
    public:
    union
    {
    Integer VInteger;
    Boolean VBoolean;
    Char VChar;
    PExtended VExtended;
    PShortString VString;
    Pointer VPointer;
    PChar VPChar;
    TObject* VObject;
    TClass VClass;
    WideChar VWideChar;
    PWideChar VPWideChar;
    Pointer VAnsiString;
    PCurrency VCurrency;

    PVariant VVariant;
    };
    union
    {
    Byte VType;
    };
    };


    I'm writing my own generic wrapper for fields,
    and I have a function to fill in a TVarRec
    variable:
    class Field
    {
    public:
    virtual void fill_variant(TV arRec * pv) const = 0;
    };

    class Integer_Field
    : public Field
    {
    int value;
    public:
    virtual void fill_variant(TV arRec * p_variant) const
    {*p_variant = value;}
    };


    So far, so good (I hope). Now I'm trying to make
    a templated field class. The issue is that TVarRec
    has no assignment for std::string, but for AnsiString.
    Which means that I need a conversion for the
    fill_variant method:
    class String_Field
    : public Field
    {
    std::string value;
    public:
    virtual void fill_variant(TV arRec * p_variant) const
    {*p_variant = AnsiString(valu e.c_str());}
    // The AnsiString type has constructor for C style strings.
    };


    When I convert String_Field to use a template, the content
    of the fill_variant method differs from the Integer field.
    template <typename Field_Value>
    class Typed_Field
    : public Field
    {
    Field_Value value;
    public:
    virtual void fill_variant(TV arRec * p_variant) const
    {*p_variant = value;}
    };


    The thorn in this design is that Typed_Field<std ::string>
    causes an error because there is no std::string type
    defined in the TVarRec. The PShortString type will
    convert from an AnsiString type though.

    I have other cases where I am having to convert from
    a std::string type to an AnsiString type (such as all
    the windowing APIs). This is where a global conversion
    function would come in handy. The function would be
    really handy if it could be implicit so I don't have
    to write something like:
    edit_box.Text = AnsiStringFromS TLString(s);
    I'd rather write:
    string hello("hello");
    edit_box.Text = hello; // Implied conversion

    Any ideas?

    BTW, this is not off-topic, as I am searching for
    a conversion operator between types; regardless
    of whether the types are STL, C++ runtime or
    external libraries.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

  • Victor Bazarov

    #2
    Re: Conversion between string types

    "Thomas Matthews" <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote...[color=blue]
    > My project uses the std::string class. However, Borland in
    > its infinite wisdom has its own string class, AnsiString.
    >
    > To make my life easier, I want to create a function,
    > preferably a conversion operator, to convert from a
    > std::string to an AnsiString. Please note that I don't
    > want to change either std::string or AnsiString.
    > [...]
    > I have other cases where I am having to convert from
    > a std::string type to an AnsiString type (such as all
    > the windowing APIs). This is where a global conversion
    > function would come in handy. The function would be
    > really handy if it could be implicit so I don't have
    > to write something like:
    > edit_box.Text = AnsiStringFromS TLString(s);
    > I'd rather write:
    > string hello("hello");
    > edit_box.Text = hello; // Implied conversion
    >
    > Any ideas?[/color]

    An assignment operator cannot be anything else but a non-static
    member. You already know that (hopefully). Type conversion
    operators cannot be anything but non-static members. So, you
    are pretty much screwed in that regard if you don't want to muck
    with either class. What you did is basically the only path you
    can take, I think.

    Victor


    Comment

    • Thomas Matthews

      #3
      Re: Conversion between string types

      Victor Bazarov wrote:
      [color=blue]
      > "Thomas Matthews" <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote...
      >[color=green]
      >>My project uses the std::string class. However, Borland in
      >>its infinite wisdom has its own string class, AnsiString.
      >>
      >>To make my life easier, I want to create a function,
      >>preferably a conversion operator, to convert from a
      >>std::string to an AnsiString. Please note that I don't
      >>want to change either std::string or AnsiString.
      >>[...]
      >>I have other cases where I am having to convert from
      >>a std::string type to an AnsiString type (such as all
      >>the windowing APIs). This is where a global conversion
      >>function would come in handy. The function would be
      >>really handy if it could be implicit so I don't have
      >>to write something like:
      >> edit_box.Text = AnsiStringFromS TLString(s);
      >>I'd rather write:
      >> string hello("hello");
      >> edit_box.Text = hello; // Implied conversion
      >>
      >>Any ideas?[/color]
      >
      >
      > An assignment operator cannot be anything else but a non-static
      > member. You already know that (hopefully). Type conversion
      > operators cannot be anything but non-static members. So, you
      > are pretty much screwed in that regard if you don't want to muck
      > with either class. What you did is basically the only path you
      > can take, I think.
      >
      > Victor
      >
      >[/color]

      Thanks Victor. I had a gut feeling that I was screwed.
      I can create a template function and overloaded functions
      to help me out:
      void AssignToVarRec( const std::string& s,
      TVarRec * p_varrec)
      {
      *p_varrec = AnsiString(s.c_ str());
      return;
      }

      template <typename T>
      void AssignToVarRec( const T& value,
      TVarRec * p_varrec)
      {
      *p_varrec = value;
      return;
      }

      So all I have to do is change my assignment operators
      to use AssignToVarRec. A bit more complicated, but it
      will work.

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      Working...