Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/commandhandler/nsCommandParams.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef nsCommandParams_h
8
#define nsCommandParams_h
9
10
#include "mozilla/ErrorResult.h"
11
#include "nsString.h"
12
#include "nsICommandParams.h"
13
#include "nsCOMPtr.h"
14
#include "PLDHashTable.h"
15
16
class nsCommandParams : public nsICommandParams
17
{
18
  typedef mozilla::ErrorResult ErrorResult;
19
  typedef mozilla::IgnoredErrorResult IgnoredErrorResult;
20
21
public:
22
  nsCommandParams();
23
24
  NS_DECL_ISUPPORTS
25
  NS_DECL_NSICOMMANDPARAMS
26
27
  bool GetBool(const char* aName, ErrorResult& aRv) const;
28
  inline bool GetBool(const char* aName) const
29
  {
30
    IgnoredErrorResult error;
31
    return GetBool(aName, error);
32
  }
33
  int32_t GetInt(const char* aName, ErrorResult& aRv) const;
34
  inline int32_t GetInt(const char* aName) const
35
  {
36
    IgnoredErrorResult error;
37
    return GetInt(aName, error);
38
  }
39
  double GetDouble(const char* aName, ErrorResult& aRv) const;
40
  inline double GetDouble(const char* aName) const
41
  {
42
    IgnoredErrorResult error;
43
    return GetDouble(aName, error);
44
  }
45
  nsresult GetString(const char* aName, nsAString& aValue) const;
46
  nsresult GetCString(const char* aName, nsACString& aValue) const;
47
  already_AddRefed<nsISupports>
48
  GetISupports(const char* aName, ErrorResult& aRv) const;
49
  inline already_AddRefed<nsISupports> GetISupports(const char* aName) const
50
  {
51
    IgnoredErrorResult error;
52
    return GetISupports(aName, error);
53
  }
54
55
  nsresult SetBool(const char* aName, bool aValue);
56
  nsresult SetInt(const char* aName, int32_t aValue);
57
  nsresult SetDouble(const char* aName, double aValue);
58
  nsresult SetString(const char* aName, const nsAString& aValue);
59
  nsresult SetCString(const char* aName, const nsACString& aValue);
60
  nsresult SetISupports(const char* aName, nsISupports* aValue);
61
62
protected:
63
  virtual ~nsCommandParams();
64
65
  struct HashEntry : public PLDHashEntryHdr
66
  {
67
    nsCString mEntryName;
68
69
    uint8_t mEntryType;
70
    union
71
    {
72
      bool mBoolean;
73
      int32_t mLong;
74
      double mDouble;
75
      nsString* mString;
76
      nsCString* mCString;
77
    } mData;
78
79
    nsCOMPtr<nsISupports> mISupports;
80
81
    HashEntry(uint8_t aType, const char* aEntryName)
82
      : mEntryName(aEntryName)
83
      , mEntryType(aType)
84
      , mData()
85
0
    {
86
0
      Reset(mEntryType);
87
0
    }
88
89
    explicit HashEntry(const HashEntry& aRHS)
90
      : mEntryType(aRHS.mEntryType)
91
0
    {
92
0
      Reset(mEntryType);
93
0
      switch (mEntryType) {
94
0
        case eBooleanType:
95
0
          mData.mBoolean = aRHS.mData.mBoolean;
96
0
          break;
97
0
        case eLongType:
98
0
          mData.mLong = aRHS.mData.mLong;
99
0
          break;
100
0
        case eDoubleType:
101
0
          mData.mDouble = aRHS.mData.mDouble;
102
0
          break;
103
0
        case eWStringType:
104
0
          NS_ASSERTION(aRHS.mData.mString, "Source entry has no string");
105
0
          mData.mString = new nsString(*aRHS.mData.mString);
106
0
          break;
107
0
        case eStringType:
108
0
          NS_ASSERTION(aRHS.mData.mCString, "Source entry has no string");
109
0
          mData.mCString = new nsCString(*aRHS.mData.mCString);
110
0
          break;
111
0
        case eISupportsType:
112
0
          mISupports = aRHS.mISupports.get();
113
0
          break;
114
0
        default:
115
0
          NS_ERROR("Unknown type");
116
0
      }
117
0
    }
118
119
0
    ~HashEntry() { Reset(eNoType); }
120
121
    void Reset(uint8_t aNewType)
122
0
    {
123
0
      switch (mEntryType) {
124
0
        case eNoType:
125
0
          break;
126
0
        case eBooleanType:
127
0
          mData.mBoolean = false;
128
0
          break;
129
0
        case eLongType:
130
0
          mData.mLong = 0;
131
0
          break;
132
0
        case eDoubleType:
133
0
          mData.mDouble = 0.0;
134
0
          break;
135
0
        case eWStringType:
136
0
          delete mData.mString;
137
0
          mData.mString = nullptr;
138
0
          break;
139
0
        case eISupportsType:
140
0
          mISupports = nullptr;
141
0
          break;
142
0
        case eStringType:
143
0
          delete mData.mCString;
144
0
          mData.mCString = nullptr;
145
0
          break;
146
0
        default:
147
0
          NS_ERROR("Unknown type");
148
0
      }
149
0
      mEntryType = aNewType;
150
0
    }
151
  };
152
153
  HashEntry* GetNamedEntry(const char* aName) const;
154
  HashEntry* GetOrMakeEntry(const char* aName, uint8_t aEntryType);
155
156
protected:
157
  static PLDHashNumber HashKey(const void* aKey);
158
159
  static bool HashMatchEntry(const PLDHashEntryHdr* aEntry, const void* aKey);
160
161
  static void HashMoveEntry(PLDHashTable* aTable, const PLDHashEntryHdr* aFrom,
162
                            PLDHashEntryHdr* aTo);
163
164
  static void HashClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry);
165
166
  PLDHashTable mValuesHash;
167
168
  static const PLDHashTableOps sHashOps;
169
};
170
171
nsCommandParams*
172
nsICommandParams::AsCommandParams()
173
{
174
  return static_cast<nsCommandParams*>(this);
175
}
176
177
const nsCommandParams*
178
nsICommandParams::AsCommandParams() const
179
{
180
  return static_cast<const nsCommandParams*>(this);
181
}
182
183
#endif // nsCommandParams_h