📄 ncbimisc.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: ncbimisc.hpp,v $ * PRODUCTION Revision 1000.2 2004/02/12 21:45:03 gouriano * PRODUCTION PRODUCTION: UPGRADED [CORE_001] Dev-tree R1.71 * PRODUCTION * =========================================================================== */#ifndef CORELIB___NCBIMISC__HPP#define CORELIB___NCBIMISC__HPP/* $Id: ncbimisc.hpp,v 1000.2 2004/02/12 21:45:03 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Eugene Vasilchenko * * *//// @file ncbistd.hpp/// Miscellaneous common-use basic types and functionality#include <corelib/ncbistl.hpp>#ifdef NCBI_OS_UNIX# include <sys/types.h>#endif/** @addtogroup AppFramework * * @{ */BEGIN_NCBI_SCOPE/// Which type of ownership between objects.////// Can be used to specify ownership relationship between objects./// For example, specify if a CSocket object owns the underlying/// SOCK object. enum EOwnership { eNoOwnership, ///< No ownership relationship eTakeOwnership ///< An object can take ownership of another};/// Whether a value is nullable.enum ENullable{ eNullable, ///< Value can be null eNotNullable ///< Value cannot be null};#if defined(HAVE_NO_AUTO_PTR)/////////////////////////////////////////////////////////////////////////////////// auto_ptr --////// Define auto_ptr if needed.////// Replacement of STL's std::auto_ptr for compilers with poor "auto_ptr"/// implementation./// /// See C++ Toolkit documentation for limiatations and use of auto_ptr.template <class X>class auto_ptr{public: typedef X element_type; ///< Define element_type /// Explicit conversion to auto_ptr. explicit auto_ptr(X* p = 0) : m_Ptr(p) {} /// Copy constructor with implicit conversion. /// /// Note that the copy constructor parameter is not a const /// because it is modified -- ownership is transferred. auto_ptr(auto_ptr<X>& a) : m_Ptr(a.release()) {} /// Assignment operator. auto_ptr<X>& operator=(auto_ptr<X>& a) { if (this != &a) { if (m_Ptr && m_Ptr != a.m_Ptr) { delete m_Ptr; } m_Ptr = a.release(); } return *this; } /// Destructor. ~auto_ptr(void) { if ( m_Ptr ) delete m_Ptr; } /// Deference operator. X& operator*(void) const { return *m_Ptr; } /// Reference operator. X* operator->(void) const { return m_Ptr; } /// Equality operator. int operator==(const X* p) const { return (m_Ptr == p); } /// Get pointer value. X* get(void) const { return m_Ptr; } /// Release pointer. X* release(void) { X* x_Ptr = m_Ptr; m_Ptr = 0; return x_Ptr; } /// Reset pointer. void reset(X* p = 0) { if (m_Ptr != p) { delete m_Ptr; m_Ptr = p; } }private: X* m_Ptr; ///< Internal pointer implementation.};#endif /* HAVE_NO_AUTO_PTR *//// Functor template for allocating object.template<class X>struct Creater{ /// Default create function. static X* Create(void) { return new X; }};/// Functor tempate for deleting object.template<class X>struct Deleter{ /// Default delete function. static void Delete(X* object) { delete object; }};/// Functor template for deleting array of objects.template<class X>struct ArrayDeleter{ /// Array delete function. static void Delete(X* object) { delete[] object; }};/// Functor template for the C language deallocation function, free().template<class X>struct CDeleter{ /// C Language deallocation function. static void Delete(X* object) { free(object); }};/////////////////////////////////////////////////////////////////////////////////// AutoPtr --////// Define an "auto_ptr" like class that can be used inside STL containers.////// The Standard auto_ptr template from STL doesn't allow the auto_ptr to be/// put in STL containers (list, vector, map etc.). The reason for this is/// the absence of copy constructor and assignment operator./// We decided that it would be useful to have an analog of STL's auto_ptr/// without this restriction - AutoPtr.////// Due to nature of AutoPtr its copy constructor and assignment operator/// modify the state of the source AutoPtr object as it transfers ownership/// to the target AutoPtr object. Also, we added possibility to redefine the/// way pointer will be deleted: the second argument of template allows/// pointers from "malloc" in AutoPtr, or you can use "ArrayDeleter" (see/// above) to properly delete an array of objects using "delete[]" instead/// of "delete". By default, the internal pointer will be deleted by C++/// "delete" operator.////// @sa/// Deleter(), ArrayDeleter(), CDeleter()template< class X, class Del = Deleter<X> >class AutoPtr{public: typedef X element_type; ///< Define element type. /// Constructor. AutoPtr(X* p = 0) : m_Ptr(p), m_Owner(true) { } /// Copy constructor. AutoPtr(const AutoPtr<X, Del>& p) : m_Ptr(0), m_Owner(p.m_Owner) { m_Ptr = p.x_Release(); } /// Destructor. ~AutoPtr(void) { reset(); } /// Assignment operator. AutoPtr<X, Del>& operator=(const AutoPtr<X, Del>& p) { if (this != &p) { bool owner = p.m_Owner; reset(p.x_Release()); m_Owner = owner; } return *this; } /// Assignment operator. AutoPtr<X, Del>& operator=(X* p) { reset(p); return *this; } /// Bool operator for use in if() clause. operator bool(void) const { return m_Ptr != 0; } // Standard getters. /// Dereference operator. X& operator* (void) const { return *m_Ptr; } /// Reference operator. X* operator->(void) const { return m_Ptr; } /// Get pointer. X* get (void) const { return m_Ptr; } /// Release will release ownership of pointer to caller. X* release(void) { m_Owner = false; return m_Ptr; } /// Reset will delete old pointer, set content to new value, /// and accept ownership upon the new pointer. void reset(X* p = 0) { if (m_Ptr && m_Owner) { Del::Delete(release()); } m_Ptr = p; m_Owner = true; }private: X* m_Ptr; ///< Internal pointer representation. mutable bool m_Owner; /// Release for const object. X* x_Release(void) const { return const_cast<AutoPtr<X, Del>*>(this)->release(); }};// "min" and "max" templates//#if defined(HAVE_NO_MINMAX_TEMPLATE)# define NOMINMAX# ifdef min# undef min# endif# ifdef max# undef max# endif/// Min function template.template <class T>inlineconst T& min(const T& a, const T& b) { return b < a ? b : a;}/// Max function template.template <class T>inlineconst T& max(const T& a, const T& b) { return a < b ? b : a;}#endif /* HAVE_NO_MINMAX_TEMPLATE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -