1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009 Oracle. All rights reserved.
*
* $Id$
*/
#ifndef _DB_STL_KDPAIR_H
#define _DB_STL_KDPAIR_H
#include <iostream>
#include "dbstl_common.h"
#include "dbstl_dbt.h"
#include "dbstl_exception.h"
#include "dbstl_base_iterator.h"
#include "dbstl_utility.h"
START_NS(dbstl)
using std::istream;
using std::ostream;
using std::basic_ostream;
using std::basic_istream;
template <Typename ddt>
class db_base_iterator;
template <Typename ddt>
class ElementHolder;
/** \ingroup dbstl_helper_classes
\defgroup Element_wrappers ElementRef and ElementHolder wrappers.
An ElementRef and ElementHolder object represents the reference to the
data element referenced by an iterator. Each iterator
object has an ElementRef or ElementHolder object that
stores the data element that the iterator points to.
The ElementHolder class is used to store primitive types into STL containers.
The ElementRef class is used to store other types into STL containers.
The ElementRef and ElementHolder classes have identical interfaces, and are
treated the same by other STL classes. Since the ElementRef class inherits
from the template data class, all methods have a _DB_STL_ prefix to avoid name
clashes.
An ElementRef or ElementHolder class corresponds to a single iterator instance.
An Element object is generally owned by an iterator object. The ownership
relationship is swapped in some specific situations, specifically for the
dereference and array index operator.
@{
*/
/// ElementRef element wrapper for classes and structures.
/// \sa ElementHolder
template <Typename ddt>
class _exported ElementRef : public ddt
{
public:
typedef ElementRef<ddt> self;
typedef ddt base;
typedef db_base_iterator<ddt> iterator_type;
typedef ddt content_type; // Used by assoc classes.
private:
// The iterator pointing the data element stored in this object.
iterator_type *_DB_STL_itr_;
// Whether or not to delete itr on destruction, by default it is
// false because this object is supposed to live in the lifetime of
// its _DB_STL_itr_ owner. But there is one exception: in
// db_vector<>::operator[]/front/back and db_map<>::operator[]
// functions, an ElementRef<T> object has to be
// returned instead of its reference, thus the
// returned ElementRef<> has to live longer than its _DB_STL_itr_,
// thus we new an iterator, and call _DB_STL_SetDelItr() method,
// setting this member to true,
// to tell this object that it should delete the
// _DB_STL_itr_ iterator on destruction, and duplicate the _DB_STL_itr_
// iterator on copy construction. Although
// std::vector<> returns reference rather than value, this is not a
// problem because the returned ElementRef<> will duplicate cursor and
// still points to the same key/data pair.
//
mutable bool _DB_STL_delete_itr_;
public:
////////////////////////////////////////////////////////////////////
//
// Begin constructors and destructor.
//
/// \name Constructors and destructor.
//@{
/// Destructor.
~ElementRef() {
if (_DB_STL_delete_itr_) {
// Prevent recursive destruction.
_DB_STL_delete_itr_ = false;
_DB_STL_itr_->delete_me();
}
}
/// Constructor.
/// If the pitr parameter is NULL or the default value is used, the
/// object created is a simple wrapper and not connected to a container.
/// If a valid iterator parameter is passed in, the wrapped element will
/// be associated with the matching key/data pair in the underlying
/// container.
/// \param pitr The iterator owning this object.
explicit ElementRef(iterator_type *pitr = NULL)
{
_DB_STL_delete_itr_ = false;
_DB_STL_itr_ = pitr;
}
/// Constructor.
/// Initializes an ElementRef wrapper without an iterator. It can only
/// be used to wrap a data element in memory, it can't access an
/// unerlying database.
/// \param dt The base class object to initialize this object.
ElementRef(const ddt &dt) : ddt(dt)
{
_DB_STL_delete_itr_ = false;
_DB_STL_itr_ = NULL;
}
/// Copy constructor.
/// The constructor takes a "deep" copy. The created object will be
/// identical to, but independent from the original object.
/// \param other The object to clone from.
ElementRef(const self &other) : ddt(other)
{
// Duplicate iterator if this object lives longer than
// _DB_STL_itr_.
_DB_STL_delete_itr_ = other._DB_STL_delete_itr_;
if (_DB_STL_delete_itr_) {
// Avoid recursive duplicate iterator calls.
other._DB_STL_delete_itr_ = false;
_DB_STL_itr_ = other._DB_STL_itr_->dup_itr();
other._DB_STL_delete_itr_ = true;
} else
_DB_STL_itr_ = other._DB_STL_itr_;
}
//@}
////////////////////////////////////////////////////////////////////
/// \name Assignment operators.
/// The assignment operators are used to store right-values into the
/// wrapped object, and also to store values into an underlying
/// container.
//@{
/// Assignment Operator.
/// \param dt2 The data value to assign with.
/// \return The object dt2's reference.
inline const ddt& operator=(const ddt& dt2)
{
*((ddt*)this) = dt2;
if (_DB_STL_itr_ != NULL) {
if (!_DB_STL_itr_->is_set_iterator())
_DB_STL_itr_->replace_current(dt2);
else
_DB_STL_itr_->replace_current_key(dt2);
}
return dt2;
}
/// Assignment Operator.
/// \param me The object to assign with.
/// \return The object me's reference.
inline const self& operator=(const self& me)
{
ASSIGNMENT_PREDCOND(me)
*((ddt*)this) = (ddt)me;
if (_DB_STL_itr_ != NULL) {
// This object is the reference of an valid data
// element, so we must keep it that way, we don't
// use me's iterator here.
if (!_DB_STL_itr_->is_set_iterator())
_DB_STL_itr_->replace_current(
me._DB_STL_value());
else
_DB_STL_itr_->replace_current_key(
me._DB_STL_value());
} else if (me._DB_STL_delete_itr_) {
// Duplicate an iterator from me.
_DB_STL_delete_itr_ = true;
me._DB_STL_delete_itr_ = false;
_DB_STL_itr_ = me._DB_STL_itr_->dup_itr();
me._DB_STL_delete_itr_ = true;
}
return me;
}
//@}
/// Function to store the data element.
/// The user needs to call this method after modifying the underlying
/// object, so that the version stored in the container can be updated.
///
/// When db_base_iterator's directdb_get_ member is true, this function
/// must be called after modifying the data member and before any
/// subsequent container iterator dereference operations. If this step
/// is not carried out any changes will be lost.
///
/// If the data element is changed via ElementHolder<>::operator=(),
/// you don't need to call this function.
inline void _DB_STL_StoreElement()
{
assert(_DB_STL_itr_ != NULL);
_DB_STL_itr_->replace_current(*this);
}
/// Returns the data element this wrapper object wraps.
inline const ddt& _DB_STL_value() const
{
return *((ddt*)this);
}
/// Returns the data element this wrapper object wraps.
inline ddt& _DB_STL_value()
{
return *((ddt*)this);
}
#ifndef DOXYGEN_CANNOT_SEE_THIS
////////////////////////////////////////////////////////////////////
//
// The following methods are not part of the official public API,
// but can't be declared as protected, since it is not possible
// to declare template-specialised classes as friends.
//
// Call this function to tell this object that it should delete the
// _DB_STL_itr_ iterator because that iterator was allocated in
// the heap. Methods like db_vector/db_map<>::operator[] should call
// this function.
inline void _DB_STL_SetDelItr()
{
_DB_STL_delete_itr_ = true;
}
// Only copy data into this object, do not store into database.
inline void _DB_STL_CopyData(const self&dt2)
{
*((ddt*)this) = (ddt)dt2;
}
inline void _DB_STL_CopyData(const ddt&dt2)
{
*((ddt*)this) = dt2;
}
// Following functions are prefixed with _DB_STL_ to avoid
// potential name clash with ddt members.
//
inline iterator_type* _DB_STL_GetIterator() const
{
return _DB_STL_itr_;
}
inline int _DB_STL_GetData(ddt& d) const
{
d = *((ddt*)this);
return 0;
}
inline void _DB_STL_SetIterator(iterator_type*pitr)
{
_DB_STL_itr_ = pitr;
}
inline void _DB_STL_SetData(const ddt&d)
{
*(ddt*)this = d;
}
////////////////////////////////////////////////////////////////////
}; // ElementRef<>
template<typename T>
class DbstlSeqWriter;
#else
};
#endif // DOXYGEN_CANNOT_SEE_THIS
// The ElementHolder class must have an identical public interface to
// the ElementRef class.
/// A wrapper class for primitive types. It has identical usage and public
/// interface to the ElementRef class.
/// \sa ElementRef.
template <typename ptype>
class _exported ElementHolder
{
protected:
typedef ElementHolder<ptype> self;
inline void _DB_STL_put_new_value_to_db()
{
if (_DB_STL_itr_ != NULL) {
if (!_DB_STL_itr_->is_set_iterator())
_DB_STL_itr_->replace_current(dbstl_my_value_);
else
_DB_STL_itr_->replace_current_key(
dbstl_my_value_);
}
}
inline void _DB_STL_put_new_value_to_db(const self &me)
{
if (_DB_STL_itr_ != NULL) {
if (!_DB_STL_itr_->is_set_iterator())
_DB_STL_itr_->replace_current(dbstl_my_value_);
else
_DB_STL_itr_->replace_current_key(
dbstl_my_value_);
} else if (me._DB_STL_delete_itr_) {
// Duplicate an iterator from me.
_DB_STL_delete_itr_ = true;
me._DB_STL_delete_itr_ = false;
_DB_STL_itr_ = me._DB_STL_itr_->dup_itr();
me._DB_STL_delete_itr_ = true;
}
}
public:
typedef ptype type1;
typedef db_base_iterator<ptype> iterator_type;
typedef ptype content_type;
////////////////////////////////////////////////////////////////////
//
// Begin constructors and destructor.
//
/// \name Constructors and destructor.
//@{
/// Constructor.
/// If the pitr parameter is NULL or the default value is used, the
/// object created is a simple wrapper and not connected to a container.
/// If a valid iterator parameter is passed in, the wrapped element will
/// be associated with the matching key/data pair in the underlying
/// container.
/// \param pitr The iterator owning this object.
explicit inline ElementHolder(iterator_type* pitr = NULL)
{
_DB_STL_delete_itr_ = false;
_DB_STL_itr_ = pitr;
dbstl_str_buf_ = NULL;
dbstl_str_buf_len_ = 0;
memset(&dbstl_my_value_, 0, sizeof(dbstl_my_value_));
}
/// Constructor.
/// Initializes an ElementRef wrapper without an iterator. It can only
/// be used to wrap a data element in memory, it can't access an
/// unerlying database.
/// \param dt The base class object to initialize this object.
inline ElementHolder(const ptype&dt)
{
dbstl_str_buf_ = NULL;
dbstl_str_buf_len_ = 0;
_DB_STL_delete_itr_ = false;
_DB_STL_itr_ = NULL;
_DB_STL_CopyData_int(dt);
}
/// Copy constructor.
/// The constructor takes a "deep" copy. The created object will be
/// identical to, but independent from the original object.
/// \param other The object to clone from.
inline ElementHolder(const self& other)
{
dbstl_str_buf_ = NULL;
dbstl_str_buf_len_ = 0;
_DB_STL_delete_itr_ = other._DB_STL_delete_itr_;
_DB_STL_CopyData(other);
// Duplicate iterator if this object lives longer than
// _DB_STL_itr_.
_DB_STL_delete_itr_ = other._DB_STL_delete_itr_;
if (_DB_STL_delete_itr_) {
// Avoid recursive duplicate iterator calls.
other._DB_STL_delete_itr_ = false;
_DB_STL_itr_ = other._DB_STL_itr_->dup_itr();
other._DB_STL_delete_itr_ = true;
} else
_DB_STL_itr_ = other._DB_STL_itr_;
}
/// Destructor.
~ElementHolder() {
if (_DB_STL_delete_itr_) {
_DB_STL_delete_itr_ = false;
_DB_STL_itr_->delete_me();
}
if (dbstl_str_buf_) {
free(dbstl_str_buf_);
dbstl_str_buf_ = NULL;
}
}
//@}
////////////////////////////////////////////////////////////////////
/// This operator is a type converter. Where an automatic type
/// conversion is needed, this function is called to convert this
/// object into the primitive type it wraps.
operator ptype () const
{
return dbstl_my_value_;
}
// ElementHolder is a wrapper for primitive types, and backed by db,
// so we need to override all assignment operations to store updated
// value to database. We don't need to implement other operators for
// primitive types because we have a convert operator which can
// automatically convert to primitive type and use its C++ built in
// operator.
//
/** \name Math operators.
ElementHolder class templates also have all C/C++ self mutating
operators for numeric primitive types, including:
+=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=, ++, --
These operators should not be used when ddt is a sequence pointer type
like char* or wchar_t* or T*, otherwise the behavior is undefined.
These methods exist only to override default bahavior to store the
new updated value, otherwise, the type convert operator could have
done all the job.
As you know, some of them are not applicable to float or double types
or ElementHolder wrapper types for float/double types.
These operators not only modifies the cached data element, but also
stores new value to database if it associates a database key/data pair.
@{
*/
template <Typename T2>
const self& operator +=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ += p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator -=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ -= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator *=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ *= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator /=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ /= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator %=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ %= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator &=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ &= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator |=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ |= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
template <Typename T2>
const self& operator ^=(const ElementHolder<T2> &p2)
{
dbstl_my_value_ ^= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db(p2);
return *this;
}
const self& operator >>=(size_t n)
{
dbstl_my_value_ >>= n;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator <<=(size_t n)
{
dbstl_my_value_ <<= n;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator ^=(const self &p2)
{
dbstl_my_value_ ^= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator &=(const self &p2)
{
dbstl_my_value_ &= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator |=(const self &p2)
{
dbstl_my_value_ |= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator %=(const self &p2)
{
dbstl_my_value_ %= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator +=(const self &p2)
{
dbstl_my_value_ += p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator -=(const self &p2)
{
dbstl_my_value_ -= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator /=(const self &p2)
{
dbstl_my_value_ /= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
const self& operator *=(const self &p2)
{
dbstl_my_value_ *= p2.dbstl_my_value_;
_DB_STL_put_new_value_to_db();
return *this;
}
self& operator++()
{
dbstl_my_value_++;
_DB_STL_put_new_value_to_db();
return *this;
}
self operator++(int)
{
self obj(*this);
dbstl_my_value_++;
_DB_STL_put_new_value_to_db();
return obj;
}
self& operator--()
{
dbstl_my_value_--;
_DB_STL_put_new_value_to_db();
return *this;
}
self operator--(int)
{
self obj(*this);
dbstl_my_value_--;
_DB_STL_put_new_value_to_db();
return obj;
}
inline const ptype& operator=(const ptype& dt2)
{
_DB_STL_CopyData_int(dt2);
_DB_STL_put_new_value_to_db();
return dt2;
}
inline const self& operator=(const self& dt2)
{
ASSIGNMENT_PREDCOND(dt2)
_DB_STL_CopyData(dt2);
_DB_STL_put_new_value_to_db(dt2);
return dt2;
}
//@}
/// Returns the data element this wrapper object wraps;
inline const ptype& _DB_STL_value() const
{
return dbstl_my_value_;
}
/// Returns the data element this wrapper object wraps;
inline ptype&_DB_STL_value()
{
return dbstl_my_value_;
}
/// Function to store the data element.
/// The user needs to call this method after modifying the underlying
/// object, so that the version stored in the container can be updated.
///
/// When db_base_iterator's directdb_get_ member is true, this function
/// must be called after modifying the data member and before any
/// subsequent container iterator dereference operations. If this step
/// is not carried out any changes will be lost.
///
/// If the data element is changed via ElementHolder<>::operator=(),
/// you don't need to call this function.
inline void _DB_STL_StoreElement()
{
assert(_DB_STL_itr_ != NULL);
_DB_STL_itr_->replace_current(dbstl_my_value_);
}
#ifndef DOXYGEN_CANNOT_SEE_THIS
////////////////////////////////////////////////////////////////////
//
// The following methods are not part of the official public API,
// but can't be declared as protected, since it is not possible
// to declare template-specialised classes as friends.
//
inline void _DB_STL_CopyData(const self&dt2)
{
_DB_STL_CopyData_int(dt2.dbstl_my_value_);
}
template<Typename T>
inline void _DB_STL_CopyData_int(const T&src)
{
dbstl_my_value_ = src;
}
// Try to catch all types of pointers.
template<Typename T>
inline void _DB_STL_CopyData_int(T* const &src)
{
DbstlSeqWriter<T>::copy_to_holder((ElementHolder<T *> *)this,
(T *)src);
}
template<Typename T>
inline void _DB_STL_CopyData_int(const T* const &src)
{
DbstlSeqWriter<T>::copy_to_holder((ElementHolder<T *> *)this,
(T *)src);
}
template<Typename T>
inline void _DB_STL_CopyData_int(T* &src)
{
DbstlSeqWriter<T>::copy_to_holder((ElementHolder<T *> *)this,
(T *)src);
}
template<Typename T>
inline void _DB_STL_CopyData_int(const T*&src)
{
DbstlSeqWriter<T>::copy_to_holder((ElementHolder<T *> *)this,
(T *)src);
}
inline iterator_type* _DB_STL_GetIterator() const
{
return _DB_STL_itr_;
}
inline int _DB_STL_GetData(ptype& d) const
{
d = dbstl_my_value_;
return 0;
}
inline void _DB_STL_SetIterator(iterator_type*pitr)
{
_DB_STL_itr_ = pitr;
}
inline void _DB_STL_SetData(const ptype&d)
{
_DB_STL_CopyData_int(d);
}
inline void _DB_STL_SetDelItr()
{
_DB_STL_delete_itr_ = true;
}
// The two member has to be public for DbstlSeqWriter to access,
// but can't be accessed by user.
size_t dbstl_str_buf_len_;
void *dbstl_str_buf_; // Stores a sequence, used when ptype is T*
iterator_type *_DB_STL_itr_;
ptype dbstl_my_value_;
mutable bool _DB_STL_delete_itr_;
};
#else
};
#endif // DOXYGEN_CANNOT_SEE_THIS
//@} // Element_wrappers
//@} //dbstl_helper_classes
// These operators help reading from and writing to iostreams, if the wrapped
// data type has iostream operators.
template<Typename _CharT, Typename _Traits, Typename ddt>
basic_istream<_CharT,_Traits>&
operator>>( basic_istream<_CharT,_Traits> & in, ElementRef<ddt>&p)
{
in>>(ddt)p;
return in;
}
template<Typename _CharT, Typename _Traits, Typename ddt>
basic_ostream<_CharT,_Traits>&
operator<<( basic_ostream<_CharT,_Traits> & out,
const ElementRef<ddt>&p)
{
out<<(ddt)p;
return out;
}
template<Typename _CharT, Typename _Traits, Typename ddt>
basic_istream<_CharT,_Traits>&
operator>>( basic_istream<_CharT,_Traits> & in, ElementHolder<ddt>&p)
{
in>>p._DB_STL_value();
return in;
}
template<Typename _CharT, Typename _Traits, Typename ddt>
basic_ostream<_CharT,_Traits>&
operator<<( basic_ostream<_CharT,_Traits> & out,
const ElementHolder<ddt>&p)
{
out<<p._DB_STL_value();
return out;
}
template<typename T>
class _exported DbstlSeqWriter
{
public:
typedef ElementHolder<T *> HolderType;
static void copy_to_holder(HolderType *holder, T *src)
{
size_t i, slen, sql;
if (src == NULL) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
holder->dbstl_my_value_ = NULL;
return;
}
if (holder->dbstl_str_buf_len_ > DBSTL_MAX_DATA_BUF_LEN) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
}
typedef DbstlElemTraits<T> DM;
typename DM::SequenceCopyFunct seqcpy =
DM::instance()->get_sequence_copy_function();
typename DM::SequenceLenFunct seqlen =
DM::instance()->get_sequence_len_function();
typename DM::ElemSizeFunct elemszf =
DM::instance()->get_size_function();
assert(seqcpy != NULL && seqlen != NULL);
sql = seqlen(src);
if (elemszf == NULL)
slen = sizeof(T) * (sql + 1);
else
// We don't add the terminating object if it has one.
// So the registered functions should take care of it.
for (slen = 0, i = 0; i < sql; i++)
slen += elemszf(src[i]);
if (slen > holder->dbstl_str_buf_len_)
holder->dbstl_str_buf_ = DbstlReAlloc(
holder->dbstl_str_buf_,
holder->dbstl_str_buf_len_ = slen);
seqcpy((T*)holder->dbstl_str_buf_, src, sql);
holder->dbstl_my_value_ = (T*)holder->dbstl_str_buf_;
}
};
template<>
class _exported DbstlSeqWriter<char>
{
public:
typedef ElementHolder<char *> HolderType;
static void copy_to_holder(HolderType *holder, char *src)
{
size_t slen;
if (src == NULL) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
holder->dbstl_my_value_ = NULL;
return;
}
if (holder->dbstl_str_buf_len_ > DBSTL_MAX_DATA_BUF_LEN) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
}
slen = sizeof(char) * (strlen(src) + 1);
if (slen > holder->dbstl_str_buf_len_)
holder->dbstl_str_buf_ = DbstlReAlloc(
holder->dbstl_str_buf_,
(u_int32_t)(holder->dbstl_str_buf_len_ = slen));
strcpy((char*)holder->dbstl_str_buf_, src);
holder->dbstl_my_value_ = (char*)holder->dbstl_str_buf_;
}
};
template<>
class _exported DbstlSeqWriter<wchar_t>
{
public:
typedef ElementHolder<wchar_t *> HolderType;
static void copy_to_holder(HolderType *holder, wchar_t *src)
{
size_t slen;
if (src == NULL) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
holder->dbstl_my_value_ = NULL;
return;
}
if (holder->dbstl_str_buf_len_ > DBSTL_MAX_DATA_BUF_LEN) {
free(holder->dbstl_str_buf_);
holder->dbstl_str_buf_ = NULL;
}
slen = sizeof(wchar_t) * (wcslen(src) + 1);
if (slen > holder->dbstl_str_buf_len_)
holder->dbstl_str_buf_ = DbstlReAlloc(
holder->dbstl_str_buf_,
holder->dbstl_str_buf_len_ = slen);
wcscpy((wchar_t*)holder->dbstl_str_buf_, src);
holder->dbstl_my_value_ = (wchar_t*)holder->dbstl_str_buf_;
}
};
END_NS
#endif// !_DB_STL_KDPAIR_H
|