~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/json_server/json/json_valueiterator.inl

  • Committer: Mark Atwood
  • Date: 2011-05-12 22:20:08 UTC
  • mfrom: (2283.4.17 json-interface)
  • Revision ID: me@mark.atwood.name-20110512222008-7facdn0qu7lsbm0g
mergeĀ lp:~stewart/drizzle/json-interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
2
 * 
 
3
 *  JSON Library, originally from http://jsoncpp.sourceforge.net/
 
4
 *
 
5
 *  Copyright (C) 2011 Stewart Smith
 
6
 *  All rights reserved.
 
7
 *
 
8
 *  Redistribution and use in source and binary forms, with or without
 
9
 *  modification, are permitted provided that the following conditions are
 
10
 *  met:
 
11
 *
 
12
 *      * Redistributions of source code must retain the above copyright
 
13
 *  notice, this list of conditions and the following disclaimer.
 
14
 *
 
15
 *      * Redistributions in binary form must reproduce the above
 
16
 *  copyright notice, this list of conditions and the following disclaimer
 
17
 *  in the documentation and/or other materials provided with the
 
18
 *  distribution.
 
19
 *
 
20
 *      * The names of its contributors may not be used to endorse or
 
21
 *  promote products derived from this software without specific prior
 
22
 *  written permission.
 
23
 *
 
24
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
25
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
26
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
27
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
28
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
29
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
30
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
31
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
32
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
33
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
34
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
35
 *
 
36
 */
 
37
 
 
38
#pragma once
 
39
// included by json_value.cpp
 
40
// everything is within Json namespace
 
41
 
 
42
 
 
43
// //////////////////////////////////////////////////////////////////
 
44
// //////////////////////////////////////////////////////////////////
 
45
// //////////////////////////////////////////////////////////////////
 
46
// class ValueIteratorBase
 
47
// //////////////////////////////////////////////////////////////////
 
48
// //////////////////////////////////////////////////////////////////
 
49
// //////////////////////////////////////////////////////////////////
 
50
 
 
51
ValueIteratorBase::ValueIteratorBase()
 
52
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
53
   : current_()
 
54
   , isNull_( true )
 
55
{
 
56
}
 
57
#else
 
58
   : isArray_( true )
 
59
   , isNull_( true )
 
60
{
 
61
   iterator_.array_ = ValueInternalArray::IteratorState();
 
62
}
 
63
#endif
 
64
 
 
65
 
 
66
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
67
ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator &current )
 
68
   : current_( current )
 
69
   , isNull_( false )
 
70
{
 
71
}
 
72
#else
 
73
ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state )
 
74
   : isArray_( true )
 
75
{
 
76
   iterator_.array_ = state;
 
77
}
 
78
 
 
79
 
 
80
ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state )
 
81
   : isArray_( false )
 
82
{
 
83
   iterator_.map_ = state;
 
84
}
 
85
#endif
 
86
 
 
87
Value &
 
88
ValueIteratorBase::deref() const
 
89
{
 
90
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
91
   return current_->second;
 
92
#else
 
93
   if ( isArray_ )
 
94
      return ValueInternalArray::dereference( iterator_.array_ );
 
95
   return ValueInternalMap::value( iterator_.map_ );
 
96
#endif
 
97
}
 
98
 
 
99
 
 
100
void 
 
101
ValueIteratorBase::increment()
 
102
{
 
103
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
104
   ++current_;
 
105
#else
 
106
   if ( isArray_ )
 
107
      ValueInternalArray::increment( iterator_.array_ );
 
108
   ValueInternalMap::increment( iterator_.map_ );
 
109
#endif
 
110
}
 
111
 
 
112
 
 
113
void 
 
114
ValueIteratorBase::decrement()
 
115
{
 
116
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
117
   --current_;
 
118
#else
 
119
   if ( isArray_ )
 
120
      ValueInternalArray::decrement( iterator_.array_ );
 
121
   ValueInternalMap::decrement( iterator_.map_ );
 
122
#endif
 
123
}
 
124
 
 
125
 
 
126
ValueIteratorBase::difference_type 
 
127
ValueIteratorBase::computeDistance( const SelfType &other ) const
 
128
{
 
129
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
130
# ifdef JSON_USE_CPPTL_SMALLMAP
 
131
   return current_ - other.current_;
 
132
# else
 
133
   // Iterator for null value are initialized using the default
 
134
   // constructor, which initialize current_ to the default
 
135
   // std::map::iterator. As begin() and end() are two instance 
 
136
   // of the default std::map::iterator, they can not be compared.
 
137
   // To allow this, we handle this comparison specifically.
 
138
   if ( isNull_  &&  other.isNull_ )
 
139
   {
 
140
      return 0;
 
141
   }
 
142
 
 
143
 
 
144
   // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL,
 
145
   // which is the one used by default).
 
146
   // Using a portable hand-made version for non random iterator instead:
 
147
   //   return difference_type( std::distance( current_, other.current_ ) );
 
148
   difference_type myDistance = 0;
 
149
   for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it )
 
150
   {
 
151
      ++myDistance;
 
152
   }
 
153
   return myDistance;
 
154
# endif
 
155
#else
 
156
   if ( isArray_ )
 
157
      return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ );
 
158
   return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ );
 
