Channels ▼
RSS

Generalized String Manipulation: Access Shims and Type Tunneling


Listing 12: String length shims

Listing 12: String length shims

/* /////////////////////////////////////////////////////////////
 * Extract from stlsoft_string_access.h, comstl_string_access.h,
 *              mfcstl_string_access.h, winstl_string_access.h
 *
 * www:    http://stlsoft.org/, http://comstl.org/
 *         http://mfcstl.org/, http://winstl.org/
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 * ////////////////////////////////////////////////////////// */

namespace stlsoft
{
  /* C-style Unicode string */
  inline size_t c_str_len(wchar_t const *s)
  {
    return (s == 0) ? 0 : char_traits<wchar_t>::length(s);
  }
} // namespace stlsoft

namespace comstl
{
  const size_t COMSTL_CCH_GUID = 38; // Fixed format & length

  /* GUID */
  inline size_t c_str_len(GUID const &/* guid */)
  {
    return COMSTL_CCH_GUID;
  }
  /* VARIANT */
  inline size_t c_str_len(VARIANT const &v)
  {
    if(v.vt == VT_BSTR)
    {
      return ::SysStringLen(v.bstrVal);
    }
    else
    {
      // Do a copy and conversion to VT_BSTR and get
      // length from that
      ...
  }
} // namespace comstl

namespace mfcstl
{
  /* CString */
  inline size_t c_str_len(CString const &s)
  {
    return s.GetLength();
  }
} // namespace mfcstl

namespace winstl
{
  /* HWND */
  inline size_t c_str_len(HWND h)
  {
    return static_cast<size_t>(::GetWindowTextLength(h));
  }
  /* LSA_UNICODE_STRING */
  inline size_t c_str_len(LSA_UNICODE_STRING const &s)
  {
    return s.Length;
  }
} // namespace winstl


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video