1/* 2 * Copyright (C) 2010 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 ArgumentEncoder_h 27#define ArgumentEncoder_h 28 29#include "ArgumentCoder.h" 30#include "Attachment.h" 31#include <wtf/Vector.h> 32 33namespace IPC { 34 35class ArgumentEncoder; 36class DataReference; 37 38class ArgumentEncoder { 39 WTF_MAKE_FAST_ALLOCATED; 40public: 41 ArgumentEncoder(); 42 virtual ~ArgumentEncoder(); 43 44 void encodeFixedLengthData(const uint8_t*, size_t, unsigned alignment); 45 void encodeVariableLengthByteArray(const DataReference&); 46 47 template<typename T> void encodeEnum(T t) 48 { 49 COMPILE_ASSERT(sizeof(T) <= sizeof(uint64_t), enum_type_must_not_be_larger_than_64_bits); 50 51 encode(static_cast<uint64_t>(t)); 52 } 53 54 template<typename T> void encode(const T& t) 55 { 56 ArgumentCoder<T>::encode(*this, t); 57 } 58 59 template<typename T> ArgumentEncoder& operator<<(const T& t) 60 { 61 encode(t); 62 return *this; 63 } 64 65 uint8_t* buffer() const { return m_buffer; } 66 size_t bufferSize() const { return m_bufferSize; } 67 68 void addAttachment(const Attachment&); 69 Vector<Attachment> releaseAttachments(); 70 void reserve(size_t); 71 72private: 73 void encode(bool); 74 void encode(uint8_t); 75 void encode(uint16_t); 76 void encode(uint32_t); 77 void encode(uint64_t); 78 void encode(int32_t); 79 void encode(int64_t); 80 void encode(float); 81 void encode(double); 82 83 uint8_t* grow(unsigned alignment, size_t size); 84 85 uint8_t m_inlineBuffer[512]; 86 87 uint8_t* m_buffer; 88 uint8_t* m_bufferPointer; 89 90 size_t m_bufferSize; 91 size_t m_bufferCapacity; 92 93 Vector<Attachment> m_attachments; 94}; 95 96} // namespace IPC 97 98#endif // ArgumentEncoder_h 99