Bug 1186793 - Replace nsBaseHashtable::EnumerateRead() calls in gfx/ with iterators r=njn

This commit is contained in:
Sotaro Ikeda 2015-09-29 07:49:41 -07:00
parent 720ccd1f08
commit c32fc36944
3 changed files with 70 additions and 115 deletions

View File

@ -892,51 +892,6 @@ struct ParamTraits<mozilla::layers::EventRegions>
} }
}; };
struct MessageAndAttributeMap
{
Message* msg;
const mozilla::gfx::AttributeMap& map;
};
static bool
WriteAttribute(mozilla::gfx::AttributeName aName,
mozilla::gfx::AttributeType aType,
void* aUserData)
{
MessageAndAttributeMap* msgAndMap =
static_cast<MessageAndAttributeMap*>(aUserData);
WriteParam(msgAndMap->msg, aType);
WriteParam(msgAndMap->msg, aName);
switch (aType) {
#define HANDLE_TYPE(typeName) \
case mozilla::gfx::AttributeType::e##typeName: \
WriteParam(msgAndMap->msg, msgAndMap->map.Get##typeName(aName)); \
break;
HANDLE_TYPE(Bool)
HANDLE_TYPE(Uint)
HANDLE_TYPE(Float)
HANDLE_TYPE(Size)
HANDLE_TYPE(IntSize)
HANDLE_TYPE(IntPoint)
HANDLE_TYPE(Matrix)
HANDLE_TYPE(Matrix5x4)
HANDLE_TYPE(Point3D)
HANDLE_TYPE(Color)
HANDLE_TYPE(AttributeMap)
HANDLE_TYPE(Floats)
#undef HANDLE_TYPE
default:
MOZ_CRASH("unhandled attribute type");
}
return true;
}
template <> template <>
struct ParamTraits<mozilla::gfx::AttributeMap> struct ParamTraits<mozilla::gfx::AttributeMap>
{ {
@ -945,8 +900,41 @@ struct ParamTraits<mozilla::gfx::AttributeMap>
static void Write(Message* aMsg, const paramType& aParam) static void Write(Message* aMsg, const paramType& aParam)
{ {
WriteParam(aMsg, aParam.Count()); WriteParam(aMsg, aParam.Count());
MessageAndAttributeMap msgAndMap = { aMsg, aParam }; for (auto iter = aParam.ConstIter(); !iter.Done(); iter.Next()) {
aParam.EnumerateRead(WriteAttribute, &msgAndMap); mozilla::gfx::AttributeName name =
mozilla::gfx::AttributeName(iter.Key());
mozilla::gfx::AttributeType type =
mozilla::gfx::AttributeMap::GetType(iter.UserData());
WriteParam(aMsg, type);
WriteParam(aMsg, name);
switch (type) {
#define CASE_TYPE(typeName) \
case mozilla::gfx::AttributeType::e##typeName: \
WriteParam(aMsg, aParam.Get##typeName(name)); \
break;
CASE_TYPE(Bool)
CASE_TYPE(Uint)
CASE_TYPE(Float)
CASE_TYPE(Size)
CASE_TYPE(IntSize)
CASE_TYPE(IntPoint)
CASE_TYPE(Matrix)
CASE_TYPE(Matrix5x4)
CASE_TYPE(Point3D)
CASE_TYPE(Color)
CASE_TYPE(AttributeMap)
CASE_TYPE(Floats)
#undef CASE_TYPE
default:
MOZ_CRASH("unhandled attribute type");
}
}
} }
static bool Read(const Message* aMsg, void** aIter, paramType* aResult) static bool Read(const Message* aMsg, void** aIter, paramType* aResult)

View File

@ -2045,20 +2045,13 @@ AttributeMap::~AttributeMap()
{ {
} }
static PLDHashOperator
CopyAttribute(const uint32_t& aAttributeName,
Attribute* aAttribute,
void* aAttributes)
{
typedef nsClassHashtable<nsUint32HashKey, Attribute> Map;
Map* map = static_cast<Map*>(aAttributes);
map->Put(aAttributeName, new Attribute(*aAttribute));
return PL_DHASH_NEXT;
}
AttributeMap::AttributeMap(const AttributeMap& aOther) AttributeMap::AttributeMap(const AttributeMap& aOther)
{ {
aOther.mMap.EnumerateRead(CopyAttribute, &mMap); for (auto iter = aOther.mMap.Iter(); !iter.Done(); iter.Next()) {
const uint32_t& attributeName = iter.Key();
Attribute* attribute = iter.UserData();
mMap.Put(attributeName, new Attribute(*attribute));
}
} }
AttributeMap& AttributeMap&
@ -2066,34 +2059,15 @@ AttributeMap::operator=(const AttributeMap& aOther)
{ {
if (this != &aOther) { if (this != &aOther) {
mMap.Clear(); mMap.Clear();
aOther.mMap.EnumerateRead(CopyAttribute, &mMap); for (auto iter = aOther.mMap.Iter(); !iter.Done(); iter.Next()) {
const uint32_t& attributeName = iter.Key();
Attribute* attribute = iter.UserData();
mMap.Put(attributeName, new Attribute(*attribute));
}
} }
return *this; return *this;
} }
namespace {
struct MatchingMap {
typedef nsClassHashtable<nsUint32HashKey, Attribute> Map;
const Map& map;
bool matches;
};
} // namespace
static PLDHashOperator
CheckAttributeEquality(const uint32_t& aAttributeName,
Attribute* aAttribute,
void* aMatchingMap)
{
MatchingMap& matchingMap = *static_cast<MatchingMap*>(aMatchingMap);
Attribute* matchingAttribute = matchingMap.map.Get(aAttributeName);
if (!matchingAttribute ||
*matchingAttribute != *aAttribute) {
matchingMap.matches = false;
return PL_DHASH_STOP;
}
return PL_DHASH_NEXT;
}
bool bool
AttributeMap::operator==(const AttributeMap& aOther) const AttributeMap::operator==(const AttributeMap& aOther) const
{ {
@ -2101,37 +2075,16 @@ AttributeMap::operator==(const AttributeMap& aOther) const
return false; return false;
} }
MatchingMap matchingMap = { mMap, true }; for (auto iter = aOther.mMap.Iter(); !iter.Done(); iter.Next()) {
aOther.mMap.EnumerateRead(CheckAttributeEquality, &matchingMap); const uint32_t& attributeName = iter.Key();
return matchingMap.matches; Attribute* attribute = iter.UserData();
} Attribute* matchingAttribute = mMap.Get(attributeName);
if (!matchingAttribute || *matchingAttribute != *attribute) {
return false;
}
}
namespace { return true;
struct HandlerWithUserData
{
AttributeMap::AttributeHandleCallback handler;
void* userData;
};
} // namespace
static PLDHashOperator
PassAttributeToHandleCallback(const uint32_t& aAttributeName,
Attribute* aAttribute,
void* aHandlerWithUserData)
{
HandlerWithUserData* handlerWithUserData =
static_cast<HandlerWithUserData*>(aHandlerWithUserData);
return handlerWithUserData->handler(AttributeName(aAttributeName),
aAttribute->Type(),
handlerWithUserData->userData) ?
PL_DHASH_NEXT : PL_DHASH_STOP;
}
void
AttributeMap::EnumerateRead(AttributeMap::AttributeHandleCallback aCallback, void* aUserData) const
{
HandlerWithUserData handlerWithUserData = { aCallback, aUserData };
mMap.EnumerateRead(PassAttributeToHandleCallback, &handlerWithUserData);
} }
uint32_t uint32_t
@ -2140,6 +2093,18 @@ AttributeMap::Count() const
return mMap.Count(); return mMap.Count();
} }
nsClassHashtable<nsUint32HashKey, FilterAttribute>::Iterator
AttributeMap::ConstIter() const
{
return mMap.ConstIter();
}
/* static */ AttributeType
AttributeMap::GetType(FilterAttribute* aAttribute)
{
return aAttribute->Type();
}
#define MAKE_ATTRIBUTE_HANDLERS_BASIC(type, typeLabel, defaultValue) \ #define MAKE_ATTRIBUTE_HANDLERS_BASIC(type, typeLabel, defaultValue) \
type \ type \
AttributeMap::Get##typeLabel(AttributeName aName) const { \ AttributeMap::Get##typeLabel(AttributeName aName) const { \

View File

@ -221,10 +221,12 @@ public:
AttributeMap GetAttributeMap(AttributeName aName) const; AttributeMap GetAttributeMap(AttributeName aName) const;
const nsTArray<float>& GetFloats(AttributeName aName) const; const nsTArray<float>& GetFloats(AttributeName aName) const;
typedef bool (*AttributeHandleCallback)(AttributeName aName, AttributeType aType, void* aUserData);
void EnumerateRead(AttributeHandleCallback aCallback, void* aUserData) const;
uint32_t Count() const; uint32_t Count() const;
nsClassHashtable<nsUint32HashKey, FilterAttribute>::Iterator ConstIter() const;
static AttributeType GetType(FilterAttribute* aAttribute);
private: private:
mutable nsClassHashtable<nsUint32HashKey, FilterAttribute> mMap; mutable nsClassHashtable<nsUint32HashKey, FilterAttribute> mMap;
}; };