2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ArgumentDecoder_h
27 #define ArgumentDecoder_h
29 #include "ArgumentCoder.h"
30 #include "Attachment.h"
31 #include <wtf/Deque.h>
32 #include <wtf/TypeTraits.h>
33 #include <wtf/Vector.h>
39 class ArgumentDecoder {
41 ArgumentDecoder(const uint8_t* buffer, size_t bufferSize);
42 ArgumentDecoder(const uint8_t* buffer, size_t bufferSize, Deque<Attachment>&);
45 uint64_t destinationID() const { return m_destinationID; }
47 bool isInvalid() const { return m_bufferPos > m_bufferEnd; }
48 void markInvalid() { m_bufferPos = m_bufferEnd + 1; }
50 bool decodeFixedLengthData(uint8_t*, size_t, unsigned alignment);
52 // The data in the data reference here will only be valid for the lifetime of the ArgumentDecoder object.
53 bool decodeVariableLengthByteArray(DataReference&);
55 bool decodeBool(bool&);
56 bool decodeUInt32(uint32_t&);
57 bool decodeUInt64(uint64_t&);
58 bool decodeInt32(int32_t&);
59 bool decodeInt64(int64_t&);
60 bool decodeFloat(float&);
61 bool decodeDouble(double&);
63 template<typename T> bool decodeEnum(T& result)
65 COMPILE_ASSERT(sizeof(T) <= sizeof(uint64_t), enum_type_must_not_be_larger_than_64_bits);
68 if (!decodeUInt64(value))
71 result = static_cast<T>(value);
76 bool bufferIsLargeEnoughToContain(size_t numElements) const
78 COMPILE_ASSERT(WTF::IsArithmetic<T>::value, type_must_have_known_encoded_size);
80 if (numElements > std::numeric_limits<size_t>::max() / sizeof(T))
83 return bufferIsLargeEnoughToContain(__alignof(T), numElements * sizeof(T));
86 // Generic type decode function.
87 template<typename T> bool decode(T& t)
89 return ArgumentCoder<T>::decode(this, t);
92 // This overload exists so we can pass temporaries to decode. In the Star Trek future, it
93 // can take an rvalue reference instead.
94 template<typename T> bool decode(const T& t)
96 return decode(const_cast<T&>(t));
99 bool removeAttachment(Attachment&);
106 ArgumentDecoder(const ArgumentDecoder*);
107 ArgumentDecoder* operator=(const ArgumentDecoder*);
109 void initialize(const uint8_t* buffer, size_t bufferSize);
111 bool alignBufferPosition(unsigned alignment, size_t size);
112 bool bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const;
114 uint64_t m_destinationID;
116 uint8_t* m_allocatedBase;
118 uint8_t* m_bufferPos;
119 uint8_t* m_bufferEnd;
121 Deque<Attachment> m_attachments;
124 template<> inline bool ArgumentDecoder::decode(bool& n)
126 return decodeBool(n);
129 template<> inline bool ArgumentDecoder::decode(uint32_t& n)
131 return decodeUInt32(n);
134 template<> inline bool ArgumentDecoder::decode(uint64_t& n)
136 return decodeUInt64(n);
139 template<> inline bool ArgumentDecoder::decode(int32_t& n)
141 return decodeInt32(n);
144 template<> inline bool ArgumentDecoder::decode(int64_t& n)
146 return decodeInt64(n);
149 template<> inline bool ArgumentDecoder::decode(float& n)
151 return decodeFloat(n);
154 template<> inline bool ArgumentDecoder::decode(double& n)
156 return decodeDouble(n);
159 } // namespace CoreIPC
161 #endif // ArgumentDecoder_h