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 <string.h>
30#include <wtf/Forward.h>
31#include <wtf/HashTraits.h>
32
33namespace IPC {
34
35class ArgumentEncoder;
36class ArgumentDecoder;
37
38class StringReference {
39public:
40    StringReference()
41        : m_data(0)
42        , m_size(0)
43    {
44    }
45
46    StringReference(const char* data, size_t size)
47        : m_data(data)
48        , m_size(size)
49    {
50    }
51
52    template<size_t length>
53    StringReference(const char (&string)[length])
54        : m_data(string)
55        , m_size(length - 1)
56    {
57    }
58
59    bool isEmpty() const { return !m_size; }
60
61    size_t size() const { return m_size; }
62    const char* data() const { return m_data; }
63
64    CString toString() const;
65
66    friend bool operator==(const StringReference& a, const StringReference& b)
67    {
68        return a.m_size == b.m_size && !memcmp(a.m_data, b.m_data, a.m_size);
69    }
70
71    void encode(ArgumentEncoder&) const;
72    static bool decode(ArgumentDecoder&, StringReference&);
73
74    struct Hash {
75        static unsigned hash(const StringReference& a);
76        static bool equal(const StringReference& a, const StringReference& b) { return a == b; }
77        static const bool safeToCompareToEmptyOrDeleted = true;
78    };
79
80private:
81    const char* m_data;
82    size_t m_size;
83};
84
85} // namespace IPC
86
87namespace WTF {
88template<typename T> struct DefaultHash;
89
90template<> struct DefaultHash<IPC::StringReference> {
91    typedef IPC::StringReference::Hash Hash;
92};
93
94template<> struct HashTraits<IPC::StringReference> : GenericHashTraits<IPC::StringReference> {
95    static const bool emptyValueIsZero = 0;
96    static void constructDeletedValue(IPC::StringReference& stringReference) { stringReference = IPC::StringReference(0, std::numeric_limits<size_t>::max()); }
97    static bool isDeletedValue(const IPC::StringReference& stringReference) { return stringReference.size() == std::numeric_limits<size_t>::max(); }
98};
99
100} // namespace WTF
101
102#endif // StringReference_h
103