159
#endif
 
160
}
 
161
 
 
162
 
 
163
bool 
 
164
ValueIteratorBase::isEqual( const SelfType &other ) const
 
165
{
 
166
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
167
   if ( isNull_ )
 
168
   {
 
169
      return other.isNull_;
 
170
   }
 
171
   return current_ == other.current_;
 
172
#else
 
173
   if ( isArray_ )
 
174
      return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ );
 
175
   return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ );
 
176
#endif
 
177
}
 
178
 
 
179
 
 
180
void 
 
181
ValueIteratorBase::copy( const SelfType &other )
 
182
{
 
183
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
184
   current_ = other.current_;
 
185
#else
 
186
   if ( isArray_ )
 
187
      iterator_.array_ = other.iterator_.array_;
 
188
   iterator_.map_ = other.iterator_.map_;
 
189
#endif
 
190
}
 
191
 
 
192
 
 
193
Value 
 
194
ValueIteratorBase::key() const
 
195
{
 
196
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
197
   const Value::CZString czstring = (*current_).first;
 
198
   if ( czstring.c_str() )
 
199
   {
 
200
      if ( czstring.isStaticString() )
 
201
         return Value( StaticString( czstring.c_str() ) );
 
202
      return Value( czstring.c_str() );
 
203
   }
 
204
   return Value( czstring.index() );
 
205
#else
 
206
   if ( isArray_ )
 
207
      return Value( ValueInternalArray::indexOf( iterator_.array_ ) );
 
208
   bool isStatic;
 
209
   const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic );
 
210
   if ( isStatic )
 
211
      return Value( StaticString( memberName ) );
 
212
   return Value( memberName );
 
213
#endif
 
214
}
 
215
 
 
216
 
 
217
UInt 
 
218
ValueIteratorBase::index() const
 
219
{
 
220
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
221
   const Value::CZString czstring = (*current_).first;
 
222
   if ( !czstring.c_str() )
 
223
      return czstring.index();
 
224
   return Value::UInt( -1 );
 
225
#else
 
226
   if ( isArray_ )
 
227
      return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) );
 
228
   return Value::UInt( -1 );
 
229
#endif
 
230
}
 
231
 
 
232
 
 
233
const char *
 
234
ValueIteratorBase::memberName() const
 
235
{
 
236
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
237
   const char *name = (*current_).first.c_str();
 
238
   return name ? name : "";
 
239
#else
 
240
   if ( !isArray_ )
 
241
      return ValueInternalMap::key( iterator_.map_ );
 
242
   return "";
 
243
#endif
 
244
}
 
245
 
 
246
 
 
247
// //////////////////////////////////////////////////////////////////
 
248
// //////////////////////////////////////////////////////////////////
 
249
// //////////////////////////////////////////////////////////////////
 
250
// class ValueConstIterator
 
251
// //////////////////////////////////////////////////////////////////
 
252
// //////////////////////////////////////////////////////////////////
 
253
// //////////////////////////////////////////////////////////////////
 
254
 
 
255
ValueConstIterator::ValueConstIterator()
 
256
{
 
257
}
 
258
 
 
259
 
 
260
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
261
ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator &current )
 
262
   : ValueIteratorBase( current )
 
263
{
 
264
}
 
265
#else
 
266
ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state )
 
267
   : ValueIteratorBase( state )
 
268
{
 
269
}
 
270
 
 
271
ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state )
 
272
   : ValueIteratorBase( state )
 
273
{
 
274
}
 
275
#endif
 
276
 
 
277
ValueConstIterator &
 
278
ValueConstIterator::operator =( const ValueIteratorBase &other )
 
279
{
 
280
   copy( other );
 
281
   return *this;
 
282
}
 
283
 
 
284
 
 
285
// //////////////////////////////////////////////////////////////////
 
286
// //////////////////////////////////////////////////////////////////
 
287
// //////////////////////////////////////////////////////////////////
 
288
// class ValueIterator
 
289
// //////////////////////////////////////////////////////////////////
 
290
// //////////////////////////////////////////////////////////////////
 
291
// //////////////////////////////////////////////////////////////////
 
292
 
 
293
ValueIterator::ValueIterator()
 
294
{
 
295
}
 
296
 
 
297
 
 
298
#ifndef JSON_VALUE_USE_INTERNAL_MAP
 
299
ValueIterator::ValueIterator( const Value::ObjectValues::iterator &current )
 
300
   : ValueIteratorBase( current )
 
301
{
 
302
}
 
303
#else
 
304
ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state )
 
305
   : ValueIteratorBase( state )
 
306
{
 
307
}
 
308
 
 
309
ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state )
 
310
   : ValueIteratorBase( state )
 
311
{
 
312
}
 
313
#endif
 
314
 
 
315
ValueIterator::ValueIterator( const ValueConstIterator &other )
 
316
   : ValueIteratorBase( other )
 
317
{
 
318
}
 
319
 
 
320
ValueIterator::ValueIterator( const ValueIterator &other )
 
321
   : ValueIteratorBase( other )
 
322
{
 
323
}
 
324
 
 
325
ValueIterator &
 
326
ValueIterator::operator =( const SelfType &other )
 
327
{
 
328
   copy( other );
 
329
   return *this;
 
330
}