1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_SVG_SVGATTRTEAROFFTABLE_H_
8 #define DOM_SVG_SVGATTRTEAROFFTABLE_H_
10 #include "mozilla/DebugOnly.h"
11 #include "mozilla/StaticPtr.h"
12 #include "nsTHashMap.h"
14 #include "nsHashKeys.h"
19 * Global hashmap to associate internal SVG data types (e.g. SVGAnimatedLength)
20 * with DOM tear-off objects (e.g. DOMSVGLength). This allows us to always
21 * return the same object for subsequent requests for DOM objects.
23 * We don't keep an owning reference to the tear-off objects so they are
24 * responsible for removing themselves from this table when they die.
26 template <class SimpleType
, class TearoffType
>
27 class SVGAttrTearoffTable
{
30 ~SVGAttrTearoffTable() {
31 NS_ASSERTION(!mTable
, "Tear-off objects remain in hashtable at shutdown.");
35 TearoffType
* GetTearoff(SimpleType
* aSimple
);
37 void AddTearoff(SimpleType
* aSimple
, TearoffType
* aTearoff
);
39 void RemoveTearoff(SimpleType
* aSimple
);
42 using SimpleTypePtrKey
= nsPtrHashKey
<SimpleType
>;
43 using TearoffTable
= nsTHashMap
<SimpleTypePtrKey
, TearoffType
*>;
45 StaticAutoPtr
<TearoffTable
> mTable
;
48 template <class SimpleType
, class TearoffType
>
49 TearoffType
* SVGAttrTearoffTable
<SimpleType
, TearoffType
>::GetTearoff(
50 SimpleType
* aSimple
) {
55 TearoffType
* tearoff
= nullptr;
57 DebugOnly
<bool> found
= mTable
->Get(aSimple
, &tearoff
);
58 MOZ_ASSERT(!found
|| tearoff
,
59 "null pointer stored in attribute tear-off map");
64 template <class SimpleType
, class TearoffType
>
65 void SVGAttrTearoffTable
<SimpleType
, TearoffType
>::AddTearoff(
66 SimpleType
* aSimple
, TearoffType
* aTearoff
) {
68 mTable
= new TearoffTable();
71 // We shouldn't be adding a tear-off if there already is one. If that happens,
72 // something is wrong.
73 if (mTable
->Get(aSimple
, nullptr)) {
74 MOZ_ASSERT(false, "There is already a tear-off for this object.");
78 mTable
->InsertOrUpdate(aSimple
, aTearoff
);
81 template <class SimpleType
, class TearoffType
>
82 void SVGAttrTearoffTable
<SimpleType
, TearoffType
>::RemoveTearoff(
83 SimpleType
* aSimple
) {
85 // Perhaps something happened in between creating the SimpleType object and
90 mTable
->Remove(aSimple
);
91 if (mTable
->Count() == 0) {
96 } // namespace mozilla
98 #endif // DOM_SVG_SVGATTRTEAROFFTABLE_H_