MaxStringCast< ChType > Class Template Reference
 
 
 
MaxStringCast< ChType > Class Template Reference

This reference page is linked to from the following overview topics: Unicode.


#include <maxstring.h>

Inheritance diagram for MaxStringCast< ChType >:
MaxHeapOperators

Class Description

template<typename ChType>
class MaxSDK::Util::MaxStringCast< ChType >

A MaxStringCast is used when casting a MaxString to a type.

It holds 1. A MaxString, which increments the ref count on the MaxStringData (so the MaxStringData pointer won't be deleted) 2. A pointer to the appropriate string buffer in the MaxStringData.

So, the string pointer is guaranteed to be valid as along as the MaxStringCast instance is alive. Warning: If you cast directly to a, for example, char*, then something else could operate on the MaxString causing it to delete the string buffer in the MaxStringData, and you would be left holding a dangling pointer.

Note, data held by this class should be treated as read only and not modifiable. Thus its operator methods only convert to constant types. For instance if used with a char type, its operators will only return a const char*. If you want a char* type need to create a copy instead. Therefore do not use const_cast<> with this class.

Public Member Functions

  MaxStringCast ()
  MaxStringCast (const MaxString &string, const ChType *w)
  operator const ChType * () const
  Returns a const pointer to the string.
const ChType *  data () const
  Returns a const pointer to the string.
const ChType *  null_data () const
  Returns a const pointer pointer to 0.
bool  isNull () const
  returns true if this string is null or empty, false otherwise
const MaxStringCast< char >  ToMCHAR (size_t *length=NULL) const
  Cast this object to a MCHAR.
const MaxStringCast< wchar_t >  ToOLESTR (size_t *length=NULL) const
  Cast this object to a wchar_t.
const MaxString owner () const
  Returns a const ref to the owning MaxString.

Protected Attributes

MaxString  p
const ChType *  buf

Constructor & Destructor Documentation

MaxStringCast ( ) [inline]
                : buf(NULL)
        {
        }
MaxStringCast ( const MaxString string,
const ChType *  w 
) [inline]
                : p(string)
                , buf(w)
        {
        }

Member Function Documentation

operator const ChType * ( ) const [inline]

Returns a const pointer to the string.

If the string is NULL, a pointer to 0 is returned.

        {
                return (buf) ? buf: null_data();
        }
const ChType* data ( ) const [inline]

Returns a const pointer to the string.

If the string is NULL, a pointer to 0 is returned.

        {
                return (buf) ? buf: null_data();
        }
const ChType* null_data ( ) const [inline]

Returns a const pointer pointer to 0.

        {
                static ChType null_char = 0; 
                return &null_char;
        }
bool isNull ( ) const [inline]

returns true if this string is null or empty, false otherwise

        {
                return data()[0] != 0;
        }
const MaxStringCast<char> ToMCHAR ( size_t *  length = NULL ) const [inline]

Cast this object to a MCHAR.

The string returned is valid as long as the MaxStringCast exists.

        {
                return p.ToACP(length);
        }
const MaxStringCast<wchar_t> ToOLESTR ( size_t *  length = NULL ) const [inline]

Cast this object to a wchar_t.

The string returned is valid as long as the MaxStringCast exists.

        {
                return p.ToUTF16(length);
        }
const MaxString& owner ( ) const [inline]

Returns a const ref to the owning MaxString.

        {
                return p;
        }

Member Data Documentation

const ChType* buf [protected]