1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef StringReference_h
27#define StringReference_h
28
29#include <wtf/Forward.h>
30#include <wtf/HashTraits.h>
31
32namespace CoreIPC {
33
34class ArgumentEncoder;
35class ArgumentDecoder;
36
37class StringReference {
38public:
39    StringReference()
40        : m_data(0)
41        , m_size(0)
42    {
43    }
44
45    StringReference(const char* data, size_t size)
46        : m_data(data)
47        , m_size(size)
48    {
49    }
50
51    template<size_t length>
52    StringReference(const char (&string)[length])
53        : m_data(string)
54        , m_size(length - 1)
55    {
56    }
57
58    bool isEmpty() const { return !m_size; }
59
60    size_t size() const { return m_size; }
61    const char* data() const { return m_data; }
62
63    CString toString() const;
64
65    friend bool operator==(const StringReference& a, const StringReference& b)
66    {
67        return a.m_size == b.m_size && !memcmp(a.m_data, b.m_data, a.m_size);
68    }
69
70    void encode(ArgumentEncoder&) const;
71    static bool decode(ArgumentDecoder&, StringReference&);
72
73    struct Hash {
74        static unsigned hash(const StringReference& a);
75        static bool equal(const StringReference& a, const StringReference& b) { return a == b; }
76        static const bool safeToCompareToEmptyOrDeleted = true;
77    };
78
79private:
80    const char* m_data;
81    size_t m_size;
82};
83
84} // namespace CoreIPC
85
86namespace WTF {
87template<typename T> struct DefaultHash;
88
89template<> struct DefaultHash<CoreIPC::StringReference> {
90    typedef CoreIPC::StringReference::Hash Hash;
91};
92
93template<> struct HashTraits<CoreIPC::StringReference> : GenericHashTraits<CoreIPC::StringReference> {
94    static const bool emptyValueIsZero = 0;
95    static void constructDeletedValue(CoreIPC::StringReference& stringReference) { stringReference = CoreIPC::StringReference(0, std::numeric_limits<size_t>::max()); }
96    static bool isDeletedValue(const CoreIPC::StringReference& stringReference) { return stringReference.size() == std::numeric_limits<size_t>::max(); }
97};
98
99} // namespace WTF
100
101#endif // StringReference_h
102