NCBI C++ ToolKit
ncbimisc.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB___NCBIMISC__HPP
2 #define CORELIB___NCBIMISC__HPP
3 
4 /* $Id: ncbimisc.hpp 100071 2023-06-12 19:29:39Z sadyrovr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Denis Vakatov, Eugene Vasilchenko
30  *
31  *
32  */
33 
34 /// @file ncbimisc.hpp
35 /// Miscellaneous common-use basic types and functionality
36 
37 
38 #include <corelib/ncbidbg.hpp>
39 #include <stdlib.h>
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43 
44 #if !defined(HAVE_NULLPTR) && !defined(nullptr)
45 # define nullptr NULL
46 #endif
47 
48 
49 /** @addtogroup AppFramework
50  *
51  * @{
52  */
53 
54 
55 #ifndef NCBI_ESWITCH_DEFINED
56 #define NCBI_ESWITCH_DEFINED
57 
58 extern "C" {
59 
60 /*
61  * ATTENTION! Do not change this enumeration!
62  *
63  * It must always be kept in sync with its plain C counterpart defined in
64  * "connect/ncbi_types.h". If you absolutely(sic!) need to alter this
65  * type, please apply equivalent changes to both definitions.
66  */
67 
68 /** Aux. enum to set/unset/default various features.
69  */
70 typedef enum ENcbiSwitch {
71  eOff = 0,
72  eOn,
73  eDefault
75 
76 } // extern "C"
77 
78 #endif //!NCBI_ESWITCH_DEFINED
79 
80 
81 #ifndef NCBI_EOWNERSHIP_DEFINED
82 #define NCBI_EOWNERSHIP_DEFINED
83 
84 extern "C" {
85 
86 /*
87  * ATTENTION! Do not change this enumeration!
88  *
89  * It must always be kept in sync with its plain C counterpart defined in
90  * "connect/ncbi_types.h". If you absolutely(sic!) need to alter this
91  * type, please apply equivalent changes to both definitions.
92  */
93 
94 /** Ownership relations between objects.
95  *
96  * Can be used to define or transfer ownership of objects.
97  * For example, specify if a CSocket object owns its underlying SOCK object.
98  */
99 typedef enum ENcbiOwnership {
100  eNoOwnership, /** No ownership assumed */
101  eTakeOwnership /** An object can take ownership of another */
103 
104 } // extern "C"
105 
106 #endif //!NCBI_EOWNERSHIP_DEFINED
107 
108 
110 
111 
112 /// Whether a value is nullable.
113 enum ENullable {
114  eNullable, ///< Value can be null
115  eNotNullable ///< Value cannot be null
116 };
117 
118 
119 /// Signedness of a value.
120 enum ESign {
121  eNegative = -1, ///< Value is negative
122  eZero = 0, ///< Value is zero
123  ePositive = 1 ///< Value is positive
124 };
125 
126 
127 /// Enumeration to represent a tristate value.
128 enum ETriState {
129  eTriState_Unknown = -1, ///< The value is indeterminate
130  eTriState_False = 0, ///< The value is equivalent to false/no
131  eTriState_True = 1, ///< The value is equivalent to true/yes
132 };
133 
134 
135 /// Whether to truncate/round a value.
136 enum ERound {
137  eTrunc, ///< Value must be truncated
138  eRound ///< Value must be rounded
139 };
140 
141 
142 /// Whether to follow symbolic links (also known as shortcuts or aliases)
144  eIgnoreLinks, ///< Do not follow symbolic links
145  eFollowLinks ///< Follow symbolic links
146 };
147 
148 
149 /// Whether to normalize a path
151  eNormalizePath, ///< Normalize a path
152  eNotNormalizePath ///< Do not normalize a path
153 };
154 
155 
156 /// Interrupt on signal mode
157 ///
158 /// On UNIX some functions can be interrupted by a signal and EINTR errno
159 /// value. We can restart or cancel its execution.
161  eInterruptOnSignal, ///< Cancel operation if interrupted by a signal
162  eRestartOnSignal ///< Restart operation if interrupted by a signal
163 };
164 
165 
166 /// Can the action be retried?
168  eRetriable_No, ///< It makes no sense to retry the action
169  eRetriable_Unknown, ///< It is unknown if the action can succeed if retried
170  eRetriable_Yes ///< It makes sense to try again
171 };
172 
173 
174 /////////////////////////////////////////////////////////////////////////////
175 /// Support for safe bool operators
176 /////////////////////////////////////////////////////////////////////////////
177 
178 
179 /// Low level macro for declaring safe bool operator.
180 #define DECLARE_SAFE_BOOL_METHOD(Expr) \
181  struct SSafeBoolTag { \
182  void SafeBoolTrue(SSafeBoolTag*) {} \
183  }; \
184  typedef void (SSafeBoolTag::*TBoolType)(SSafeBoolTag*); \
185  operator TBoolType() const { \
186  return (Expr)? &SSafeBoolTag::SafeBoolTrue: 0; \
187  } \
188  private: \
189  bool operator==(TBoolType) const; \
190  bool operator!=(TBoolType) const; \
191  public: \
192  friend struct SSafeBoolTag
193 
194 
195 /// Declaration of safe bool operator from boolean expression.
196 /// Actual operator declaration will be:
197 /// operator TBoolType(void) const;
198 /// where TBoolType is a typedef convertible to bool (member pointer).
199 #define DECLARE_OPERATOR_BOOL(Expr) \
200  DECLARE_SAFE_BOOL_METHOD(Expr)
201 
202 
203 /// Declaration of safe bool operator from pointer expression.
204 /// Actual operator declaration will be:
205 /// operator bool(void) const;
206 #define DECLARE_OPERATOR_BOOL_PTR(Ptr) \
207  DECLARE_OPERATOR_BOOL((Ptr) != 0)
208 
209 
210 /// Declaration of safe bool operator from CRef<>/CConstRef<> expression.
211 /// Actual operator declaration will be:
212 /// operator bool(void) const;
213 #define DECLARE_OPERATOR_BOOL_REF(Ref) \
214  DECLARE_OPERATOR_BOOL((Ref).NotNull())
215 
216 /// Override the DECLARE_OPERATOR_BOOL, etc.
217 /// from an ancestor class. This is needed because sometimes you can't just
218 /// use DECLARE_OPERATOR_BOOL due to cases such as the ancestor class
219 /// being privately inherited from.
220 #define OVERRIDE_OPERATOR_BOOL(TAncestorClass, NewBoolExpr) \
221  using TAncestorClass::TBoolType; \
222  using TAncestorClass::SSafeBoolTag; \
223  operator TBoolType() const { \
224  return (NewBoolExpr)? & SSafeBoolTag::SafeBoolTrue : 0; \
225  }
226 
227 /// Template used for empty base class optimization.
228 /// See details in the August '97 "C++ Issue" of Dr. Dobb's Journal
229 /// Also available from http://www.cantrip.org/emptyopt.html
230 /// We store usually empty template argument class together with data member.
231 /// This template is much like STL's pair<>, but the access to members
232 /// is done though methods first() and second() returning references
233 /// to corresponding members.
234 /// First template argument is represented as private base class,
235 /// while second template argument is represented as private member.
236 /// In addition to constructor taking two arguments,
237 /// we add constructor for initialization of only data member (second).
238 /// This is useful since usually first type is empty and doesn't require
239 /// non-trivial constructor.
240 /// We do not define any comparison functions as this template is intented
241 /// to be used internally within another templates,
242 /// which themselves should provide any additional functionality.
243 
244 template<class Base, class Member>
245 class pair_base_member : private Base
246 {
247 public:
248  typedef Base base_type;
249  typedef Base first_type;
250  typedef Member member_type;
251  typedef Member second_type;
252 
254  : base_type(), m_Member()
255  {
256  }
257 
258  explicit pair_base_member(const member_type& member_value)
259  : base_type(), m_Member(member_value)
260  {
261  }
262 
263  explicit pair_base_member(const first_type& first_value,
264  const second_type& second_value)
265  : base_type(first_value), m_Member(second_value)
266  {
267  }
268 
269  const first_type& first() const
270  {
271  return *this;
272  }
274  {
275  return *this;
276  }
277 
278  const second_type& second() const
279  {
280  return m_Member;
281  }
283  {
284  return m_Member;
285  }
286 
288  {
289  if (static_cast<void*>(&first()) != static_cast<void*>(&second())){
290  // work around an IBM compiler bug which causes it to perform
291  // a spurious 1-byte swap, yielding mixed-up values.
292  swap(first(), p.first());
293  }
294  swap(second(), p.second());
295  }
296 
297 private:
299 };
300 
301 
302 /// Template used to replace bool type arguments with some strict equivalent.
303 /// This allow to prevent compiler to do an implicit casts from other types
304 /// to bool. "TEnum" should be an enumerated type with two values:
305 /// - negative (FALSE/OFF) should have value 0
306 // - positive (TRUE/ON) have value 1.
307 
308 template <class TEnum>
310 {
311 public:
312  // Constructors
315 
316  /// Operator bool
317  operator bool() const { return m_Value; }
318  /// Operator enum
319  operator TEnum () const { return TEnum(m_Value); }
320 
321 private:
322  bool m_Value;
323 
324 private:
325  // Disable implicit conversions from/to other types
326  CBoolEnum(char*);
327  CBoolEnum(int);
328  CBoolEnum(unsigned int);
329  template <class T> CBoolEnum(T);
330 
331  operator int() const;
332  operator unsigned int() const;
333 };
334 
335 
336 
337 /// Functor template for allocating object.
338 template<class X>
339 struct Creater
340 {
341  /// Default create function.
342  static X* Create(void)
343  { return new X; }
344 };
345 
346 /// Functor template for deleting object.
347 template<class X>
348 struct Deleter
349 {
350  /// Default delete function.
351  static void Delete(X* object)
352  { delete object; }
353 };
354 
355 /// Functor template for deleting array of objects.
356 template<class X>
358 {
359  /// Array delete function.
360  static void Delete(X* object)
361  { delete[] object; }
362 };
363 
364 /// Functor template for the C language deallocation function, free().
365 template<class X>
366 struct CDeleter
367 {
368  /// C Language deallocation function.
369  static void Delete(X* object)
370  { free(object); }
371 };
372 
373 
374 
375 /////////////////////////////////////////////////////////////////////////////
376 ///
377 /// AutoPtr --
378 ///
379 /// Define an "auto_ptr" like class that can be used inside STL containers.
380 ///
381 /// The Standard auto_ptr template from STL doesn't allow the auto_ptr to be
382 /// put in STL containers (list, vector, map etc.). The reason for this is
383 /// the absence of copy constructor and assignment operator.
384 /// We decided that it would be useful to have an analog of STL's auto_ptr
385 /// without this restriction - AutoPtr.
386 ///
387 /// Due to the nature of AutoPtr its copy constructor and assignment operator
388 /// modify the state of the source AutoPtr object as it transfers the ownership
389 /// to the target AutoPtr object. Also, we added possibility to redefine the
390 /// way pointer will be deleted: the second argument of the template allows
391 /// pointers from "malloc" in AutoPtr, or you can use "ArrayDeleter" (see
392 /// above) to properly delete an array of objects using "delete[]" instead
393 /// of "delete". By default, the internal pointer is deleted by the C++
394 /// "delete" operator.
395 ///
396 /// @sa
397 /// Deleter(), ArrayDeleter(), CDeleter()
398 
399 template< class X, class Del = Deleter<X> >
400 class AutoPtr
401 {
402 public:
403  typedef X element_type; ///< Define element type.
404  typedef Del deleter_type; ///< Alias for template argument.
405 
406  /// Constructor.
408  : m_Ptr(p), m_Data(true)
409  { }
410 
411  /// Constructor.
412  AutoPtr(element_type* p, const deleter_type& deleter)
413  : m_Ptr(p), m_Data(deleter, true)
414  { }
415 
416  /// Constructor, own the pointed object if ownership == eTakeOwnership
418  : m_Ptr(p), m_Data(ownership != eNoOwnership)
419  { }
420 
421  /// Constructor, own the pointed object if ownership == eTakeOwnership
422  AutoPtr(element_type* p, const deleter_type& deleter, EOwnership ownership)
423  : m_Ptr(p), m_Data(deleter, ownership != eNoOwnership)
424  { }
425 
426  /// Copy constructor.
428  : m_Ptr(0), m_Data(p.m_Data)
429  {
430  m_Ptr = p.x_Release();
431  }
432 
433  /// Destructor.
434  ~AutoPtr(void)
435  {
436  reset();
437  }
438 
439  /// Assignment operator.
441  {
442  if (this != &p) {
443  bool owner = p.m_Data.second();
444  reset(p.x_Release());
445  m_Data.second() = owner;
446  }
447  return *this;
448  }
449 
450  /// Assignment operator.
452  {
453  reset(p);
454  return *this;
455  }
456 
457  /// Bool operator for use in if() clause.
459 
460  // Standard getters.
461 
462  /// Dereference operator.
463  element_type& operator* (void) const { return *m_Ptr; }
464 
465  /// Reference operator.
466  element_type* operator->(void) const { return m_Ptr; }
467 
468  /// Get pointer.
469  element_type* get (void) const { return m_Ptr; }
470 
471  /// Release will release ownership of pointer to caller.
473  {
474  m_Data.second() = false;
475  return m_Ptr;
476  }
477 
478  /// Reset will delete the old pointer (if owned), set content to the new
479  /// value, and assume the ownership upon the new pointer by default.
480  void reset(element_type* p = 0, EOwnership ownership = eTakeOwnership)
481  {
482  if ( m_Ptr != p ) {
483  if (m_Ptr && m_Data.second()) {
484  m_Data.first().Delete(release());
485  }
486  m_Ptr = p;
487  }
488  m_Data.second() = ownership != eNoOwnership;
489  }
490 
492  {
493  swap(m_Ptr, a.m_Ptr);
494  swap(m_Data, a.m_Data);
495  }
496 
497  bool IsOwned(void) const { return m_Ptr && m_Data.second(); }
498 
499 private:
500  element_type* m_Ptr; ///< Internal pointer representation.
502 
503  /// Release for const object.
504  element_type* x_Release(void) const
505  {
506  return const_cast<AutoPtr<X, Del>*>(this)->release();
507  }
508 };
509 
510 
511 /////////////////////////////////////////////////////////////////////////////
512 ///
513 /// AutoArray --
514 ///
515 /// "AutoPtr" like class for using with arrays
516 ///
517 /// vector<> template comes with a performance penalty, since it always
518 /// initializes its content. This template is not a vector replacement,
519 /// it's a version of AutoPtr<> tuned for array pointers. For convenience
520 /// it defines array style access operator [] and size based contructor.
521 ///
522 /// @sa AutoPtr
523 ///
524 
525 template< class X, class Del = ArrayDeleter<X> >
527 {
528 public:
529  typedef X element_type; ///< Define element type.
530  typedef Del deleter_type; ///< Alias for template argument.
531 
532 public:
533 
534  /// Construct the array using C++ new[] operator
535  /// @note In this case you should use ArrayDeleter<> or compatible
536  explicit AutoArray(size_t size)
537  : m_Ptr(new element_type[size]), m_Data(true)
538  { }
539 
540  explicit AutoArray(element_type* p = 0)
541  : m_Ptr(p), m_Data(true)
542  { }
543 
544  AutoArray(element_type* p, const deleter_type& deleter)
545  : m_Ptr(p), m_Data(deleter, true)
546  { }
547 
549  : m_Ptr(0), m_Data(p.m_Data)
550  {
551  m_Ptr = p.x_Release();
552  }
553 
555  {
556  reset();
557  }
558 
559  /// Assignment operator.
561  {
562  if (this != &p) {
563  bool owner = p.m_Data.second();
564  reset(p.x_Release());
565  m_Data.second() = owner;
566  }
567  return *this;
568  }
569 
570  /// Assignment operator.
572  {
573  reset(p);
574  return *this;
575  }
576 
577  /// Bool operator for use in if() clause.
579 
580  /// Get pointer.
581  element_type* get (void) const { return m_Ptr; }
582 
583  /// Release will release ownership of pointer to caller.
585  {
586  m_Data.second() = false;
587  return m_Ptr;
588  }
589 
590  /// Array style dereference (returns value)
591  const element_type& operator[](size_t pos) const { return m_Ptr[pos]; }
592 
593  /// Array style dereference (returns reference)
594  element_type& operator[](size_t pos) { return m_Ptr[pos]; }
595 
596  /// Reset will delete the old pointer, set content to the new value,
597  /// and assume the ownership upon the new pointer.
598  void reset(element_type* p = 0)
599  {
600  if (m_Ptr != p) {
601  if (m_Ptr && m_Data.second()) {
602  m_Data.first().Delete(release());
603  }
604  m_Ptr = p;
605  }
606  m_Data.second() = true;
607  }
608 
610  {
611  swap(m_Ptr, a.m_Ptr);
612  swap(m_Data, a.m_Data);
613  }
614 
615 private:
616  /// Release for const object.
617  element_type* x_Release(void) const
618  {
619  return const_cast<AutoArray<X, Del>*>(this)->release();
620  }
621 
622 private:
625 };
626 
627 
628 
629 /////////////////////////////////////////////////////////////////////////////
630 ///
631 /// CNullable --
632 ///
633 /// A value whith 'unassigned' state.
634 ///
635 
636 
637 /// Define "null" pointer value.
638 #ifdef __cpp_using_enum
639 enum class ENull {
640  null = 0
641 };
642 
643 using enum ENull;
644 #else
645 enum ENull {
646  null = 0
647 };
648 #endif
649 
650 
651 // Default callback for null value - throws CCoreException.
652 NCBI_NORETURN NCBI_XNCBI_EXPORT void g_ThrowOnNull(void);
653 
654 // Default callback template.
655 template <class TValue>
657 {
658  TValue operator()(void) const
659  {
660  g_ThrowOnNull();
661  }
662 };
663 
664 
665 /// Template class allowing to store a value or null (unassigned) state.
666 /// TNullToValue functor can be used to perform an action when the value
667 /// is requested from a null object. By default CCoreException is thrown.
668 /// To perform other actions (e.g. provide a default value) the functor
669 /// must define 'TValue operator()(void) const' method.
670 /// @deprecated: Use std::optional<> instead.
671 template <class TValue, class TNullToValue = SThrowOnNull<TValue> >
673 //
674 // DEPRECATED! - Use std::optional<> instead.
675 //
676 {
677 public:
678  /// Create an empty nullable.
679  CNullable(ENull = null)
680  : m_IsNull(true) {}
681  /// Initialize nullable with a specific value.
682  CNullable(TValue value)
683  : m_IsNull(false), m_Value(value) {}
684 
685  /// Check if the object is unassigned.
686  bool IsNull(void) const
687  {
688  return m_IsNull;
689  }
690 
691  /// Get nullable value.
692  /// If NULL, then call TNullToValue and use the value return by the latter.
693  /// @attention The default implementation of TNullToValue (g_ThrowOnNull)
694  /// throws an exception!
695  operator TValue(void) const
696  {
697  return m_IsNull ? TNullToValue()() : m_Value;
698  }
699 
700  /// Get a const reference to the current value. If NULL, the reference is
701  /// a copy of the default value or throw (depending on TNullToValue
702  /// implementation). In any case the IsNull state is unchanged.
703  const TValue& GetValue(void) const
704  {
705  if ( m_IsNull ) {
706  const_cast<TValue&>(m_Value) = TNullToValue()();
707  }
708  return m_Value;
709  }
710 
711  /// Get a non-const reference to the value. If NULL, try to initialize this
712  /// instance with the default value or throw.
713  TValue& SetValue(void)
714  {
715  if ( m_IsNull ) {
716  *this = TNullToValue()();
717  }
718  return m_Value;
719  }
720 
721  /// Assign a value to the nullable.
723  {
724  m_IsNull = false; m_Value = value;
725  return *this;
726  }
727 
728  /// Reset nullable to unassigned state.
729  CNullable& operator= (ENull /*null_value*/)
730  {
731  m_IsNull = true;
732  return *this;
733  }
734 
735 private:
736  bool m_IsNull;
737  TValue m_Value;
738 };
739 
740 template <class TValue, class TNullToValue = SThrowOnNull<TValue> >
742 {
743  return (l.IsNull() && r.IsNull()) ||
744  (!l.IsNull() && !r.IsNull() && l.GetValue() == r.GetValue());
745 }
746 template <class TValue, class TNullToValue = SThrowOnNull<TValue> >
748 {
749  return !operator==(l, r);
750 }
751 
752 // "min" and "max" templates
753 //
754 
755 // Always get rid of the old non-conformant min/max macros
756 #ifdef min
757 # undef min
758 #endif
759 #ifdef max
760 # undef max
761 #endif
762 
763 
764 
765 // strdup()
766 //
767 
768 #ifndef HAVE_STRDUP
769 /// Supply string duplicate function, if one is not defined.
770 extern char* strdup(const char* str);
771 #endif
772 
773 
774 
775 // ITERATE
776 // NON_CONST_ITERATE
777 // REVERSE_ITERATE
778 // NON_CONST_REVERSE_ITERATE
779 // ERASE_ITERATE
780 //
781 // Useful macros to write 'for' statements with the STL container iterator as
782 // a variable.
783 //
784 
785 // *ITERATE helper to enforce constness of the container reference
786 template<typename Type>
787 inline const Type& s_ITERATE_ConstRef(const Type& obj)
788 {
789  return obj;
790 }
791 #define ITERATE_CONST(Cont) NCBI_NS_NCBI::s_ITERATE_ConstRef(Cont)
792 
793 // *ITERATE helper to verify that the container isn't a temporary object
794 #ifdef _DEBUG
795 template<typename Type>
796 inline bool s_ITERATE_SameObject(const Type& obj1, const Type& obj2)
797 {
798  return &obj1 == &obj2;
799 }
800 # define ITERATE_BEGIN(Cont, Begin) \
801  (NCBI_ASSERT_EXPR(NCBI_NS_NCBI::s_ITERATE_SameObject(Cont, Cont), \
802  "rvalue container in *ITERATE"), (Cont).Begin())
803 #else
804 # define ITERATE_BEGIN(Cont, Begin) ((Cont).Begin())
805 #endif
806 
807 // *ITERATE helper macro to declare iterator variable
808 #if 0 && defined(NCBI_HAVE_CXX11)
809 # define ITERATE_VAR(Type) auto
810 #else
811 # define ITERATE_VAR(Type) Type
812 #endif
813 
814 /// ITERATE macro to sequence through container elements.
815 #define ITERATE(Type, Var, Cont) \
816  for ( ITERATE_VAR(Type::const_iterator) \
817  Var = ITERATE_BEGIN(ITERATE_CONST(Cont), begin), \
818  NCBI_NAME2(Var,_end) = ITERATE_CONST(Cont).end(); \
819  Var != NCBI_NAME2(Var,_end); ++Var )
820 
821 /// Non constant version of ITERATE macro.
822 #define NON_CONST_ITERATE(Type, Var, Cont) \
823  for ( ITERATE_VAR(Type::iterator) Var = ITERATE_BEGIN(Cont, begin); \
824  Var != (Cont).end(); ++Var )
825 
826 /// ITERATE macro to reverse sequence through container elements.
827 #define REVERSE_ITERATE(Type, Var, Cont) \
828  for ( ITERATE_VAR(Type::const_reverse_iterator) \
829  Var = ITERATE_BEGIN(ITERATE_CONST(Cont), rbegin), \
830  NCBI_NAME2(Var,_end) = ITERATE_CONST(Cont).rend(); \
831  Var != NCBI_NAME2(Var,_end); ++Var )
832 
833 /// Non constant version of REVERSE_ITERATE macro.
834 #define NON_CONST_REVERSE_ITERATE(Type, Var, Cont) \
835  for ( ITERATE_VAR(Type::reverse_iterator) \
836  Var = ITERATE_BEGIN(Cont, rbegin); \
837  Var != (Cont).rend(); ++Var )
838 
839 /// Non-constant version with ability to erase current element, if container
840 /// permits. Use only on containers, for which erase do not ruin other
841 /// iterators into the container, e.g. map, list, but NOT vector.
842 /// See also VECTOR_ERASE
843 #define ERASE_ITERATE(Type, Var, Cont) \
844  for ( ITERATE_VAR(Type::iterator) Var = ITERATE_BEGIN(Cont, begin), \
845  NCBI_NAME2(Var,_next) = Var; \
846  (Var = NCBI_NAME2(Var,_next)) != (Cont).end() && \
847  (++NCBI_NAME2(Var,_next), true); )
848 
849 /// Use this macro inside body of ERASE_ITERATE cycle to erase from
850 /// vector-like container. Plain erase() call would invalidate Var_next
851 /// iterator and would make the cycle controlling code to fail.
852 #define VECTOR_ERASE(Var, Cont) (NCBI_NAME2(Var,_next) = (Cont).erase(Var))
853 
854 /// The body of the loop will be run with Var equal to false and then true.
855 /// The seemlingly excessive complexity of this macro is to get around a couple of limitations:
856 /// * A bool only has two states, so it's not possible to represent the complete state space
857 /// of (first iteration, second iteration, done) without another variable.
858 /// * The variables declared in a for-loop's first part must be of the same type, so
859 /// the other variable has to be a bool instead of something more convenient such as
860 /// as a loop-counter.
861 #define ITERATE_BOTH_BOOL_VALUES(BoolVar) \
862  for( bool BoolVar##BOTH_BOOL_VALUES_DONE##__LINE__ = false, BoolVar = false; ! BoolVar##BOTH_BOOL_VALUES_DONE##__LINE__ ; BoolVar##BOTH_BOOL_VALUES_DONE##__LINE__ = BoolVar, BoolVar = true )
863 
864 /// idx loops from 0 (inclusive) to up_to (exclusive)
865 #define ITERATE_0_IDX(idx, up_to) \
866  for( TSeqPos idx = 0; idx < up_to; ++idx )
867 
868 /// Just repeat the body of the loop num_iters times
869 #define ITERATE_SIMPLE(num_iters) \
870  ITERATE_0_IDX( _dummy_idx_94768308_##__LINE__, num_iters ) // the number has no significance; it is entirely random.
871 
872 /// Type for sequence locations and lengths.
873 ///
874 /// Use this typedef rather than its expansion, which may change.
875 typedef unsigned int TSeqPos;
876 
877 /// Define special value for invalid sequence position.
878 const TSeqPos kInvalidSeqPos = ((TSeqPos) (-1));
879 
880 
881 /// Type for signed sequence position.
882 ///
883 /// Use this type when and only when negative values are a possibility
884 /// for reporting differences between positions, or for error reporting --
885 /// though exceptions are generally better for error reporting.
886 /// Use this typedef rather than its expansion, which may change.
887 typedef int TSignedSeqPos;
888 
889 
890 /// Template class for strict ID types.
891 template<class TKey, class TStorage>
893 {
894 public:
895  typedef TStorage TId;
897 
898  CStrictId(void) : m_Id(0) {}
899  CStrictId(const TThis& other) : m_Id(other.m_Id) {}
900  TThis& operator=(const TThis& other) { m_Id = other.m_Id; return *this; }
901 
902  bool operator==(const TThis& id) const { return m_Id == id.m_Id; }
903  bool operator!=(const TThis& id) const { return m_Id != id.m_Id; }
904  bool operator<(const TThis& id) const { return m_Id < id.m_Id; }
905  bool operator<=(const TThis& id) const { return m_Id <= id.m_Id; }
906  bool operator>(const TThis& id) const { return m_Id > id.m_Id; }
907  bool operator>=(const TThis& id) const { return m_Id >= id.m_Id; }
908 
909  TThis& operator++(void) { m_Id++; return *this; }
910  TThis operator++(int) { TThis tmp = *this; m_Id++; return tmp; }
911  TThis& operator--(void) { m_Id--; return *this; }
912  TThis operator--(int) { TThis tmp = *this; m_Id--; return tmp; }
913 
914  TThis operator+(const TThis& id) const { return TThis(m_Id + id.m_Id); }
915  TThis operator-(const TThis& id) const { return TThis(m_Id - id.m_Id); }
916  TThis operator-(void) const { return TThis(-m_Id); }
917 
918  //template<typename T>
919  //operator typename enable_if<is_same<T, bool>::value, bool>::type(void) const { return m_Id != 0; }
920 
921  TId Get(void) const { return m_Id; }
922  void Set(TId id) { m_Id = id; }
923  TId& Set(void) { return m_Id; }
924 
925 #if !defined(NCBI_TEST_APPLICATION)
926  template<typename T>
927  explicit CStrictId(T id, typename enable_if<is_same<T, TId>::value, T>::type = 0) : m_Id(id) {}
928 private:
929 #else
930  template<typename T>
931  CStrictId(T id, typename enable_if<is_arithmetic<T>::value, T>::type = 0) : m_Id(id) {}
932  template<typename T>
933  typename enable_if<is_arithmetic<T>::value, TThis>::type& operator=(T id) { m_Id = id; return *this; }
934  template<typename T>
935  typename enable_if<is_arithmetic<T>::value, bool>::type operator==(T id) const { return m_Id == id; }
937  operator T(void) const { return static_cast<T>(m_Id); }
938 #endif
939 
940 private:
942 };
943 
944 template<class TKey, class TStorage>
945 inline
947 {
948  return out << id.Get();
949 }
950 
951 template<class TKey, class TStorage>
952 inline
954 {
955  in >> id.Set();
956  return in;
957 }
958 
959 
960 /// Type for sequence GI.
961 ///
962 /// Use this typedef rather than its expansion, which may change.
963 
964 //#define NCBI_INT4_GI
965 //#define NCBI_STRICT_GI
966 //#define NCBI_STRICT_ENTREZ_ID
967 //#define NCBI_STRICT_TAX_ID
968 
969 
970 #if defined(NCBI_INT4_GI)
971 # ifdef NCBI_INT8_GI
972 # error "Both NCBI_INT4_GI and NCBI_INT8_GI must not be defined!"
973 # endif
974 # ifdef NCBI_STRICT_GI
975 # error "Both NCBI_INT4_GI and NCBI_STRICT_GI must not be defined!"
976 # endif
977 #elif !defined(NCBI_INT8_GI)
978 # define NCBI_INT8_GI
979 #endif
980 
981 #ifdef NCBI_TEST_STRICT_ENTREZ_ID
982 // Allow strict TEntrezId test builds
983 #define NCBI_STRICT_ENTREZ_ID
984 #else
985 // Temporary fix: disable strict TEntrezId
986 # ifdef NCBI_STRICT_ENTREZ_ID
987 # undef NCBI_STRICT_ENTREZ_ID
988 # endif
989 #endif
990 
991 #ifndef NCBI_STRICT_GI
992 # undef NCBI_STRICT_ENTREZ_ID
993 # undef NCBI_STRICT_TAX_ID
994 #endif
995 
996 #ifdef NCBI_INT8_GI
997 
998 // Generic id type which needs to be the same size as GI.
999 typedef Int8 TIntId;
1000 typedef Uint8 TUintId;
1001 
1002 #else // NCBI_INT8_GI
1003 
1004 typedef int TGi;
1005 typedef Int4 TIntId;
1006 typedef Uint4 TUintId;
1007 
1008 #endif
1009 
1010 
1011 /// Key structs must always be defined - they are used in aliasinfo.cpp
1013  typedef TIntId TId;
1014 };
1016  typedef TIntId TId;
1017 };
1019  typedef int TId;
1020 };
1021 
1022 
1023 #ifdef NCBI_STRICT_GI
1024 
1026 
1027 /// @deprecated: Use TGi/TEntrezId typedefs.
1029 
1030 #else // NCBI_STRICT_GI
1031 
1032 typedef SStrictId_Gi::TId TGi;
1033 
1034 #endif // NCBI_STRICT_GI
1035 
1036 
1037 /// TEntrezId type for entrez ids which require the same strictness as TGi.
1038 #ifdef NCBI_STRICT_ENTREZ_ID
1040 #else
1042 #endif
1043 
1044 /// Taxon id type.
1045 #ifdef NCBI_STRICT_TAX_ID
1047 #else
1049 #endif
1050 
1051 
1052 /// a helper template to enforce constness of argument to GI_CONST macro
1053 template<class TId, TId id>
1055 public:
1056 #ifndef NCBI_COMPILER_MSVC
1057  enum : TId {
1058  value = id
1059  };
1060 #else
1061  static const TId value = id;
1062 #endif
1063 };
1064 
1065 #ifdef NCBI_STRICT_GI
1066 
1067 /// Macros to convert TGi to other types (int, unsigned etc.).
1068 #define STRICT_ID_TO(TId, TInt, id) (static_cast<TInt>((id).Get()))
1069 #define STRICT_ID_FROM(TIdType, TIntType, id) (TIdType(static_cast<TIdType::TId>(id)))
1070 #define STRICT_ID_CONST(type, id) \
1071  (type(static_cast<type::TId>(ncbi::CConstIdChecker<type::TId, id>::value)))
1072 #define STRICT_ID_ZERO(type) STRICT_ID_CONST(type, 0)
1073 #define STRICT_ID_INVALID(type) STRICT_ID_CONST(type, -1)
1074 
1075 #else // NCBI_STRICT_GI
1076 
1077 #define STRICT_ID_TO(TId, TInt, id) (static_cast<TInt>(id))
1078 #define STRICT_ID_FROM(TIdType, TIntType, id) (static_cast<TIdType>(id))
1079 #define STRICT_ID_CONST(type, id) (ncbi::CConstIdChecker<type, id>::value)
1080 #define STRICT_ID_ZERO(type) type(0)
1081 #define STRICT_ID_INVALID(type) type(-1)
1082 
1083 #endif // NCBI_STRICT_GI
1084 
1085 #define GI_TO(T, gi) STRICT_ID_TO(ncbi::TGi, T, gi)
1086 #define GI_FROM(T, value) STRICT_ID_FROM(ncbi::TGi, T, value)
1087 #define GI_CONST(gi) STRICT_ID_CONST(ncbi::TGi, gi)
1088 #define ZERO_GI STRICT_ID_ZERO(ncbi::TGi)
1089 #define INVALID_GI STRICT_ID_INVALID(ncbi::TGi)
1090 
1091 
1092 #ifdef NCBI_STRICT_ENTREZ_ID
1093 # define ENTREZ_ID_TO(T, entrez_id) STRICT_ID_TO(ncbi::TEntrezId, T, entrez_id)
1094 # define ENTREZ_ID_FROM(T, value) STRICT_ID_FROM(ncbi::TEntrezId, T, value)
1095 # define ENTREZ_ID_CONST(id) STRICT_ID_CONST(ncbi::TEntrezId, id)
1096 #else
1097 # define ENTREZ_ID_TO(T, entrez_id) (static_cast<T>(entrez_id))
1098 # define ENTREZ_ID_FROM(T, value) (static_cast<ncbi::TEntrezId>(value))
1099 # define ENTREZ_ID_CONST(id) id
1100 #endif
1101 
1102 #define ZERO_ENTREZ_ID ENTREZ_ID_CONST(0)
1103 #define INVALID_ENTREZ_ID ENTREZ_ID_CONST(-1)
1104 
1105 #ifdef NCBI_STRICT_TAX_ID
1106 # define TAX_ID_TO(T, tax_id) STRICT_ID_TO(ncbi::TTaxId, T, tax_id)
1107 # define TAX_ID_FROM(T, value) STRICT_ID_FROM(ncbi::TTaxId, T, value)
1108 # define TAX_ID_CONST(id) STRICT_ID_CONST(ncbi::TTaxId, id)
1109 #else
1110 # define TAX_ID_TO(T, tax_id) (static_cast<T>(tax_id))
1111 # define TAX_ID_FROM(T, value) (static_cast<ncbi::TTaxId>(value))
1112 # define TAX_ID_CONST(id) id
1113 #endif
1114 
1115 #define ZERO_TAX_ID TAX_ID_CONST(0)
1116 #define INVALID_TAX_ID TAX_ID_CONST(-1)
1117 
1118 
1119 /// Convert gi-compatible int to/from other types.
1120 #define INT_ID_TO(T, id) (static_cast<T>(id))
1121 #define INT_ID_FROM(T, value) (static_cast<ncbi::TIntId>(value))
1122 
1123 
1124 /// Helper address class
1126 {
1127 public:
1128  /// add offset to object reference (to get object's member)
1129  static void* Add(void* object, ssize_t offset);
1130  static const void* Add(const void* object, ssize_t offset);
1131  /// calculate offset inside object
1132  static ssize_t Sub(const void* first, const void* second);
1133 };
1134 
1135 
1136 inline
1137 void* CRawPointer::Add(void* object, ssize_t offset)
1138 {
1139  return static_cast<char*> (object) + offset;
1140 }
1141 
1142 inline
1143 const void* CRawPointer::Add(const void* object, ssize_t offset)
1144 {
1145  return static_cast<const char*> (object) + offset;
1146 }
1147 
1148 inline
1149 ssize_t CRawPointer::Sub(const void* first, const void* second)
1150 {
1151  return (ssize_t)/*ptrdiff_t*/
1152  (static_cast<const char*> (first) - static_cast<const char*> (second));
1153 }
1154 
1155 
1156 
1157 /// Buffer with an embedded pre-reserved space.
1158 ///
1159 /// It is convenient to use if you want to avoid allocation in heap
1160 /// for smaller requested sizes, and use stack for such cases.
1161 /// @example:
1162 /// CFastBuffer<2048> buf(some_size);
1163 
1164 template <size_t KEmbeddedSize, class TType = char>
1166 {
1167 public:
1168  CFastBuffer(size_t buf_size)
1169  : m_Size(buf_size),
1170  m_Buffer(buf_size <= KEmbeddedSize ? m_EmbeddedBuffer : new TType[buf_size])
1171  {}
1173 
1174  TType operator[] (size_t pos) const { return m_Buffer[pos]; }
1175 
1176  TType& operator* (void) { return *m_Buffer; }
1177  const TType& operator* (void) const { return *m_Buffer; }
1178 
1179  TType* operator+ (size_t offset) { return m_Buffer + offset; }
1180  const TType* operator+ (size_t offset) const { return m_Buffer + offset; }
1181 
1182  TType* begin(void) { return m_Buffer; }
1183  const TType* begin(void) const { return m_Buffer; }
1184 
1185  TType* end(void) { return m_Buffer + m_Size; }
1186  const TType* end(void) const { return m_Buffer + m_Size; }
1187 
1188  size_t size(void) const { return m_Size; }
1189 
1190 private:
1191  size_t m_Size;
1192  TType* m_Buffer;
1193  TType m_EmbeddedBuffer[KEmbeddedSize];
1194 };
1195 
1196 
1197 
1198 /// Macro used to mark a constructor as deprecated.
1199 ///
1200 /// The correct syntax for this varies from compiler to compiler:
1201 /// older versions of GCC (prior to 3.4) require NCBI_DEPRECATED to
1202 /// follow any relevant constructor declarations, but some other
1203 /// compilers (Microsoft Visual Studio 2005, IBM Visual Age / XL)
1204 /// require it to precede any relevant declarations, whether or not
1205 /// they are for constructors.
1206 #if defined(NCBI_COMPILER_MSVC) || defined(NCBI_COMPILER_VISUALAGE)
1207 # define NCBI_DEPRECATED_CTOR(decl) NCBI_DEPRECATED decl
1208 #else
1209 # define NCBI_DEPRECATED_CTOR(decl) decl NCBI_DEPRECATED
1210 #endif
1211 
1212 /// Macro used to mark a class as deprecated.
1213 ///
1214 /// @sa NCBI_DEPRECATED_CTOR
1215 #define NCBI_DEPRECATED_CLASS NCBI_DEPRECATED_CTOR(class)
1216 
1217 //#define NCBI_ENABLE_SAFE_FLAGS
1218 #ifdef NCBI_ENABLE_SAFE_FLAGS
1219 /////////////////////////////////////////////////////////////////////////////
1220 /// Support for safe enum flags
1221 /////////////////////////////////////////////////////////////////////////////
1222 ///
1223 /// The CSafeFlags enum is used to define flags with enum definition
1224 /// so that they preserve type after bit-wise operations,
1225 /// and doesn't allow to be used in context of another enum/flags/int type.
1226 /// With the definition of enum, usually inside class that uses it:
1227 /// class CMyClass {
1228 // public:
1229 /// enum EFlags {
1230 /// fFlag1 = 1<<0,
1231 /// fFlag2 = 1<<1,
1232 /// fFlag3 = 1<<1,
1233 /// fMask = fFlag1|fFlag2
1234 /// };
1235 /// you can add at the same level a macro declaration:
1236 /// DECLARE_SAFE_FLAGS_TYPE(EFlags, TFlags);
1237 /// };
1238 /// and then outside the class, or at the same level if it's not a class definition:
1239 /// DECLARE_SAFE_FLAGS(CMyClass::EFlags);
1240 /// The first macro DECLARE_SAFE_FLAGS_TYPE() declares a typedef for safe-flags.
1241 /// The second macro DECLARE_SAFE_FLAGS() marks the enum as a safe-flags enum, so that
1242 /// bit-wise operations on its values will preserve the safe-flags type.
1243 /// No other modification is necessary in code that uses enum flags correctly.
1244 /// In case the enum values are used in a wrong context, like with a different enum type,
1245 /// compilation error will occur.
1246 
1247 /// Safe enum flags template, not to be used explicitly.
1248 /// Use macros DECLARE_SAFE_FLAGS_TYPE and DECLARE_SAFE_FLAGS instead.
1249 template<class Enum>
1250 class CSafeFlags {
1251 public:
1252  typedef Enum enum_type;
1254 
1256  : m_Flags(0)
1257  {
1258  }
1259  explicit
1261  : m_Flags(flags)
1262  {
1263  }
1265  : m_Flags(static_cast<storage_type>(flags))
1266  {
1267  }
1268 
1270  {
1271  return m_Flags;
1272  }
1273 
1275 
1276  bool operator==(const CSafeFlags& b) const
1277  {
1278  return get() == b.get();
1279  }
1280  bool operator!=(const CSafeFlags& b) const
1281  {
1282  return get() != b.get();
1283  }
1284  // the following operators are necessary to allow comparison with 0
1285  bool operator==(int v) const
1286  {
1287  return get() == storage_type(v);
1288  }
1289  bool operator!=(int v) const
1290  {
1291  return get() != storage_type(v);
1292  }
1293 
1295  {
1296  return CSafeFlags(~get());
1297  }
1298 
1300  {
1301  return CSafeFlags(get() & b.get());
1302  }
1304  {
1305  m_Flags &= b.get();
1306  return *this;
1307  }
1308 
1310  {
1311  return CSafeFlags(get() | b.get());
1312  }
1314  {
1315  m_Flags |= b.get();
1316  return *this;
1317  }
1318 
1320  {
1321  return CSafeFlags(get() ^ b.get());
1322  }
1324  {
1325  m_Flags ^= b.get();
1326  return *this;
1327  }
1328 
1329 private:
1331 };
1332 /// Macro DECLARE_SAFE_FLAGS_TYPE defines a typedef for safe-flags.
1333 /// First argument is enum name, second argument is the new typedef name.
1334 /// In place of old typedef:
1335 /// typedef int TFlags;
1336 /// put new macro:
1337 /// DECLARE_SAFE_FLAGS_TYPE(EFlags, TFlags);
1338 #define DECLARE_SAFE_FLAGS_TYPE(E, T) \
1339  typedef NCBI_NS_NCBI::CSafeFlags<E> T
1340 
1341 /// Macro DECLARE_SAFE_FLAGS marks a enum as safe-flags enum.
1342 /// The argument is the enum name.
1343 /// If the enum is defined inside a class then DECLARE_SAFE_FLAGS()
1344 /// must be placed outside the class definition:
1345 /// DECLARE_SAFE_FLAGS(CMyClass::EFlags);
1346 #define DECLARE_SAFE_FLAGS(E) \
1347 inline NCBI_NS_NCBI::CSafeFlags<E> operator|(E a, E b) \
1348 { return NCBI_NS_NCBI::CSafeFlags<E>(a) | b; } \
1349 inline NCBI_NS_NCBI::CSafeFlags<E> operator&(E a, E b) \
1350 { return NCBI_NS_NCBI::CSafeFlags<E>(a) & b; } \
1351 inline NCBI_NS_NCBI::CSafeFlags<E> operator^(E a, E b) \
1352 { return NCBI_NS_NCBI::CSafeFlags<E>(a) ^ b; } \
1353 inline NCBI_NS_NCBI::CSafeFlags<E> operator~(E a) \
1354 { return ~NCBI_NS_NCBI::CSafeFlags<E>(a); }
1355 
1356 /// Helper operators for safe-flags enums.
1357 /// These operators will be used only for enums marked
1358 /// as safe-flag enums by macro DECLARE_SAFE_FLAGS()
1359 template<class E> inline CSafeFlags<E> operator|(E a, CSafeFlags<E> b)
1360 {
1361  return b | a;
1362 }
1363 template<class E> inline CSafeFlags<E> operator&(E a, CSafeFlags<E> b)
1364 {
1365  return b & a;
1366 }
1367 template<class E> inline CSafeFlags<E> operator^(E a, CSafeFlags<E> b)
1368 {
1369  return b ^ a;
1370 }
1371 template<class E> inline
1372 ostream& operator<<(ostream& out, const CSafeFlags<E>& v)
1373 {
1374  return out << v.get();
1375 }
1376 
1377 #else // NCBI_ENABLE_SAFE_FLAGS
1378 // backup implementation of safe flag macros
1379 # define DECLARE_SAFE_FLAGS_TYPE(Enum,Typedef) typedef underlying_type<Enum>::type Typedef
1380 # define DECLARE_SAFE_FLAGS(Enum) NCBI_EAT_SEMICOLON(safe_flags)
1381 #endif // NCBI_ENABLE_SAFE_FLAGS
1382 
1383 
1384 /// CNcbiActionGuard class
1385 /// Executes registered callbacks on request or on destruction.
1387 {
1388 public:
1390  virtual ~CNcbiActionGuard(void) { ExecuteActions(); }
1391 
1392  template<class TFunc> void AddAction(TFunc func)
1393  {
1394  m_Actions.push_back(unique_ptr<CAction_Base>(new CAction<TFunc>(func)));
1395  }
1396 
1397  void ExecuteActions(void)
1398  {
1399  ITERATE(TActions, it, m_Actions) {
1400  try {
1401  (*it)->Execute();
1402  }
1403  catch (exception& e) {
1404  ERR_POST("Error executing action: " << e.what());
1405  }
1406  }
1407  m_Actions.clear(); // prevent multiple executions
1408  }
1409 
1410 private:
1413 
1414  // Action wrapper
1416  {
1417  public:
1418  CAction_Base(void) {}
1419  virtual ~CAction_Base() {}
1420  virtual void Execute(void) const = 0;
1421  private:
1424  };
1425 
1426  template<class TFunc> class CAction : public CAction_Base
1427  {
1428  public:
1429  CAction(TFunc func) : m_Func(func) {}
1430  virtual ~CAction(void) {}
1431  void Execute(void) const override { m_Func(); }
1432  private:
1434  };
1435  typedef list<unique_ptr<CAction_Base> > TActions;
1436 
1438 };
1439 
1440 /// Template helpers for unique_ptr to work with resource allocating/deallocating C functions.
1441 /// @{
1442 /// Eliminates the necessity for specifying type of deallocating C function.
1443 template <class T>
1444 using c_unique_ptr = unique_ptr<T, void(*)(T*)>;
1445 
1446 /// Eliminates the necessity for specifying types of both allocated resource and deallocating C function.
1447 template <class T, typename TDeleter>
1448 unique_ptr<T, TDeleter> make_c_unique(T* p, TDeleter d)
1449 {
1450  return {p, d};
1451 }
1452 
1453 /// Overload for the above for all types of allocated memory that are to be deallocated by free()
1454 template <class T>
1455 unique_ptr<T, void(*)(void*)> make_c_unique(T* p)
1456 {
1457  return make_c_unique(p, &free);
1458 }
1459 /// @}
1460 
1461 
1462 /// Helpers for exporting stand-alone functions from DLLs without changing the functions.
1463 /// Useful for exporting third-party APIs without a need to adjust third-party headers.
1464 /// @example:
1465 /// dll_header.hpp: NCBI_EXPORT_FUNC_DECLARE(XCONNECT, mbedtls_strerror);
1466 /// dll_source.cpp: NCBI_EXPORT_FUNC_DEFINE(XCONNECT, mbedtls_strerror);
1467 /// app.cpp: NCBI_XCONNECT::mbedtls_strerror(err, buf, sizeof(buf));
1468 /// @{
1469 #define NCBI_EXPORT_FUNC_DECLARE(lib, func) \
1470 namespace NCBI_ ## lib \
1471 { \
1472  namespace S ## func \
1473  { \
1474  using T = decltype(func); \
1475  \
1476  NCBI_ ## lib ## _EXPORT T* F(); \
1477  \
1478  template <class T> \
1479  struct S; \
1480  \
1481  template <class TR, class... TArgs> \
1482  struct S<TR (TArgs...)> \
1483  { \
1484  static TR Call(TArgs... args) { return F()(args...); } \
1485  }; \
1486  } \
1487  \
1488  constexpr auto func = S ## func::S<S ## func::T>::Call; \
1489 }
1490 
1491 #define NCBI_EXPORT_FUNC_DEFINE(lib, func) \
1492 namespace NCBI_ ## lib \
1493 { \
1494  namespace S ## func \
1495  { \
1496  T* F() { return ::func; } \
1497  } \
1498 }
1499 /// @}
1500 
1501 
1503 
1505 
1506 template<class T1, class T2>
1507 inline
1508 void swap(NCBI_NS_NCBI::pair_base_member<T1,T2>& pair1,
1509  NCBI_NS_NCBI::pair_base_member<T1,T2>& pair2)
1510 {
1511  pair1.Swap(pair2);
1512 }
1513 
1514 
1515 template<class P, class D>
1516 inline
1517 void swap(NCBI_NS_NCBI::AutoPtr<P,D>& ptr1,
1518  NCBI_NS_NCBI::AutoPtr<P,D>& ptr2)
1519 {
1520  ptr1.Swap(ptr2);
1521 }
1522 
1523 
1524 #if defined(NCBI_COMPILER_WORKSHOP) || defined(NCBI_COMPILER_MIPSPRO)
1525 
1526 #define ArraySize(array) (sizeof(array)/sizeof((array)[0]))
1527 
1528 #else
1529 
1530 template<class Element, size_t Size>
1531 inline
1532 constexpr size_t ArraySize(const Element (&)[Size])
1533 {
1534  return Size;
1535 }
1536 
1537 #ifdef NCBI_STRICT_GI
1538 template <class TKey, class TStorage> struct hash<ncbi::CStrictId<TKey, TStorage>>
1539 {
1540  size_t operator()(const ncbi::CStrictId<TKey, TStorage> & x) const
1541  {
1542  return hash<TStorage>()(x.Get());
1543  }
1544 };
1545 #endif /* NCBI_STRICT_GI */
1546 
1547 #endif
1548 
1550 
1551 /* @} */
1552 
1553 #endif /* CORELIB___NCBIMISC__HPP */
#define true
Definition: bool.h:35
#define false
Definition: bool.h:36
#define bool
Definition: bool.h:34
AutoArray –.
Definition: ncbimisc.hpp:527
AutoPtr –.
Definition: ncbimisc.hpp:401
Template used to replace bool type arguments with some strict equivalent.
Definition: ncbimisc.hpp:310
a helper template to enforce constness of argument to GI_CONST macro
Definition: ncbimisc.hpp:1054
CNcbiActionGuard class Executes registered callbacks on request or on destruction.
Definition: ncbimisc.hpp:1387
Template class allowing to store a value or null (unassigned) state.
Definition: ncbimisc.hpp:676
Helper address class.
Definition: ncbimisc.hpp:1126
Support for safe enum flags.
Definition: ncbimisc.hpp:1250
Template class for strict ID types.
Definition: ncbimisc.hpp:893
definition of a Culling tree
Definition: ncbi_tree.hpp:100
Template used for empty base class optimization.
Definition: ncbimisc.hpp:246
char value[7]
Definition: config.c:431
static uch flags
#define T(s)
Definition: common.h:230
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
std::ofstream out("events_result.xml")
main entry point for tests
static int type
Definition: getdata.c:31
element_type * m_Ptr
Internal pointer representation.
Definition: ncbimisc.hpp:500
TValue operator()(void) const
Definition: ncbimisc.hpp:658
virtual ~CNcbiActionGuard(void)
Definition: ncbimisc.hpp:1390
SStrictId_Entrez::TId TEntrezId
TEntrezId type for entrez ids which require the same strictness as TGi.
Definition: ncbimisc.hpp:1041
CNcbiActionGuard(void)
Definition: ncbimisc.hpp:1389
const Type & s_ITERATE_ConstRef(const Type &obj)
Definition: ncbimisc.hpp:787
NCBI_XNCBI_EXPORT void g_ThrowOnNull(void)
Definition: ncbiexpt.cpp:861
Del deleter_type
Alias for template argument.
Definition: ncbimisc.hpp:404
CBoolEnum(TEnum value)
Definition: ncbimisc.hpp:314
BEGIN_STD_NAMESPACE
Definition: ncbimisc.hpp:1504
#define DECLARE_OPERATOR_BOOL(Expr)
Declaration of safe bool operator from boolean expression.
Definition: ncbimisc.hpp:199
CStrictId(void)
Definition: ncbimisc.hpp:898
~AutoPtr(void)
Destructor.
Definition: ncbimisc.hpp:434
CNcbiActionGuard & operator=(const CNcbiActionGuard &)
ETriState
Enumeration to represent a tristate value.
Definition: ncbimisc.hpp:128
CSafeFlags operator~() const
Definition: ncbimisc.hpp:1294
AutoPtr< X, Del > & operator=(const AutoPtr< X, Del > &p)
Assignment operator.
Definition: ncbimisc.hpp:440
CNcbiOstream & operator<<(CNcbiOstream &out, const CStrictId< TKey, TStorage > &id)
Definition: ncbimisc.hpp:946
CBoolEnum(bool value)
Definition: ncbimisc.hpp:313
void AddAction(TFunc func)
Definition: ncbimisc.hpp:1392
bool IsOwned(void) const
Definition: ncbimisc.hpp:497
element_type & operator[](size_t pos)
Array style dereference (returns reference)
Definition: ncbimisc.hpp:594
CAction_Base & operator=(const CAction_Base &)
X element_type
Define element type.
Definition: ncbimisc.hpp:529
CSafeFlags & operator|=(const CSafeFlags &b)
Definition: ncbimisc.hpp:1313
~AutoArray(void)
Definition: ncbimisc.hpp:554
enum ENcbiSwitch ESwitch
Aux.
CStrictId(T id, typename enable_if< is_same< T, TId >::value, T >::type=0)
Definition: ncbimisc.hpp:927
element_type * x_Release(void) const
Release for const object.
Definition: ncbimisc.hpp:504
Uint8 TUintId
Definition: ncbimisc.hpp:1000
void reset(element_type *p=0, EOwnership ownership=eTakeOwnership)
Reset will delete the old pointer (if owned), set content to the new value, and assume the ownership ...
Definition: ncbimisc.hpp:480
void ExecuteActions(void)
Definition: ncbimisc.hpp:1397
bool operator!=(const CNullable< TValue, TNullToValue > &l, const CNullable< TValue, TNullToValue > &r)
Definition: ncbimisc.hpp:747
char * strdup(const char *str)
Supply string duplicate function, if one is not defined.
Definition: ncbistr.cpp:5598
AutoPtr(const AutoPtr< X, Del > &p)
Copy constructor.
Definition: ncbimisc.hpp:427
storage_type get() const
Definition: ncbimisc.hpp:1269
TType * begin(void)
Definition: ncbimisc.hpp:1182
bool operator==(const CSafeFlags &b) const
Definition: ncbimisc.hpp:1276
CBoolEnum(int)
CSafeFlags operator&(const CSafeFlags &b) const
Definition: ncbimisc.hpp:1299
pair_base_member(const first_type &first_value, const second_type &second_value)
Definition: ncbimisc.hpp:263
ERetriable
Can the action be retried?
Definition: ncbimisc.hpp:167
void Swap(AutoPtr< X, Del > &a)
Definition: ncbimisc.hpp:491
CAction_Base(const CAction_Base &)
void Swap(pair_base_member< first_type, second_type > &p)
Definition: ncbimisc.hpp:287
pair_base_member< deleter_type, bool > m_Data
State info.
Definition: ncbimisc.hpp:501
AutoArray< X, Del > & operator=(element_type *p)
Assignment operator.
Definition: ncbimisc.hpp:571
CSafeFlags< E > operator|(E a, CSafeFlags< E > b)
Helper operators for safe-flags enums.
Definition: ncbimisc.hpp:1359
Member second_type
Definition: ncbimisc.hpp:251
bool operator!=(const CSafeFlags &b) const
Definition: ncbimisc.hpp:1280
bool operator<(const TThis &id) const
Definition: ncbimisc.hpp:904
Member member_type
Definition: ncbimisc.hpp:250
element_type & operator*(void) const
Dereference operator.
Definition: ncbimisc.hpp:463
size_t size(void) const
Definition: ncbimisc.hpp:1188
CSafeFlags operator|(const CSafeFlags &b) const
Definition: ncbimisc.hpp:1309
AutoArray< X, Del > & operator=(const AutoArray< X, Del > &p)
Assignment operator.
Definition: ncbimisc.hpp:560
CBoolEnum(char *)
TThis operator-(const TThis &id) const
Definition: ncbimisc.hpp:915
static ssize_t Sub(const void *first, const void *second)
calculate offset inside object
Definition: ncbimisc.hpp:1149
bool m_Value
Definition: ncbimisc.hpp:322
const first_type & first() const
Definition: ncbimisc.hpp:269
bool IsNull(void) const
Check if the object is unassigned.
Definition: ncbimisc.hpp:686
element_type * operator->(void) const
Reference operator.
Definition: ncbimisc.hpp:466
CFastBuffer(size_t buf_size)
Definition: ncbimisc.hpp:1168
bool m_IsNull
Definition: ncbimisc.hpp:736
TGi CStrictGi
Definition: ncbimisc.hpp:1028
CNcbiActionGuard(const CNcbiActionGuard &)
size_t m_Size
Definition: ncbimisc.hpp:1191
member_type m_Member
Definition: ncbimisc.hpp:298
END_STD_NAMESPACE
Definition: ncbimisc.hpp:1549
void Execute(void) const override
Definition: ncbimisc.hpp:1431
X element_type
Define element type.
Definition: ncbimisc.hpp:403
ERound
Whether to truncate/round a value.
Definition: ncbimisc.hpp:136
TThis & operator=(const TThis &other)
Definition: ncbimisc.hpp:900
static void * Add(void *object, ssize_t offset)
add offset to object reference (to get object's member)
Definition: ncbimisc.hpp:1137
element_type * x_Release(void) const
Release for const object.
Definition: ncbimisc.hpp:617
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
void Swap(AutoPtr< X, Del > &a)
Definition: ncbimisc.hpp:609
storage_type m_Flags
Definition: ncbimisc.hpp:1330
bool operator==(const TThis &id) const
Definition: ncbimisc.hpp:902
bool operator<=(const TThis &id) const
Definition: ncbimisc.hpp:905
CSafeFlags operator^(const CSafeFlags &b) const
Definition: ncbimisc.hpp:1319
constexpr size_t ArraySize(const Element(&)[Size])
Definition: ncbimisc.hpp:1532
CNullable(TValue value)
Initialize nullable with a specific value.
Definition: ncbimisc.hpp:682
unique_ptr< T, void(*)(T *)> c_unique_ptr
Template helpers for unique_ptr to work with resource allocating/deallocating C functions.
Definition: ncbimisc.hpp:1444
AutoArray(size_t size)
Construct the array using C++ new[] operator.
Definition: ncbimisc.hpp:536
TThis operator--(int)
Definition: ncbimisc.hpp:912
CStrictId< TKey, TStorage > TThis
Definition: ncbimisc.hpp:896
size_t operator()(const ncbi::CStrictId< TKey, TStorage > &x) const
Definition: ncbimisc.hpp:1540
TType m_EmbeddedBuffer[KEmbeddedSize]
Definition: ncbimisc.hpp:1193
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
const element_type & operator[](size_t pos) const
Array style dereference (returns value)
Definition: ncbimisc.hpp:591
ENormalizePath
Whether to normalize a path.
Definition: ncbimisc.hpp:150
pair_base_member< deleter_type, bool > m_Data
State info.
Definition: ncbimisc.hpp:624
bool s_ITERATE_SameObject(const Type &obj1, const Type &obj2)
Definition: ncbimisc.hpp:796
CSafeFlags & operator^=(const CSafeFlags &b)
Definition: ncbimisc.hpp:1323
CNullable(ENull=null)
Create an empty nullable.
Definition: ncbimisc.hpp:679
AutoArray(const AutoArray< X, Del > &p)
Definition: ncbimisc.hpp:548
underlying_type< enum_type >::type storage_type
Definition: ncbimisc.hpp:1253
bool operator>=(const TThis &id) const
Definition: ncbimisc.hpp:907
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:469
AutoArray(element_type *p, const deleter_type &deleter)
Definition: ncbimisc.hpp:544
static X * Create(void)
Default create function.
Definition: ncbimisc.hpp:342
AutoPtr(element_type *p=0)
Constructor.
Definition: ncbimisc.hpp:407
TActions m_Actions
Definition: ncbimisc.hpp:1437
bool operator==(const CNullable< TValue, TNullToValue > &l, const CNullable< TValue, TNullToValue > &r)
Definition: ncbimisc.hpp:741
enum ENcbiOwnership EOwnership
Ownership relations between objects.
TId & Set(void)
Definition: ncbimisc.hpp:923
const TType * begin(void) const
Definition: ncbimisc.hpp:1183
TValue & SetValue(void)
Get a non-const reference to the value.
Definition: ncbimisc.hpp:713
static void Delete(X *object)
Array delete function.
Definition: ncbimisc.hpp:360
Int8 TIntId
Definition: ncbimisc.hpp:999
EFollowLinks
Whether to follow symbolic links (also known as shortcuts or aliases)
Definition: ncbimisc.hpp:143
END_NCBI_NAMESPACE
Definition: ncbimisc.hpp:1502
TType operator[](size_t pos) const
Definition: ncbimisc.hpp:1174
CSafeFlags(enum_type flags)
Definition: ncbimisc.hpp:1264
CStrictId< SStrictId_Gi, SStrictId_Gi::TId > TGi
Definition: ncbimisc.hpp:1025
static void Delete(X *object)
Default delete function.
Definition: ncbimisc.hpp:351
TType * operator+(size_t offset)
Definition: ncbimisc.hpp:1179
unique_ptr< T, TDeleter > make_c_unique(T *p, TDeleter d)
Eliminates the necessity for specifying types of both allocated resource and deallocating C function.
Definition: ncbimisc.hpp:1448
CStrictId(const TThis &other)
Definition: ncbimisc.hpp:899
TThis operator+(const TThis &id) const
Definition: ncbimisc.hpp:914
SStrictId_Tax::TId TTaxId
Taxon id type.
Definition: ncbimisc.hpp:1048
Del deleter_type
Alias for template argument.
Definition: ncbimisc.hpp:530
AutoArray(element_type *p=0)
Definition: ncbimisc.hpp:540
EInterruptOnSignal
Interrupt on signal mode.
Definition: ncbimisc.hpp:160
CSafeFlags & operator&=(const CSafeFlags &b)
Definition: ncbimisc.hpp:1303
CNcbiIstream & operator>>(CNcbiIstream &in, CStrictId< TKey, TStorage > &id)
Definition: ncbimisc.hpp:953
AutoPtr< X, Del > & operator=(element_type *p)
Assignment operator.
Definition: ncbimisc.hpp:451
static void Delete(X *object)
C Language deallocation function.
Definition: ncbimisc.hpp:369
bool operator==(int v) const
Definition: ncbimisc.hpp:1285
BEGIN_NCBI_NAMESPACE
NCBI_EOWNERSHIP_DEFINED.
Definition: ncbimisc.hpp:109
TType & operator*(void)
Definition: ncbimisc.hpp:1176
CSafeFlags(storage_type flags)
Definition: ncbimisc.hpp:1260
#define DECLARE_OPERATOR_BOOL_PTR(Ptr)
Declaration of safe bool operator from pointer expression.
Definition: ncbimisc.hpp:206
AutoPtr(element_type *p, const deleter_type &deleter)
Constructor.
Definition: ncbimisc.hpp:412
CSafeFlags< E > operator&(E a, CSafeFlags< E > b)
Definition: ncbimisc.hpp:1363
TThis & operator++(void)
Definition: ncbimisc.hpp:909
TStorage TId
Definition: ncbimisc.hpp:895
CSafeFlags< E > operator^(E a, CSafeFlags< E > b)
Definition: ncbimisc.hpp:1367
AutoPtr(element_type *p, EOwnership ownership)
Constructor, own the pointed object if ownership == eTakeOwnership.
Definition: ncbimisc.hpp:417
const TSeqPos kInvalidSeqPos
Define special value for invalid sequence position.
Definition: ncbimisc.hpp:878
CNullable & operator=(TValue value)
Assign a value to the nullable.
Definition: ncbimisc.hpp:722
pair_base_member(void)
Definition: ncbimisc.hpp:253
ENullable
Whether a value is nullable.
Definition: ncbimisc.hpp:113
first_type & first()
Definition: ncbimisc.hpp:273
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:581
TThis operator-(void) const
Definition: ncbimisc.hpp:916
TThis & operator--(void)
Definition: ncbimisc.hpp:911
TThis operator++(int)
Definition: ncbimisc.hpp:910
TType * end(void)
Definition: ncbimisc.hpp:1185
second_type & second()
Definition: ncbimisc.hpp:282
list< unique_ptr< CAction_Base > > TActions
Definition: ncbimisc.hpp:1435
bool operator!=(int v) const
Definition: ncbimisc.hpp:1289
const second_type & second() const
Definition: ncbimisc.hpp:278
ENull
CNullable –.
Definition: ncbimisc.hpp:645
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
Enum enum_type
Definition: ncbimisc.hpp:1252
virtual void Execute(void) const =0
TId Get(void) const
Definition: ncbimisc.hpp:921
element_type * m_Ptr
Definition: ncbimisc.hpp:623
CBoolEnum(unsigned int)
element_type * release(void)
Release will release ownership of pointer to caller.
Definition: ncbimisc.hpp:472
ESign
Signedness of a value.
Definition: ncbimisc.hpp:120
element_type * release(void)
Release will release ownership of pointer to caller.
Definition: ncbimisc.hpp:584
TValue m_Value
Definition: ncbimisc.hpp:737
void Set(TId id)
Definition: ncbimisc.hpp:922
bool operator!=(const TThis &id) const
Definition: ncbimisc.hpp:903
void reset(element_type *p=0)
Reset will delete the old pointer, set content to the new value, and assume the ownership upon the ne...
Definition: ncbimisc.hpp:598
bool operator>(const TThis &id) const
Definition: ncbimisc.hpp:906
pair_base_member(const member_type &member_value)
Definition: ncbimisc.hpp:258
TType * m_Buffer
Definition: ncbimisc.hpp:1192
const TValue & GetValue(void) const
Get a const reference to the current value.
Definition: ncbimisc.hpp:703
const TType * end(void) const
Definition: ncbimisc.hpp:1186
AutoPtr(element_type *p, const deleter_type &deleter, EOwnership ownership)
Constructor, own the pointed object if ownership == eTakeOwnership.
Definition: ncbimisc.hpp:422
@ eTriState_False
The value is equivalent to false/no.
Definition: ncbimisc.hpp:130
@ eTriState_True
The value is equivalent to true/yes.
Definition: ncbimisc.hpp:131
@ eTriState_Unknown
The value is indeterminate.
Definition: ncbimisc.hpp:129
@ eRetriable_Unknown
It is unknown if the action can succeed if retried.
Definition: ncbimisc.hpp:169
@ eRetriable_No
It makes no sense to retry the action.
Definition: ncbimisc.hpp:168
@ eRetriable_Yes
It makes sense to try again.
Definition: ncbimisc.hpp:170
@ eTrunc
Value must be truncated.
Definition: ncbimisc.hpp:137
@ eRound
Value must be rounded.
Definition: ncbimisc.hpp:138
@ eNormalizePath
Normalize a path.
Definition: ncbimisc.hpp:151
@ eNotNormalizePath
Do not normalize a path.
Definition: ncbimisc.hpp:152
@ eOff
Definition: ncbimisc.hpp:71
@ eDefault
Definition: ncbimisc.hpp:73
@ eOn
Definition: ncbimisc.hpp:72
@ eIgnoreLinks
Do not follow symbolic links.
Definition: ncbimisc.hpp:144
@ eFollowLinks
Follow symbolic links.
Definition: ncbimisc.hpp:145
@ eRestartOnSignal
Restart operation if interrupted by a signal.
Definition: ncbimisc.hpp:162
@ eInterruptOnSignal
Cancel operation if interrupted by a signal.
Definition: ncbimisc.hpp:161
@ eNotNullable
Value cannot be null.
Definition: ncbimisc.hpp:115
@ eNullable
Value can be null.
Definition: ncbimisc.hpp:114
@ eNegative
Value is negative.
Definition: ncbimisc.hpp:121
@ ePositive
Value is positive.
Definition: ncbimisc.hpp:123
@ eZero
Value is zero.
Definition: ncbimisc.hpp:122
@ eTakeOwnership
No ownership assumed.
Definition: ncbimisc.hpp:101
@ eNoOwnership
Definition: ncbimisc.hpp:100
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define NCBI_DEPRECATED
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
enum ENcbiOwnership EOwnership
Ownership relations between objects.
ENcbiSwitch
Aux.
Definition: ncbi_types.h:109
ENcbiOwnership
Ownership relations between objects.
Definition: ncbi_types.h:134
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
const struct ncbi::grid::netcache::search::fields::SIZE size
Magic spell ;-) needed for some weird compilers... very empiric.
unsigned int a
Definition: ncbi_localip.c:102
int ssize_t
Definition: ncbiconf_msvc.h:92
NCBI C++ auxiliary debug macros.
const double E
std::istream & in(std::istream &in_, double &x_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
static char tmp[2048]
Definition: utf8.c:42
int offset
Definition: replacements.h:160
static const char * str(char *buf, int n)
Definition: stats.c:84
Functor template for deleting array of objects.
Definition: ncbimisc.hpp:358
Functor template for the C language deallocation function, free().
Definition: ncbimisc.hpp:367
Functor template for allocating object.
Definition: ncbimisc.hpp:340
Functor template for deleting object.
Definition: ncbimisc.hpp:349
Key structs must always be defined - they are used in aliasinfo.cpp.
Definition: ncbimisc.hpp:1012
Definition: _hash_fun.h:40
Definition: type.c:6
#define Type
void free(voidpf ptr)
Modified on Thu Mar 28 17:10:06 2024 by modify_doxy.py rev. 669887