2009-08-26 21:30:51 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-08-26 21:30:51 -07:00
|
|
|
|
|
|
|
#ifndef NS_SVGATTRTEAROFFTABLE_H_
|
|
|
|
#define NS_SVGATTRTEAROFFTABLE_H_
|
|
|
|
|
|
|
|
#include "nsDataHashtable.h"
|
2012-01-26 01:57:21 -08:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsHashKeys.h"
|
2009-08-26 21:30:51 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Global hashmap to associate internal SVG data types (e.g. nsSVGLength2) with
|
|
|
|
* DOM tear-off objects (e.g. nsIDOMSVGLength). This allows us to always return
|
|
|
|
* the same object for subsequent requests for DOM objects.
|
|
|
|
*
|
|
|
|
* We don't keep an owning reference to the tear-off objects so they are
|
|
|
|
* responsible for removing themselves from this table when they die.
|
|
|
|
*/
|
|
|
|
template<class SimpleType, class TearoffType>
|
|
|
|
class nsSVGAttrTearoffTable
|
|
|
|
{
|
|
|
|
public:
|
2013-09-02 01:41:57 -07:00
|
|
|
#ifdef DEBUG
|
2009-08-26 21:30:51 -07:00
|
|
|
~nsSVGAttrTearoffTable()
|
|
|
|
{
|
2013-09-02 01:41:57 -07:00
|
|
|
NS_ABORT_IF_FALSE(!mTable, "Tear-off objects remain in hashtable at shutdown.");
|
2009-08-26 21:30:51 -07:00
|
|
|
}
|
2013-09-02 01:41:57 -07:00
|
|
|
#endif
|
2009-08-26 21:30:51 -07:00
|
|
|
|
|
|
|
TearoffType* GetTearoff(SimpleType* aSimple);
|
|
|
|
|
|
|
|
void AddTearoff(SimpleType* aSimple, TearoffType* aTearoff);
|
|
|
|
|
|
|
|
void RemoveTearoff(SimpleType* aSimple);
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef nsPtrHashKey<SimpleType> SimpleTypePtrKey;
|
|
|
|
typedef nsDataHashtable<SimpleTypePtrKey, TearoffType* > TearoffTable;
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
TearoffTable* mTable;
|
2009-08-26 21:30:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class SimpleType, class TearoffType>
|
|
|
|
TearoffType*
|
|
|
|
nsSVGAttrTearoffTable<SimpleType, TearoffType>::GetTearoff(SimpleType* aSimple)
|
|
|
|
{
|
2013-09-02 01:41:57 -07:00
|
|
|
if (!mTable)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2009-08-26 21:30:51 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
TearoffType *tearoff = nullptr;
|
2010-07-11 05:30:35 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found =
|
2010-07-11 05:30:35 -07:00
|
|
|
#endif
|
2013-09-02 01:41:57 -07:00
|
|
|
mTable->Get(aSimple, &tearoff);
|
2009-08-26 21:30:51 -07:00
|
|
|
NS_ABORT_IF_FALSE(!found || tearoff,
|
|
|
|
"NULL pointer stored in attribute tear-off map");
|
|
|
|
|
|
|
|
return tearoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class SimpleType, class TearoffType>
|
|
|
|
void
|
|
|
|
nsSVGAttrTearoffTable<SimpleType, TearoffType>::AddTearoff(SimpleType* aSimple,
|
|
|
|
TearoffType* aTearoff)
|
|
|
|
{
|
2013-09-02 01:41:57 -07:00
|
|
|
if (!mTable) {
|
|
|
|
mTable = new TearoffTable;
|
2009-08-26 21:30:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We shouldn't be adding a tear-off if there already is one. If that happens,
|
|
|
|
// something is wrong.
|
2013-09-02 01:41:57 -07:00
|
|
|
if (mTable->Get(aSimple, nullptr)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ABORT_IF_FALSE(false, "There is already a tear-off for this object.");
|
2009-08-26 21:30:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
mTable->Put(aSimple, aTearoff);
|
2009-08-26 21:30:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class SimpleType, class TearoffType>
|
|
|
|
void
|
|
|
|
nsSVGAttrTearoffTable<SimpleType, TearoffType>::RemoveTearoff(
|
|
|
|
SimpleType* aSimple)
|
|
|
|
{
|
2013-09-02 01:41:57 -07:00
|
|
|
if (!mTable) {
|
2009-08-26 21:30:51 -07:00
|
|
|
// Perhaps something happened in between creating the SimpleType object and
|
|
|
|
// registering it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
mTable->Remove(aSimple);
|
|
|
|
if (mTable->Count() == 0) {
|
|
|
|
delete mTable;
|
|
|
|
mTable = nullptr;
|
|
|
|
}
|
2009-08-26 21:30:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // NS_SVGATTRTEAROFFTABLE_H_
|