Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:12

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  * 
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  * 
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_KVSTRINGPAIR_HPP)
0023 #define XERCESC_INCLUDE_GUARD_KVSTRINGPAIR_HPP
0024 
0025 #include <xercesc/util/XMemory.hpp>
0026 #include <xercesc/util/PlatformUtils.hpp>
0027 
0028 #include <xercesc/internal/XSerializable.hpp>
0029 
0030 XERCES_CPP_NAMESPACE_BEGIN
0031 
0032 //
0033 //  This class provides a commonly used data structure, which is that of
0034 //  a pair of strings which represent a 'key=value' type mapping. It works
0035 //  only in terms of XMLCh type raw strings.
0036 //
0037 class XMLUTIL_EXPORT KVStringPair : public XSerializable, public XMemory
0038 {
0039 public:
0040     // -----------------------------------------------------------------------
0041     //  Constructors and Destructor
0042     // -----------------------------------------------------------------------
0043     KVStringPair(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
0044     KVStringPair
0045     (
0046         const XMLCh* const key
0047         , const XMLCh* const value
0048         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
0049     );
0050     KVStringPair
0051     (
0052         const XMLCh* const key
0053         , const XMLCh* const value
0054         , const XMLSize_t    valueLength
0055         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
0056     );
0057     KVStringPair
0058     (
0059         const XMLCh* const key
0060         , const XMLSize_t    keyLength
0061         , const XMLCh* const value
0062         , const XMLSize_t    valueLength
0063         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
0064     );
0065     KVStringPair(const KVStringPair& toCopy);
0066     ~KVStringPair();
0067 
0068 
0069     // -----------------------------------------------------------------------
0070     //  Getters
0071     //
0072     //  We support the
0073     // -----------------------------------------------------------------------
0074     const XMLCh* getKey() const;
0075     XMLCh* getKey();
0076     const XMLCh* getValue() const;
0077     XMLCh* getValue();
0078 
0079 
0080     // -----------------------------------------------------------------------
0081     //  Setters
0082     // -----------------------------------------------------------------------
0083     void setKey(const XMLCh* const newKey);
0084     void setValue(const XMLCh* const newValue);
0085     void setKey
0086     (
0087         const   XMLCh* const newKey
0088         , const XMLSize_t    newKeyLength
0089     );
0090     void setValue
0091     (
0092         const   XMLCh* const newValue
0093         , const XMLSize_t    newValueLength
0094     );
0095     void set
0096     (
0097         const   XMLCh* const newKey
0098         , const XMLCh* const newValue
0099     );
0100     void set
0101     (
0102         const     XMLCh* const newKey
0103         , const   XMLSize_t    newKeyLength
0104         , const   XMLCh* const newValue
0105         , const   XMLSize_t    newValueLength
0106     );
0107 
0108     /***
0109      * Support for Serialization/De-serialization
0110      ***/
0111     DECL_XSERIALIZABLE(KVStringPair)
0112 
0113 private :
0114     // unimplemented:
0115        
0116     KVStringPair& operator=(const KVStringPair&);
0117     // -----------------------------------------------------------------------
0118     //  Private data members
0119     //
0120     //  fKey
0121     //      The string that represents the key field of this object.
0122     //
0123     //  fKeyAllocSize
0124     //      The amount of memory allocated for fKey.
0125     //
0126     //  fValue
0127     //      The string that represents the value of this pair object.
0128     //
0129     //  fValueAllocSize
0130     //      The amount of memory allocated for fValue.
0131     //
0132     // -----------------------------------------------------------------------
0133     XMLSize_t      fKeyAllocSize;
0134     XMLSize_t      fValueAllocSize;
0135     XMLCh*         fKey;
0136     XMLCh*         fValue;
0137     MemoryManager* fMemoryManager;
0138 };
0139 
0140 // ---------------------------------------------------------------------------
0141 //  KVStringPair: Getters
0142 // ---------------------------------------------------------------------------
0143 inline const XMLCh* KVStringPair::getKey() const
0144 {
0145     return fKey;
0146 }
0147 
0148 inline XMLCh* KVStringPair::getKey()
0149 {
0150     return fKey;
0151 }
0152 
0153 inline const XMLCh* KVStringPair::getValue() const
0154 {
0155     return fValue;
0156 }
0157 
0158 inline XMLCh* KVStringPair::getValue()
0159 {
0160     return fValue;
0161 }
0162 
0163 // ---------------------------------------------------------------------------
0164 //  KVStringPair: Setters
0165 // ---------------------------------------------------------------------------
0166 inline void KVStringPair::setKey(  const XMLCh* const newKey
0167                                  , const XMLSize_t    newKeyLength)
0168 {
0169     if (newKeyLength >= fKeyAllocSize)
0170     {
0171         fMemoryManager->deallocate(fKey); //delete [] fKey;
0172         fKey = 0;
0173         fKeyAllocSize = newKeyLength + 1;
0174         fKey = (XMLCh*) fMemoryManager->allocate(fKeyAllocSize * sizeof(XMLCh)); //new XMLCh[fKeyAllocSize];
0175     }
0176 
0177     memcpy(fKey, newKey, (newKeyLength+1) * sizeof(XMLCh)); // len+1 because of the 0 at the end
0178 }
0179 
0180 inline void KVStringPair::setValue(  const XMLCh* const newValue
0181                                    , const XMLSize_t    newValueLength)
0182 {
0183     if (newValueLength >= fValueAllocSize)
0184     {
0185         fMemoryManager->deallocate(fValue); //delete [] fValue;
0186         fValue = 0;
0187         fValueAllocSize = newValueLength + 1;
0188         fValue = (XMLCh*) fMemoryManager->allocate(fValueAllocSize * sizeof(XMLCh)); //new XMLCh[fValueAllocSize];
0189     }
0190 
0191     memcpy(fValue, newValue, (newValueLength+1) * sizeof(XMLCh)); // len+1 because of the 0 at the end
0192 }
0193 
0194 inline void KVStringPair::setKey(const XMLCh* const newKey)
0195 {
0196     setKey(newKey, XMLString::stringLen(newKey));
0197 }
0198 
0199 inline void KVStringPair::setValue(const XMLCh* const newValue)
0200 {
0201     setValue(newValue, XMLString::stringLen(newValue));
0202 }
0203 
0204 inline void KVStringPair::set(  const   XMLCh* const    newKey
0205                               , const   XMLCh* const    newValue)
0206 {
0207     setKey(newKey, XMLString::stringLen(newKey));
0208     setValue(newValue, XMLString::stringLen(newValue));
0209 }
0210 
0211 inline void KVStringPair::set(  const   XMLCh* const newKey
0212                               , const   XMLSize_t    newKeyLength
0213                               , const   XMLCh* const newValue
0214                               , const   XMLSize_t    newValueLength)
0215 {
0216     setKey(newKey, newKeyLength);
0217     setValue(newValue, newValueLength);
0218 }
0219 
0220 
0221 XERCES_CPP_NAMESPACE_END
0222 
0223 #endif