1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla SVG project.
16 : *
17 : * Contributor(s):
18 : * Brian Birtles <birtles@gmail.com>
19 : *
20 : * Alternatively, the contents of this file may be used under the terms of
21 : * either the GNU General Public License Version 2 or later (the "GPL"), or
22 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 : * in which case the provisions of the GPL or the LGPL are applicable instead
24 : * of those above. If you wish to allow use of your version of this file only
25 : * under the terms of either the GPL or the LGPL, and not to allow others to
26 : * use your version of this file under the terms of the MPL, indicate your
27 : * decision by deleting the provisions above and replace them with the notice
28 : * and other provisions required by the GPL or the LGPL. If you do not delete
29 : * the provisions above, a recipient may use your version of this file under
30 : * the terms of any one of the MPL, the GPL or the LGPL.
31 : *
32 : * ***** END LICENSE BLOCK ***** */
33 :
34 : #ifndef NS_SVGATTRTEAROFFTABLE_H_
35 : #define NS_SVGATTRTEAROFFTABLE_H_
36 :
37 : #include "nsDataHashtable.h"
38 : #include "nsDebug.h"
39 : #include "nsHashKeys.h"
40 :
41 : /**
42 : * Global hashmap to associate internal SVG data types (e.g. nsSVGLength2) with
43 : * DOM tear-off objects (e.g. nsIDOMSVGLength). This allows us to always return
44 : * the same object for subsequent requests for DOM objects.
45 : *
46 : * We don't keep an owning reference to the tear-off objects so they are
47 : * responsible for removing themselves from this table when they die.
48 : */
49 : template<class SimpleType, class TearoffType>
50 : class nsSVGAttrTearoffTable
51 13176 : {
52 : public:
53 13383 : ~nsSVGAttrTearoffTable()
54 : {
55 13383 : NS_ABORT_IF_FALSE(mTable.Count() == 0,
56 : "Tear-off objects remain in hashtable at shutdown.");
57 13383 : }
58 :
59 : TearoffType* GetTearoff(SimpleType* aSimple);
60 :
61 : void AddTearoff(SimpleType* aSimple, TearoffType* aTearoff);
62 :
63 : void RemoveTearoff(SimpleType* aSimple);
64 :
65 : private:
66 : typedef nsPtrHashKey<SimpleType> SimpleTypePtrKey;
67 : typedef nsDataHashtable<SimpleTypePtrKey, TearoffType* > TearoffTable;
68 :
69 : TearoffTable mTable;
70 : };
71 :
72 : template<class SimpleType, class TearoffType>
73 : TearoffType*
74 0 : nsSVGAttrTearoffTable<SimpleType, TearoffType>::GetTearoff(SimpleType* aSimple)
75 : {
76 0 : if (!mTable.IsInitialized())
77 0 : return nsnull;
78 :
79 0 : TearoffType *tearoff = nsnull;
80 :
81 : #ifdef DEBUG
82 : bool found =
83 : #endif
84 0 : mTable.Get(aSimple, &tearoff);
85 0 : NS_ABORT_IF_FALSE(!found || tearoff,
86 : "NULL pointer stored in attribute tear-off map");
87 :
88 0 : return tearoff;
89 : }
90 :
91 : template<class SimpleType, class TearoffType>
92 : void
93 0 : nsSVGAttrTearoffTable<SimpleType, TearoffType>::AddTearoff(SimpleType* aSimple,
94 : TearoffType* aTearoff)
95 : {
96 0 : if (!mTable.IsInitialized()) {
97 0 : mTable.Init();
98 : }
99 :
100 : // We shouldn't be adding a tear-off if there already is one. If that happens,
101 : // something is wrong.
102 0 : if (mTable.Get(aSimple, nsnull)) {
103 0 : NS_ABORT_IF_FALSE(false, "There is already a tear-off for this object.");
104 0 : return;
105 : }
106 :
107 : #ifdef DEBUG
108 : bool result =
109 : #endif
110 0 : mTable.Put(aSimple, aTearoff);
111 0 : NS_ABORT_IF_FALSE(result, "Out of memory.");
112 : }
113 :
114 : template<class SimpleType, class TearoffType>
115 : void
116 0 : nsSVGAttrTearoffTable<SimpleType, TearoffType>::RemoveTearoff(
117 : SimpleType* aSimple)
118 : {
119 0 : if (!mTable.IsInitialized()) {
120 : // Perhaps something happened in between creating the SimpleType object and
121 : // registering it
122 0 : return;
123 : }
124 :
125 0 : mTable.Remove(aSimple);
126 : }
127 :
128 : #endif // NS_SVGATTRTEAROFFTABLE_H_
|