I was playing around with strings to see if there was a route to elide
the constructors of OString and OUStrings especially for global const
strings initialised during startup

So e.g.
static const CONST_AGG_OSTRING(sGlobal, "::");
and
static const CONST_AGG_OUSTRING(TMP, "TMP");

which are, respectively, a thing called sGlobal which can be used fairly
transparently as a const OString and TMP as a const OUString. Both are
created on the stack without a constructor call and avoid duplication of
the string literal itself. I'm not sure it's worth it, but it's sort of
fun. 

Clearly as mentioned in the inline comment OUStrings are a challenge so
a real world impl have mess around with per-compiler wchar_t prefixes
and size checks like icu does, and/or hack around with -fshort-wchar
like mozilla appears to do. The horror follows...

#ifndef _STRINGHACK_HXX_
#define _STRINGHACK_HXX_

#include <rtl/string.h>
#include <rtl/ustring.h>

namespace rtl 
{ 
class OString; 
class OUString; 
}

template<int LENGTH> struct const_OString
{
    struct internal_String
    {
        oslInterlockedCount refCount;
        sal_Int32           length;
        sal_Char            buffer[LENGTH];
    };
    union
    {
        const internal_String* pRealData;
        rtl_String* pData;
    };
    const internal_String aData;
    operator rtl::OString() const
    {
        return *(reinterpret_cast<const rtl::OString*>(this));
    }
};

#define CONST_AGG_OSTRING( name, constAsciiStr ) \
    const_OString<sizeof(constAsciiStr)> name =  \
        {{&name.aData}, {0x40000000|1, \
        ((sal_Int32)sizeof(constAsciiStr)-1), \
        constAsciiStr}}

//The downside here is that gcc L wchar_t is 32bit by default, 
//-fshort-wchar can hack it to 16bits. Some other compilers default to
// 16bit or have alternative prefixes see: 
// http://bugs.icu-project.org/trac/ticket/2957
#ifdef SAL_UNICODE_NOTEQUAL_WCHAR_T
#define CONST_AGG_OUSTRING( name, constAsciiStr ) \
    ::rtl::OUString name(RTL_CONSTASCII_USTRINGPARAM(constAsciiStr))
#else

template<int LENGTH> struct const_OUString
{
    struct internal_String
    {
        oslInterlockedCount refCount;
        sal_Int32           length;
        wchar_t             buffer[LENGTH];
    };
    union
    {
        const internal_String* pRealData;
        rtl_uString* pData;
    };
    const internal_String aData;
    operator rtl::OUString() const
    {
        return *(reinterpret_cast<const rtl::OUString*>(this));
    }
};

#define CONST_AGG_OUSTRING( name, constAsciiStr ) \
    const_OUString<sizeof(constAsciiStr)> name =  \
        {{&name.aData}, {0x40000000|1, \
        ((sal_Int32)(sizeof(L##constAsciiStr)/sizeof(wchar_t))-1), \
        L##constAsciiStr}}
#endif

#endif

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to