1 /* Berkelium - Embedded Chromium
4 * Copyright (c) 2009, Patrick Reiter Horn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Sirikata nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef _BERKELIUM_WEAK_STRING_HPP_
34 #define _BERKELIUM_WEAK_STRING_HPP_
36 #include "berkelium/Platform.hpp"
40 // Simple POD string class. Data must be owned by caller.
41 template <class CharType>
43 const CharType* mData;
46 typedef CharType Type;
48 inline const CharType* data() const {
52 inline size_t length() const {
56 inline size_t size() const {
60 template <class StrType>
61 inline StrType& get(StrType& ret) const {
62 if (!mData || !mLength) {
65 ret = StrType(mData, mLength);
69 template <class StrType>
70 inline StrType get() const {
71 if (!mData || !mLength) {
74 return StrType(mData, mLength);
77 template <class StrType>
78 inline static WeakString<CharType> point_to(const StrType&input) {
79 WeakString<CharType> ret;
80 ret.mData = input.data();
81 ret.mLength = input.length();
85 inline static WeakString<CharType> point_to(const CharType*input_data,
86 size_t input_length) {
87 WeakString<CharType> ret;
88 ret.mData = input_data;
89 ret.mLength = input_length;
93 inline static WeakString<CharType> point_to(const CharType *input_data) {
94 WeakString<CharType> ret;
95 ret.mData = input_data;
96 for (ret.mLength = 0; input_data[ret.mLength]; ++ret.mLength) {
101 inline static WeakString<CharType> empty() {
102 WeakString<CharType> ret;
109 template <class StrType, class CharType>
110 inline StrType &operator+(const StrType&lhs, const WeakString<CharType>&rhs) {
112 return lhs + rhs.get(temp);
115 template <class StrType, class CharType>
116 inline StrType &operator+=(StrType&lhs, const WeakString<CharType>&rhs) {
118 return lhs += rhs.get(temp);
121 template <class OstreamType, class CharType>
122 inline OstreamType &operator<< (OstreamType&lhs, const WeakString<CharType>&rhs) {
123 size_t length = rhs.length();
124 const CharType *data = rhs.data();
125 for (size_t i = 0; i < length; i++) {
131 typedef WeakString<char> URLString;
132 typedef WeakString<wchar_t> WideString;
134 #if BERKELIUM_PLATFORM == PLATFORM_WINDOWS
135 typedef WeakString<wchar_t> FileString;
137 typedef WeakString<char> FileString;