Changeset 132169 in webkit


Ignore:
Timestamp:
Oct 22, 2012 5:58:14 PM (12 years ago)
Author:
andersca@apple.com
Message:

Handle ArgumentCoder template specializations that take the ArgumentEncoder as a reference
https://bugs.webkit.org/show_bug.cgi?id=100056

Reviewed by Andreas Kling.

Use template magic to make it possible to have ArgumentCoder specializations where the encode
function takes the ArgumentEncoder object as a reference instead of as a pointer. Also, add an
operator<< to ArgumentEncoder and change the string related ArgumentCoder specializations over to taking
the encoder as a reference and using stream operators.

  • Platform/CoreIPC/ArgumentCoders.cpp:

(CoreIPC::::encode):

  • Platform/CoreIPC/ArgumentCoders.h:
  • Platform/CoreIPC/ArgumentEncoder.h:

(ArgumentEncoder):
(UsesDeprecatedEncodeFunction):
(NoType):
(CoreIPC::ArgumentEncoder::encode):
(CoreIPC::ArgumentEncoder::operator<<):

Location:
trunk/Source/WebKit2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r132161 r132169  
     12012-10-22  Anders Carlsson  <andersca@apple.com>
     2
     3        Handle ArgumentCoder template specializations that take the ArgumentEncoder as a reference
     4        https://bugs.webkit.org/show_bug.cgi?id=100056
     5
     6        Reviewed by Andreas Kling.
     7
     8        Use template magic to make it possible to have ArgumentCoder specializations where the encode
     9        function takes the ArgumentEncoder object as a reference instead of as a pointer. Also, add an
     10        operator<< to ArgumentEncoder and change the string related ArgumentCoder specializations over to taking
     11        the encoder as a reference and using stream operators.
     12
     13        * Platform/CoreIPC/ArgumentCoders.cpp:
     14        (CoreIPC::::encode):
     15        * Platform/CoreIPC/ArgumentCoders.h:
     16        * Platform/CoreIPC/ArgumentEncoder.h:
     17        (ArgumentEncoder):
     18        (UsesDeprecatedEncodeFunction):
     19        (NoType):
     20        (CoreIPC::ArgumentEncoder::encode):
     21        (CoreIPC::ArgumentEncoder::operator<<):
     22
    1232012-10-22  Anders Carlsson  <andersca@apple.com>
    224
  • trunk/Source/WebKit2/Platform/CoreIPC/ArgumentCoders.cpp

    r131990 r132169  
    2727#include "ArgumentCoders.h"
    2828
     29#include "DataReference.h"
    2930#include <wtf/text/CString.h>
    3031#include <wtf/text/WTFString.h>
     
    3233namespace CoreIPC {
    3334
    34 void ArgumentCoder<AtomicString>::encode(ArgumentEncoder* encoder, const AtomicString& atomicString)
     35void ArgumentCoder<AtomicString>::encode(ArgumentEncoder& encoder, const AtomicString& atomicString)
    3536{
    36     encoder->encode(atomicString.string());
     37    encoder << atomicString.string();
    3738}
    3839
     
    4748}
    4849
    49 void ArgumentCoder<CString>::encode(ArgumentEncoder* encoder, const CString& string)
     50void ArgumentCoder<CString>::encode(ArgumentEncoder& encoder, const CString& string)
    5051{
    5152    // Special case the null string.
    5253    if (string.isNull()) {
    53         encoder->encode(std::numeric_limits<uint32_t>::max());
     54        encoder << std::numeric_limits<uint32_t>::max();
    5455        return;
    5556    }
    5657
    5758    uint32_t length = string.length();
    58     encoder->encode(length);
    59     encoder->encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.data()), length, 1);
     59    encoder << length << CoreIPC::DataReference(reinterpret_cast<const uint8_t*>(string.data()), length);
    6060}
    6161
     
    8888
    8989
    90 void ArgumentCoder<String>::encode(ArgumentEncoder* encoder, const String& string)
     90void ArgumentCoder<String>::encode(ArgumentEncoder& encoder, const String& string)
    9191{
    9292    // Special case the null string.
    9393    if (string.isNull()) {
    94         encoder->encode(std::numeric_limits<uint32_t>::max());
     94        encoder << std::numeric_limits<uint32_t>::max();
    9595        return;
    9696    }
    9797
    9898    uint32_t length = string.length();
    99     encoder->encode(length);
    10099    bool is8Bit = string.is8Bit();
    101     encoder->encode(is8Bit);
     100
     101    encoder << length << is8Bit;
     102
    102103    if (is8Bit)
    103         encoder->encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters8()), length * sizeof(LChar), __alignof(LChar));
     104        encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters8()), length * sizeof(LChar), __alignof(LChar));
    104105    else
    105         encoder->encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters16()), length * sizeof(UChar), __alignof(UChar));
     106        encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters16()), length * sizeof(UChar), __alignof(UChar));
    106107}
    107108
  • trunk/Source/WebKit2/Platform/CoreIPC/ArgumentCoders.h

    r132161 r132169  
    198198
    199199template<> struct ArgumentCoder<AtomicString> {
    200     static void encode(ArgumentEncoder*, const AtomicString&);
     200    static void encode(ArgumentEncoder&, const AtomicString&);
    201201    static bool decode(ArgumentDecoder*, AtomicString&);
    202202};
    203203
    204204template<> struct ArgumentCoder<CString> {
    205     static void encode(ArgumentEncoder*, const CString&);
     205    static void encode(ArgumentEncoder&, const CString&);
    206206    static bool decode(ArgumentDecoder*, CString&);
    207207};
    208208
    209209template<> struct ArgumentCoder<String> {
    210     static void encode(ArgumentEncoder*, const String&);
     210    static void encode(ArgumentEncoder&, const String&);
    211211    static bool decode(ArgumentDecoder*, String&);
    212212};
  • trunk/Source/WebKit2/Platform/CoreIPC/ArgumentEncoder.h

    r131990 r132169  
    6161        encode(static_cast<uint64_t>(t));
    6262    }
     63
     64    template<bool B, typename T = void>
     65    struct EnableIf { };
     66
     67    template<typename T>
     68    struct EnableIf<true, T> { typedef T Type; };
    6369   
    64     // Generic type encode function.
    65     template<typename T> void encode(const T& t)
     70    template<typename T> class UsesDeprecatedEncodeFunction {
     71        typedef char YesType;
     72        struct NoType {
     73            char padding[8];
     74        };
     75
     76        static YesType checkEncode(void (*)(ArgumentEncoder*, const T&));
     77        static NoType checkEncode(...);
     78
     79    public:
     80        static const bool value = sizeof(checkEncode(ArgumentCoder<T>::encode)) == sizeof(YesType);
     81    };
     82
     83    // FIXME: This is the function that gets chosen if the argument coder takes the ArgumentEncoder as a pointer.
     84    // This is the deprecated form - get rid of it.
     85    template<typename T> void encode(const T& t, typename EnableIf<UsesDeprecatedEncodeFunction<T>::value>::Type* = 0)
    6686    {
    6787        ArgumentCoder<T>::encode(this, t);
     88    }
     89
     90    template<typename T> void encode(const T& t, typename EnableIf<!UsesDeprecatedEncodeFunction<T>::value>::Type* = 0)
     91    {
     92        ArgumentCoder<T>::encode(*this, t);
     93    }
     94
     95    template<typename T> ArgumentEncoder& operator<<(const T& t)
     96    {
     97        encode(t);
     98        return *this;
    6899    }
    69100
Note: See TracChangeset for help on using the changeset viewer.