Listing 4
template< typename C
, typename T
, typename A
>
C const *basic_string_view<C, T, A>::c_str() const
{
stlsoft_assert(is_valid());
if(NULL != m_cstr)
{
return m_cstr;
}
else if(0 == m_length)
{
return empty_string_();
}
else
{
// Must allocate the m_cstr member
char_type *s = allocate(1 + length(), NULL);
traits_type::copy(s, m_base, m_length);
s[m_length] = '\0';
m_cstr = s;
stlsoft_assert(is_valid());
return m_cstr;
}
}
template<typename C, typename T, typename A>
class_type &basic_string_view<C, T, A>::operator =(class_type const &rhs)
{
stlsoft_assert(is_valid());
refresh();
m_length = rhs.m_length;
m_base = rhs.m_base;
return *this;
}
template< typename C
, typename T
, typename A
>
void basic_string_view<C, T, A>::refresh()
{
stlsoft_assert(is_valid());
if(NULL != m_cstr)
{
deallocate(m_cstr);
m_cstr = NULL;
}
stlsoft_assert(is_valid());
}
template< typename C
, typename T
, typename A
>
/* static */ C const *basic_string_view<C, T, A>::empty_string_()
{
// This array initialized to 0, which conveniently happens to
// be the empty string, by the module/application load, so is
// guaranteed to be valid, and there are no race conditions.
static char_type s_empty[1];
stlsoft_assert(s_empty[0] == '\0'); // Paranoid check
return s_empty;
}