Berkelium

include/berkelium/WeakString.hpp

Go to the documentation of this file.
00001 /*  Berkelium - Embedded Chromium
00002  *  SafeString.hpp
00003  *
00004  *  Copyright (c) 2009, Patrick Reiter Horn
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _BERKELIUM_WEAK_STRING_HPP_
00034 #define _BERKELIUM_WEAK_STRING_HPP_
00035 
00036 #include "berkelium/Platform.hpp"
00037 
00038 namespace Berkelium {
00039 
00040 // Simple POD string class. Data must be owned by caller.
00041 template <class CharType>
00042 struct WeakString {
00043     const CharType* mData;
00044     size_t mLength;
00045 
00046     typedef CharType Type;
00047 
00048     inline const CharType* data() const {
00049         return mData;
00050     }
00051 
00052     inline size_t length() const {
00053         return mLength;
00054     }
00055 
00056     inline size_t size() const {
00057         return mLength;
00058     }
00059 
00060     template <class StrType>
00061     inline StrType& get(StrType& ret) const {
00062         if (!mData || !mLength) {
00063             ret = StrType();
00064         }
00065         ret = StrType(mData, mLength);
00066         return ret;
00067     }
00068 
00069     template <class StrType>
00070     inline StrType get() const {
00071         if (!mData || !mLength) {
00072             return StrType();
00073         }
00074         return StrType(mData, mLength);
00075     }
00076 
00077     template <class StrType>
00078     inline static WeakString<CharType> point_to(const StrType&input) {
00079         WeakString<CharType> ret;
00080         ret.mData = input.data();
00081         ret.mLength = input.length();
00082         return ret;
00083     }
00084 
00085     inline static WeakString<CharType> point_to(const CharType*input_data,
00086                                                 size_t input_length) {
00087         WeakString<CharType> ret;
00088         ret.mData = input_data;
00089         ret.mLength = input_length;
00090         return ret;
00091     }
00092 
00093     inline static WeakString<CharType> point_to(const CharType *input_data) {
00094         WeakString<CharType> ret;
00095         ret.mData = input_data;
00096         for (ret.mLength = 0; input_data[ret.mLength]; ++ret.mLength) {
00097         }
00098         return ret;
00099     }
00100 
00101     inline static WeakString<CharType> empty() {
00102         WeakString<CharType> ret;
00103         ret.mData = NULL;
00104         ret.mLength = 0;
00105         return ret;
00106     }
00107 };
00108 
00109 template <class StrType, class CharType>
00110 inline StrType &operator+(const StrType&lhs, const WeakString<CharType>&rhs) {
00111     StrType temp;
00112     return lhs + rhs.get(temp);
00113 }
00114 
00115 template <class StrType, class CharType>
00116 inline StrType &operator+=(StrType&lhs, const WeakString<CharType>&rhs) {
00117     StrType temp;
00118     return lhs += rhs.get(temp);
00119 }
00120 
00121 template <class OstreamType, class CharType>
00122 inline OstreamType &operator<< (OstreamType&lhs, const WeakString<CharType>&rhs) {
00123     size_t length = rhs.length();
00124     const CharType *data = rhs.data();
00125     for (size_t i = 0; i < length; i++) {
00126         lhs << data[i];
00127     }
00128     return lhs;
00129 }
00130 
00131 typedef WeakString<char> URLString;
00132 typedef WeakString<wchar_t> WideString;
00133 
00134 #if BERKELIUM_PLATFORM == PLATFORM_WINDOWS
00135 typedef WeakString<wchar_t> FileString;
00136 #else
00137 typedef WeakString<char> FileString;
00138 #endif
00139 
00140 }
00141 
00142 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated on 22 Nov 2013