Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / xslt / xslt / txVariableMap.h
blob20c140552460aec0f2e82b2d93276b28edf78fcf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
6 #ifndef TRANSFRMX_VARIABLEMAP_H
7 #define TRANSFRMX_VARIABLEMAP_H
9 #include "nsError.h"
10 #include "txXMLUtils.h"
11 #include "txExprResult.h"
12 #include "txExpandedNameMap.h"
14 /**
15 * Map that maps from expanded name to an expression result value. This is just
16 * a base class, use txVariableMap or txParameterMap instead.
18 class txVariableMapBase {
19 public:
20 nsresult bindVariable(const txExpandedName& aName, txAExprResult* aValue);
22 void getVariable(const txExpandedName& aName, txAExprResult** aResult);
24 void removeVariable(const txExpandedName& aName);
26 protected:
27 txVariableMapBase() = default;
28 ~txVariableMapBase();
30 txExpandedNameMap<txAExprResult> mMap;
33 /**
34 * Map for mapping from expanded name to variable values. This is not
35 * refcounted, so owners need to be careful to clean this up.
37 class txVariableMap : public txVariableMapBase {
38 public:
39 txVariableMap() { MOZ_COUNT_CTOR(txVariableMap); }
40 MOZ_COUNTED_DTOR(txVariableMap)
43 /**
44 * Map for mapping from expanded name to parameter values. This is refcounted,
45 * so multiple owners can hold a reference.
47 class txParameterMap : public txVariableMapBase {
48 public:
49 NS_INLINE_DECL_REFCOUNTING(txParameterMap)
51 private:
52 ~txParameterMap() = default;
55 inline txVariableMapBase::~txVariableMapBase() {
56 txExpandedNameMap<txAExprResult>::iterator iter(mMap);
57 while (iter.next()) {
58 txAExprResult* res = iter.value();
59 NS_RELEASE(res);
63 inline nsresult txVariableMapBase::bindVariable(const txExpandedName& aName,
64 txAExprResult* aValue) {
65 NS_ASSERTION(aValue, "can't add null-variables to a txVariableMap");
66 nsresult rv = mMap.add(aName, aValue);
67 if (NS_SUCCEEDED(rv)) {
68 NS_ADDREF(aValue);
69 } else if (rv == NS_ERROR_XSLT_ALREADY_SET) {
70 rv = NS_ERROR_XSLT_VAR_ALREADY_SET;
72 return rv;
75 inline void txVariableMapBase::getVariable(const txExpandedName& aName,
76 txAExprResult** aResult) {
77 *aResult = mMap.get(aName);
78 if (*aResult) {
79 NS_ADDREF(*aResult);
83 inline void txVariableMapBase::removeVariable(const txExpandedName& aName) {
84 txAExprResult* var = mMap.remove(aName);
85 NS_IF_RELEASE(var);
88 #endif // TRANSFRMX_VARIABLEMAP_H