]> git.saurik.com Git - iphone-api.git/blob - WebCore/SharedBuffer.h
Adding the WebCore headers (for Cydget).
[iphone-api.git] / WebCore / SharedBuffer.h
1 /*
2 * Copyright (C) 2006 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #ifndef SharedBuffer_h
26 #define SharedBuffer_h
27
28 #include "PlatformString.h"
29 #include <wtf/Forward.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/RefCounted.h>
32 #include <wtf/Vector.h>
33
34 #if PLATFORM(CF)
35 #include <wtf/RetainPtr.h>
36 #endif
37
38 #if PLATFORM(MAC)
39 #ifdef __OBJC__
40 @class NSData;
41 #else
42 class NSData;
43 #endif
44
45 #endif
46
47 namespace WebCore {
48
49 class PurgeableBuffer;
50
51 class SharedBuffer : public RefCounted<SharedBuffer> {
52 public:
53 static PassRefPtr<SharedBuffer> create() { return adoptRef(new SharedBuffer); }
54 static PassRefPtr<SharedBuffer> create(const char* c, int i) { return adoptRef(new SharedBuffer(c, i)); }
55 static PassRefPtr<SharedBuffer> create(const unsigned char* c, int i) { return adoptRef(new SharedBuffer(c, i)); }
56
57 static PassRefPtr<SharedBuffer> createWithContentsOfFile(const String& filePath);
58
59 static PassRefPtr<SharedBuffer> adoptVector(Vector<char>& vector);
60
61 // The buffer must be in non-purgeable state before adopted to a SharedBuffer.
62 // It will stay that way until released.
63 static PassRefPtr<SharedBuffer> adoptPurgeableBuffer(PurgeableBuffer* buffer);
64
65 ~SharedBuffer();
66
67 #if PLATFORM(MAC)
68 NSData *createNSData();
69 static PassRefPtr<SharedBuffer> wrapNSData(NSData *data);
70 #endif
71 #if PLATFORM(CF)
72 CFDataRef createCFData();
73 #endif
74
75 const char* data() const;
76 unsigned size() const;
77 const Vector<char> &buffer() { return m_buffer; }
78
79 bool isEmpty() const { return size() == 0; }
80
81 void append(const char*, int);
82 void clear();
83 const char* platformData() const;
84 unsigned platformDataSize() const;
85
86 PassRefPtr<SharedBuffer> copy() const;
87
88 bool hasPurgeableBuffer() const { return m_purgeableBuffer.get(); }
89
90 // Ensure this buffer has no other clients before calling this.
91 PurgeableBuffer* releasePurgeableBuffer();
92
93 private:
94 SharedBuffer();
95 SharedBuffer(const char*, int);
96 SharedBuffer(const unsigned char*, int);
97
98 void clearPlatformData();
99 void maybeTransferPlatformData();
100 bool hasPlatformData() const;
101
102 Vector<char> m_buffer;
103 OwnPtr<PurgeableBuffer> m_purgeableBuffer;
104 #if PLATFORM(CF)
105 SharedBuffer(CFDataRef);
106 RetainPtr<CFDataRef> m_cfData;
107 #endif
108 };
109
110 }
111
112 #endif