Home | History | Annotate | Download | only in cpp
      1 /*
      2  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      3  * Copyright (C) 2009 Google Inc. All rights reserved.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #ifndef WebDOMCString_h
     22 #define WebDOMCString_h
     23 
     24 #include <WebDOMObject.h>
     25 #include <wtf/Forward.h>
     26 #include <stddef.h> // For size_t
     27 
     28 // UTF-16 character type
     29 #if defined(WIN32)
     30 typedef wchar_t WebUChar;
     31 #else
     32 typedef unsigned short WebUChar;
     33 #endif
     34 
     35 class WebDOMCStringPrivate;
     36 class WebDOMString;
     37 
     38 // A single-byte string container with unspecified encoding.  It is
     39 // inexpensive to copy a WebDOMCString object.
     40 //
     41 // WARNING: It is not safe to pass a WebDOMCString across threads!!!
     42 //
     43 class WebDOMCString {
     44 public:
     45     ~WebDOMCString() { reset(); }
     46 
     47     WebDOMCString() : m_private(0) { }
     48 
     49     WebDOMCString(const char* data, size_t len) : m_private(0)
     50     {
     51         assign(data, len);
     52     }
     53 
     54     WebDOMCString(const WebDOMCString& s) : m_private(0) { assign(s); }
     55 
     56     WebDOMCString& operator=(const WebDOMCString& s)
     57     {
     58         assign(s);
     59         return *this;
     60     }
     61 
     62     void reset();
     63     void assign(const WebDOMCString&);
     64     void assign(const char* data, size_t len);
     65 
     66     size_t length() const;
     67     const char* data() const;
     68 
     69     bool isEmpty() const { return !length(); }
     70     bool isNull() const { return !m_private; }
     71 
     72     WebDOMString utf16() const;
     73 
     74     static WebDOMCString fromUTF16(const WebUChar* data, size_t length);
     75     static WebDOMCString fromUTF16(const WebUChar* data);
     76 
     77     WebDOMCString(const WTF::CString&);
     78     WebDOMCString& operator=(const WTF::CString&);
     79     operator WTF::CString() const;
     80 
     81 private:
     82     void assign(WebDOMCStringPrivate*);
     83     WebDOMCStringPrivate* m_private;
     84 };
     85 
     86 #endif
     87