/src/mozilla-central/dom/xslt/xslt/txVariableMap.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #ifndef TRANSFRMX_VARIABLEMAP_H |
7 | | #define TRANSFRMX_VARIABLEMAP_H |
8 | | |
9 | | #include "nsError.h" |
10 | | #include "txXMLUtils.h" |
11 | | #include "txExprResult.h" |
12 | | #include "txExpandedNameMap.h" |
13 | | |
14 | | /** |
15 | | * Map that maps from expanded name to an expression result value. This is just a base |
16 | | * class, use txVariableMap or txParameterMap instead. |
17 | | */ |
18 | | class txVariableMapBase { |
19 | | public: |
20 | | nsresult bindVariable(const txExpandedName& aName, txAExprResult* aValue); |
21 | | |
22 | | void getVariable(const txExpandedName& aName, txAExprResult** aResult); |
23 | | |
24 | | void removeVariable(const txExpandedName& aName); |
25 | | |
26 | | protected: |
27 | | txVariableMapBase() |
28 | 0 | {} |
29 | | ~txVariableMapBase(); |
30 | | |
31 | | txExpandedNameMap<txAExprResult> mMap; |
32 | | }; |
33 | | |
34 | | /** |
35 | | * Map for mapping from expanded name to variable values. This is not refcounted, so |
36 | | * owners need to be careful to clean this up. |
37 | | */ |
38 | | class txVariableMap : public txVariableMapBase { |
39 | | public: |
40 | | txVariableMap() |
41 | | : txVariableMapBase() |
42 | 0 | { |
43 | 0 | MOZ_COUNT_CTOR(txVariableMap); |
44 | 0 | } |
45 | | ~txVariableMap() |
46 | 0 | { |
47 | 0 | MOZ_COUNT_DTOR(txVariableMap); |
48 | 0 | } |
49 | | }; |
50 | | |
51 | | /** |
52 | | * Map for mapping from expanded name to parameter values. This is refcounted, so multiple |
53 | | * owners can hold a reference. |
54 | | */ |
55 | | class txParameterMap : public txVariableMapBase { |
56 | | public: |
57 | | NS_INLINE_DECL_REFCOUNTING(txParameterMap) |
58 | | |
59 | | private: |
60 | | ~txParameterMap() |
61 | 0 | {} |
62 | | }; |
63 | | |
64 | | inline |
65 | | txVariableMapBase::~txVariableMapBase() |
66 | 0 | { |
67 | 0 | txExpandedNameMap<txAExprResult>::iterator iter(mMap); |
68 | 0 | while (iter.next()) { |
69 | 0 | txAExprResult* res = iter.value(); |
70 | 0 | NS_RELEASE(res); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | inline nsresult |
75 | | txVariableMapBase::bindVariable(const txExpandedName& aName, txAExprResult* aValue) |
76 | 0 | { |
77 | 0 | NS_ASSERTION(aValue, "can't add null-variables to a txVariableMap"); |
78 | 0 | nsresult rv = mMap.add(aName, aValue); |
79 | 0 | if (NS_SUCCEEDED(rv)) { |
80 | 0 | NS_ADDREF(aValue); |
81 | 0 | } |
82 | 0 | else if (rv == NS_ERROR_XSLT_ALREADY_SET) { |
83 | 0 | rv = NS_ERROR_XSLT_VAR_ALREADY_SET; |
84 | 0 | } |
85 | 0 | return rv; |
86 | 0 | } |
87 | | |
88 | | inline void |
89 | | txVariableMapBase::getVariable(const txExpandedName& aName, txAExprResult** aResult) |
90 | 0 | { |
91 | 0 | *aResult = mMap.get(aName); |
92 | 0 | if (*aResult) { |
93 | 0 | NS_ADDREF(*aResult); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | inline void |
98 | | txVariableMapBase::removeVariable(const txExpandedName& aName) |
99 | 0 | { |
100 | 0 | txAExprResult* var = mMap.remove(aName); |
101 | 0 | NS_IF_RELEASE(var); |
102 | 0 | } |
103 | | |
104 | | #endif //TRANSFRMX_VARIABLEMAP_H |