embedding/components/commandhandler/src/nsCommandParams.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/embedding/components/commandhandler/src/nsCommandParams.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#ifndef nsCommandParams_h__
    1.11 +#define nsCommandParams_h__
    1.12 +
    1.13 +#include "nsString.h"
    1.14 +#include "nsICommandParams.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "pldhash.h"
    1.17 +
    1.18 +
    1.19 +
    1.20 +class nsCommandParams : public nsICommandParams
    1.21 +{
    1.22 +public:
    1.23 +
    1.24 +                      nsCommandParams();
    1.25 +  virtual             ~nsCommandParams();
    1.26 +  
    1.27 +  
    1.28 +  NS_DECL_ISUPPORTS
    1.29 +  NS_DECL_NSICOMMANDPARAMS
    1.30 +
    1.31 +  nsresult            Init();
    1.32 +  
    1.33 +protected:
    1.34 +
    1.35 +  struct HashEntry : public PLDHashEntryHdr
    1.36 +  {
    1.37 +    nsCString      mEntryName;
    1.38 +
    1.39 +    uint8_t       mEntryType;
    1.40 +    union {
    1.41 +    
    1.42 +      bool                    mBoolean;
    1.43 +      int32_t                 mLong;
    1.44 +      double                  mDouble;
    1.45 +      nsString*               mString;
    1.46 +      nsCString*              mCString;
    1.47 +    } mData;
    1.48 +
    1.49 +    nsCOMPtr<nsISupports>   mISupports;    
    1.50 +    
    1.51 +    HashEntry(uint8_t inType, const char * inEntryName)
    1.52 +    : mEntryName(inEntryName)
    1.53 +    , mEntryType(inType)
    1.54 +    {
    1.55 +      memset(&mData, 0, sizeof(mData));
    1.56 +      Reset(mEntryType);
    1.57 +    }
    1.58 +
    1.59 +    HashEntry(const HashEntry& inRHS)
    1.60 +    : mEntryType(inRHS.mEntryType)
    1.61 +    {
    1.62 +      Reset(mEntryType);
    1.63 +      switch (mEntryType)
    1.64 +      {
    1.65 +        case eBooleanType:      mData.mBoolean = inRHS.mData.mBoolean;  break;
    1.66 +        case eLongType:         mData.mLong    = inRHS.mData.mLong;     break;
    1.67 +        case eDoubleType:       mData.mDouble  = inRHS.mData.mDouble;   break;
    1.68 +        case eWStringType:
    1.69 +          NS_ASSERTION(inRHS.mData.mString, "Source entry has no string");
    1.70 +          mData.mString = new nsString(*inRHS.mData.mString);
    1.71 +          break;      
    1.72 +        case eStringType:
    1.73 +          NS_ASSERTION(inRHS.mData.mCString, "Source entry has no string");
    1.74 +          mData.mCString = new nsCString(*inRHS.mData.mCString);
    1.75 +          break;      
    1.76 +        case eISupportsType:
    1.77 +          mISupports = inRHS.mISupports.get();    // additional addref
    1.78 +          break;
    1.79 +        default:
    1.80 +          NS_ERROR("Unknown type");
    1.81 +      }
    1.82 +    }
    1.83 +    
    1.84 +    ~HashEntry()
    1.85 +    {
    1.86 +      if (mEntryType == eWStringType)
    1.87 +        delete mData.mString;
    1.88 +      else if (mEntryType == eStringType)
    1.89 +        delete mData.mCString;
    1.90 +    }
    1.91 +    
    1.92 +    void Reset(uint8_t inNewType)
    1.93 +    {
    1.94 +      switch (mEntryType)
    1.95 +      {
    1.96 +        case eNoType:                                       break;
    1.97 +        case eBooleanType:      mData.mBoolean = false;  break;
    1.98 +        case eLongType:         mData.mLong = 0;            break;
    1.99 +        case eDoubleType:       mData.mDouble = 0.0;        break;
   1.100 +        case eWStringType:      delete mData.mString; mData.mString = nullptr;     break;
   1.101 +        case eISupportsType:    mISupports = nullptr;        break;    // clear the nsCOMPtr
   1.102 +        case eStringType:       delete mData.mCString; mData.mCString = nullptr;   break;
   1.103 +        default:
   1.104 +          NS_ERROR("Unknown type");
   1.105 +      }
   1.106 +      
   1.107 +      mEntryType = inNewType;
   1.108 +    }
   1.109 +    
   1.110 +  };
   1.111 +
   1.112 +
   1.113 +  HashEntry*          GetNamedEntry(const char * name);
   1.114 +  HashEntry*          GetIndexedEntry(int32_t index);
   1.115 +  uint32_t            GetNumEntries();
   1.116 +  
   1.117 +  nsresult            GetOrMakeEntry(const char * name, uint8_t entryType, HashEntry*& outEntry);
   1.118 +  
   1.119 +protected:
   1.120 +
   1.121 +  static PLDHashNumber HashKey(PLDHashTable *table, const void *key);
   1.122 +
   1.123 +  static bool          HashMatchEntry(PLDHashTable *table,
   1.124 +                                      const PLDHashEntryHdr *entry, const void *key);
   1.125 +  
   1.126 +  static void          HashMoveEntry(PLDHashTable *table, const PLDHashEntryHdr *from,
   1.127 +                                     PLDHashEntryHdr *to);
   1.128 +  
   1.129 +  static void          HashClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
   1.130 +  
   1.131 +                                  
   1.132 +protected:
   1.133 +  
   1.134 +  enum {
   1.135 +    eNumEntriesUnknown      = -1
   1.136 +  };
   1.137 +
   1.138 +  // this is going to have to use a pldhash, because we need to
   1.139 +  // be able to iterate through entries, passing names back
   1.140 +  // to the caller, which means that we need to store the names
   1.141 +  // internally.
   1.142 +  
   1.143 +  PLDHashTable    mValuesHash;
   1.144 +  
   1.145 +  // enumerator data
   1.146 +  int32_t         mCurEntry;
   1.147 +  int32_t         mNumEntries;      // number of entries at start of enumeration (-1 indicates not known)
   1.148 +    
   1.149 +  static const PLDHashTableOps    sHashOps;
   1.150 +};
   1.151 +
   1.152 +
   1.153 +#endif // nsCommandParams_h__

mercurial