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 WebDOMString_h
     22 #define WebDOMString_h
     23 
     24 #include <WebDOMCString.h>
     25 #include <wtf/Forward.h>
     26 
     27 class WebDOMStringPrivate;
     28 
     29 // A UTF-16 string container.  It is inexpensive to copy a WebDOMString
     30 // object.
     31 //
     32 // WARNING: It is not safe to pass a WebDOMString across threads!!!
     33 //
     34 class WebDOMString {
     35 public:
     36     ~WebDOMString() { reset(); }
     37 
     38     WebDOMString() : m_private(0) { }
     39 
     40     WebDOMString(const WebUChar* data, size_t len) : m_private(0)
     41     {
     42         assign(data, len);
     43     }
     44 
     45     WebDOMString(const WebDOMString& s) : m_private(0) { assign(s); }
     46 
     47     WebDOMString& operator=(const WebDOMString& s)
     48     {
     49         assign(s);
     50         return *this;
     51     }
     52 
     53     void reset();
     54     void assign(const WebDOMString&);
     55     void assign(const WebUChar* data, size_t len);
     56 
     57     size_t length() const;
     58     const WebUChar* data() const;
     59 
     60     bool isEmpty() const { return !length(); }
     61     bool isNull() const { return !m_private; }
     62 
     63     WebDOMCString utf8() const;
     64 
     65     static WebDOMString fromUTF8(const char* data, size_t length);
     66     static WebDOMString fromUTF8(const char* data);
     67 
     68     template <int N> WebDOMString(const char (&data)[N])
     69         : m_private(0)
     70     {
     71         assign(fromUTF8(data, N - 1));
     72     }
     73 
     74     template <int N> WebDOMString& operator=(const char (&data)[N])
     75     {
     76         assign(fromUTF8(data, N - 1));
     77         return *this;
     78     }
     79 
     80     WebDOMString(const WTF::String&);
     81     WebDOMString& operator=(const WTF::String&);
     82     operator WTF::String() const;
     83 
     84     WebDOMString(const WTF::AtomicString&);
     85     WebDOMString& operator=(const WTF::AtomicString&);
     86     operator WTF::AtomicString() const;
     87 
     88     bool equals(const char* string) const;
     89 
     90 private:
     91     void assign(WebDOMStringPrivate*);
     92     WebDOMStringPrivate* m_private;
     93 };
     94 
     95 #endif
     96