]> git.sesse.net Git - casparcg/blob - dependencies/berkelium/include/berkelium/WeakString.hpp
Manually merged pull request #222
[casparcg] / dependencies / berkelium / include / berkelium / WeakString.hpp
1 /*  Berkelium - Embedded Chromium
2  *  SafeString.hpp
3  *
4  *  Copyright (c) 2009, Patrick Reiter Horn
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are
9  *  met:
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
15  *    distribution.
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.
19  *
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.
31  */
32
33 #ifndef _BERKELIUM_WEAK_STRING_HPP_
34 #define _BERKELIUM_WEAK_STRING_HPP_
35
36 #include "berkelium/Platform.hpp"
37
38 namespace Berkelium {
39
40 // Simple POD string class. Data must be owned by caller.
41 template <class CharType>
42 struct WeakString {
43     const CharType* mData;
44     size_t mLength;
45
46     typedef CharType Type;
47
48     inline const CharType* data() const {
49         return mData;
50     }
51
52     inline size_t length() const {
53         return mLength;
54     }
55
56     inline size_t size() const {
57         return mLength;
58     }
59
60     template <class StrType>
61     inline StrType& get(StrType& ret) const {
62         if (!mData || !mLength) {
63             ret = StrType();
64         }
65         ret = StrType(mData, mLength);
66         return ret;
67     }
68
69     template <class StrType>
70     inline StrType get() const {
71         if (!mData || !mLength) {
72             return StrType();
73         }
74         return StrType(mData, mLength);
75     }
76
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();
82         return ret;
83     }
84
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;
90         return ret;
91     }
92
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) {
97         }
98         return ret;
99     }
100
101     inline static WeakString<CharType> empty() {
102         WeakString<CharType> ret;
103         ret.mData = NULL;
104         ret.mLength = 0;
105         return ret;
106     }
107 };
108
109 template <class StrType, class CharType>
110 inline StrType &operator+(const StrType&lhs, const WeakString<CharType>&rhs) {
111     StrType temp;
112     return lhs + rhs.get(temp);
113 }
114
115 template <class StrType, class CharType>
116 inline StrType &operator+=(StrType&lhs, const WeakString<CharType>&rhs) {
117     StrType temp;
118     return lhs += rhs.get(temp);
119 }
120
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++) {
126         lhs << data[i];
127     }
128     return lhs;
129 }
130
131 typedef WeakString<char> URLString;
132 typedef WeakString<wchar_t> WideString;
133
134 #if BERKELIUM_PLATFORM == PLATFORM_WINDOWS
135 typedef WeakString<wchar_t> FileString;
136 #else
137 typedef WeakString<char> FileString;
138 #endif
139
140 }
141
142 #endif