source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/util/KVStringPair.hpp @ 2674

Revision 2674, 6.7 KB checked in by mattausch, 16 years ago (diff)
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: KVStringPair.hpp 568078 2007-08-21 11:43:25Z amassari $
20 */
21
22#if !defined(KVSTRINGPAIR_HPP)
23#define KVSTRINGPAIR_HPP
24
25#include <xercesc/util/XMemory.hpp>
26#include <xercesc/util/PlatformUtils.hpp>
27
28#include <xercesc/internal/XSerializable.hpp>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32//
33//  This class provides a commonly used data structure, which is that of
34//  a pair of strings which represent a 'key=value' type mapping. It works
35//  only in terms of XMLCh type raw strings.
36//
37class XMLUTIL_EXPORT KVStringPair : public XSerializable, public XMemory
38{
39public:
40    // -----------------------------------------------------------------------
41    //  Constructors and Destructor
42    // -----------------------------------------------------------------------
43    KVStringPair(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
44    KVStringPair
45    (
46        const XMLCh* const key
47        , const XMLCh* const value
48        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
49    );
50    KVStringPair
51    (
52        const XMLCh* const key
53        , const XMLCh* const value
54        , const unsigned int valueLength
55        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
56    );
57    KVStringPair
58    (
59        const XMLCh* const key
60        , const unsigned int keyLength
61        , const XMLCh* const value
62        , const unsigned int valueLength
63        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
64    );
65    KVStringPair(const KVStringPair& toCopy);
66    ~KVStringPair();
67
68
69    // -----------------------------------------------------------------------
70    //  Getters
71    //
72    //  We support the
73    // -----------------------------------------------------------------------
74    const XMLCh* getKey() const;
75    XMLCh* getKey();
76    const XMLCh* getValue() const;
77    XMLCh* getValue();
78
79
80    // -----------------------------------------------------------------------
81    //  Setters
82    // -----------------------------------------------------------------------
83    void setKey(const XMLCh* const newKey);
84    void setValue(const XMLCh* const newValue);
85    void setKey
86    (
87        const   XMLCh* const newKey
88        , const unsigned int newKeyLength
89    );
90    void setValue
91    (
92        const   XMLCh* const newValue
93        , const unsigned int newValueLength
94    );
95    void set
96    (
97        const   XMLCh* const newKey
98        , const XMLCh* const newValue
99    );
100    void set
101    (
102        const     XMLCh* const newKey
103        , const   unsigned int newKeyLength
104        , const   XMLCh* const newValue
105        , const   unsigned int newValueLength
106    );
107
108    /***
109     * Support for Serialization/De-serialization
110     ***/
111    DECL_XSERIALIZABLE(KVStringPair)
112
113private :
114    // unimplemented:
115       
116    KVStringPair& operator=(const KVStringPair&);
117    // -----------------------------------------------------------------------
118    //  Private data members
119    //
120    //  fKey
121    //      The string that represents the key field of this object.
122    //
123    //  fKeyAllocSize
124    //      The amount of memory allocated for fKey.
125    //
126    //  fValue
127    //      The string that represents the value of this pair object.
128    //
129    //  fValueAllocSize
130    //      The amount of memory allocated for fValue.
131    //
132    // -----------------------------------------------------------------------
133    unsigned long  fKeyAllocSize;
134    unsigned long  fValueAllocSize;
135    XMLCh*         fKey;
136    XMLCh*         fValue;
137    MemoryManager* fMemoryManager;
138};
139
140// ---------------------------------------------------------------------------
141//  KVStringPair: Getters
142// ---------------------------------------------------------------------------
143inline const XMLCh* KVStringPair::getKey() const
144{
145    return fKey;
146}
147
148inline XMLCh* KVStringPair::getKey()
149{
150    return fKey;
151}
152
153inline const XMLCh* KVStringPair::getValue() const
154{
155    return fValue;
156}
157
158inline XMLCh* KVStringPair::getValue()
159{
160    return fValue;
161}
162
163// ---------------------------------------------------------------------------
164//  KVStringPair: Setters
165// ---------------------------------------------------------------------------
166inline void KVStringPair::setKey(const XMLCh* const newKey)
167{
168    setKey(newKey, XMLString::stringLen(newKey));
169}
170
171inline void KVStringPair::setValue(const XMLCh* const newValue)
172{
173    setValue(newValue, XMLString::stringLen(newValue));
174}
175
176inline void KVStringPair::setKey(  const XMLCh* const newKey
177                                 , const unsigned int newKeyLength)
178{
179    if (newKeyLength >= fKeyAllocSize)
180    {
181        fMemoryManager->deallocate(fKey); //delete [] fKey;
182        fKey = 0;
183        fKeyAllocSize = newKeyLength + 1;
184        fKey = (XMLCh*) fMemoryManager->allocate(fKeyAllocSize * sizeof(XMLCh)); //new XMLCh[fKeyAllocSize];
185    }
186
187    memcpy(fKey, newKey, (newKeyLength+1) * sizeof(XMLCh)); // len+1 because of the 0 at the end
188}
189
190inline void KVStringPair::setValue(  const XMLCh* const newValue
191                                   , const unsigned int newValueLength)
192{
193    if (newValueLength >= fValueAllocSize)
194    {
195        fMemoryManager->deallocate(fValue); //delete [] fValue;
196        fValue = 0;
197        fValueAllocSize = newValueLength + 1;
198        fValue = (XMLCh*) fMemoryManager->allocate(fValueAllocSize * sizeof(XMLCh)); //new XMLCh[fValueAllocSize];
199    }
200
201    memcpy(fValue, newValue, (newValueLength+1) * sizeof(XMLCh)); // len+1 because of the 0 at the end
202}
203
204inline void KVStringPair::set(  const   XMLCh* const    newKey
205                              , const   XMLCh* const    newValue)
206{
207    setKey(newKey, XMLString::stringLen(newKey));
208    setValue(newValue, XMLString::stringLen(newValue));
209}
210
211inline void KVStringPair::set(  const   XMLCh* const newKey
212                              , const   unsigned int newKeyLength
213                              , const   XMLCh* const newValue
214                              , const   unsigned int newValueLength)
215{
216    setKey(newKey, newKeyLength);
217    setValue(newValue, newValueLength);
218}
219
220
221XERCES_CPP_NAMESPACE_END
222
223#endif
Note: See TracBrowser for help on using the repository browser.