Debian Codegraph @

lang/cxx/stl/dbstl_element_ref.h

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