Renesas / opencv-lib

Dependents:   RZ_A2M_Mbed_samples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cvstd.hpp Source File

cvstd.hpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
00016 // Third party copyrights are property of their respective owners.
00017 //
00018 // Redistribution and use in source and binary forms, with or without modification,
00019 // are permitted provided that the following conditions are met:
00020 //
00021 //   * Redistribution's of source code must retain the above copyright notice,
00022 //     this list of conditions and the following disclaimer.
00023 //
00024 //   * Redistribution's in binary form must reproduce the above copyright notice,
00025 //     this list of conditions and the following disclaimer in the documentation
00026 //     and/or other materials provided with the distribution.
00027 //
00028 //   * The name of the copyright holders may not be used to endorse or promote products
00029 //     derived from this software without specific prior written permission.
00030 //
00031 // This software is provided by the copyright holders and contributors "as is" and
00032 // any express or implied warranties, including, but not limited to, the implied
00033 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00034 // In no event shall the Intel Corporation or contributors be liable for any direct,
00035 // indirect, incidental, special, exemplary, or consequential damages
00036 // (including, but not limited to, procurement of substitute goods or services;
00037 // loss of use, data, or profits; or business interruption) however caused
00038 // and on any theory of liability, whether in contract, strict liability,
00039 // or tort (including negligence or otherwise) arising in any way out of
00040 // the use of this software, even if advised of the possibility of such damage.
00041 //
00042 //M*/
00043 
00044 #ifndef OPENCV_CORE_CVSTD_HPP
00045 #define OPENCV_CORE_CVSTD_HPP
00046 
00047 #ifndef __cplusplus
00048 #  error cvstd.hpp header must be compiled as C++
00049 #endif
00050 
00051 #include "opencv2/core/cvdef.h"
00052 
00053 #include <cstddef>
00054 #include <cstring>
00055 #include <cctype>
00056 
00057 #ifndef OPENCV_NOSTL
00058 #  include <string>
00059 #endif
00060 
00061 // import useful primitives from stl
00062 #ifndef OPENCV_NOSTL_TRANSITIONAL
00063 #  include <algorithm>
00064 #  include <utility>
00065 #  include <cstdlib> //for abs(int)
00066 #  include <cmath>
00067 
00068 namespace cv
00069 {
00070     static inline uchar abs(uchar a) { return a; }
00071     static inline ushort abs(ushort a) { return a; }
00072     static inline unsigned abs(unsigned a) { return a; }
00073     static inline uint64 abs(uint64 a) { return a; }
00074 
00075     using std::min;
00076     using std::max;
00077     using std::abs;
00078     using std::swap;
00079     using std::sqrt;
00080     using std::exp;
00081     using std::pow;
00082     using std::log;
00083 }
00084 
00085 #else
00086 namespace cv
00087 {
00088     template<typename T> static inline T min(T a, T b) { return a < b ? a : b; }
00089     template<typename T> static inline T max(T a, T b) { return a > b ? a : b; }
00090     template<typename T> static inline T abs(T a) { return a < 0 ? -a : a; }
00091     template<typename T> static inline void swap(T& a, T& b) { T tmp = a; a = b; b = tmp; }
00092 
00093     template<> inline uchar abs(uchar a) { return a; }
00094     template<> inline ushort abs(ushort a) { return a; }
00095     template<> inline unsigned abs(unsigned a) { return a; }
00096     template<> inline uint64 abs(uint64 a) { return a; }
00097 }
00098 #endif
00099 
00100 namespace cv {
00101 
00102 //! @addtogroup core_utils
00103 //! @{
00104 
00105 //////////////////////////// memory management functions ////////////////////////////
00106 
00107 /** @brief Allocates an aligned memory buffer.
00108 
00109 The function allocates the buffer of the specified size and returns it. When the buffer size is 16
00110 bytes or more, the returned buffer is aligned to 16 bytes.
00111 @param bufSize Allocated buffer size.
00112  */
00113 CV_EXPORTS void* fastMalloc(size_t bufSize);
00114 
00115 /** @brief Deallocates a memory buffer.
00116 
00117 The function deallocates the buffer allocated with fastMalloc . If NULL pointer is passed, the
00118 function does nothing. C version of the function clears the pointer *pptr* to avoid problems with
00119 double memory deallocation.
00120 @param ptr Pointer to the allocated buffer.
00121  */
00122 CV_EXPORTS void fastFree(void* ptr);
00123 
00124 /*!
00125   The STL-compilant memory Allocator based on cv::fastMalloc() and cv::fastFree()
00126 */
00127 template<typename _Tp> class Allocator 
00128 {
00129 public:
00130     typedef _Tp value_type;
00131     typedef value_type* pointer;
00132     typedef const value_type* const_pointer;
00133     typedef value_type& reference;
00134     typedef const value_type& const_reference;
00135     typedef size_t size_type;
00136     typedef ptrdiff_t difference_type;
00137     template<typename U> class rebind { typedef Allocator<U>  other; };
00138 
00139     explicit Allocator () {}
00140     ~Allocator () {}
00141     explicit Allocator (Allocator  const&) {}
00142     template<typename U>
00143     explicit Allocator (Allocator<U>  const&) {}
00144 
00145     // address
00146     pointer address(reference r) { return &r; }
00147     const_pointer address(const_reference r) { return &r; }
00148 
00149     pointer allocate(size_type count, const void* =0) { return reinterpret_cast<pointer>(fastMalloc(count * sizeof (_Tp))); }
00150     void deallocate(pointer p, size_type) { fastFree(p); }
00151 
00152     void construct(pointer p, const _Tp& v) { new(static_cast<void*>(p)) _Tp(v); }
00153     void destroy(pointer p) { p->~_Tp(); }
00154 
00155     size_type max_size() const { return cv::max(static_cast<_Tp>(-1)/sizeof(_Tp), 1); }
00156 };
00157 
00158 //! @} core_utils
00159 
00160 //! @cond IGNORED
00161 
00162 namespace detail
00163 {
00164 
00165 // Metafunction to avoid taking a reference to void.
00166 template<typename T>
00167 struct RefOrVoid { typedef T& type; };
00168 
00169 template<>
00170 struct RefOrVoid<void>{ typedef void type; };
00171 
00172 template<>
00173 struct RefOrVoid<const void>{ typedef const void type; };
00174 
00175 template<>
00176 struct RefOrVoid<volatile void>{ typedef volatile void type; };
00177 
00178 template<>
00179 struct RefOrVoid<const volatile void>{ typedef const volatile void type; };
00180 
00181 // This class would be private to Ptr, if it didn't have to be a non-template.
00182 struct PtrOwner;
00183 
00184 }
00185 
00186 template<typename Y>
00187 struct DefaultDeleter
00188 {
00189     void operator () (Y* p) const;
00190 };
00191 
00192 //! @endcond
00193 
00194 //! @addtogroup core_basic
00195 //! @{
00196 
00197 /** @brief Template class for smart pointers with shared ownership
00198 
00199 A Ptr<T> pretends to be a pointer to an object of type T. Unlike an ordinary pointer, however, the
00200 object will be automatically cleaned up once all Ptr instances pointing to it are destroyed.
00201 
00202 Ptr is similar to boost::shared_ptr that is part of the Boost library
00203 (<http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm>) and std::shared_ptr from
00204 the [C++11](http://en.wikipedia.org/wiki/C++11) standard.
00205 
00206 This class provides the following advantages:
00207 -   Default constructor, copy constructor, and assignment operator for an arbitrary C++ class or C
00208     structure. For some objects, like files, windows, mutexes, sockets, and others, a copy
00209     constructor or an assignment operator are difficult to define. For some other objects, like
00210     complex classifiers in OpenCV, copy constructors are absent and not easy to implement. Finally,
00211     some of complex OpenCV and your own data structures may be written in C. However, copy
00212     constructors and default constructors can simplify programming a lot. Besides, they are often
00213     required (for example, by STL containers). By using a Ptr to such an object instead of the
00214     object itself, you automatically get all of the necessary constructors and the assignment
00215     operator.
00216 -   *O(1)* complexity of the above-mentioned operations. While some structures, like std::vector,
00217     provide a copy constructor and an assignment operator, the operations may take a considerable
00218     amount of time if the data structures are large. But if the structures are put into a Ptr, the
00219     overhead is small and independent of the data size.
00220 -   Automatic and customizable cleanup, even for C structures. See the example below with FILE\*.
00221 -   Heterogeneous collections of objects. The standard STL and most other C++ and OpenCV containers
00222     can store only objects of the same type and the same size. The classical solution to store
00223     objects of different types in the same container is to store pointers to the base class (Base\*)
00224     instead but then you lose the automatic memory management. Again, by using Ptr<Base> instead
00225     of raw pointers, you can solve the problem.
00226 
00227 A Ptr is said to *own* a pointer - that is, for each Ptr there is a pointer that will be deleted
00228 once all Ptr instances that own it are destroyed. The owned pointer may be null, in which case
00229 nothing is deleted. Each Ptr also *stores* a pointer. The stored pointer is the pointer the Ptr
00230 pretends to be; that is, the one you get when you use Ptr::get or the conversion to T\*. It's
00231 usually the same as the owned pointer, but if you use casts or the general shared-ownership
00232 constructor, the two may diverge: the Ptr will still own the original pointer, but will itself point
00233 to something else.
00234 
00235 The owned pointer is treated as a black box. The only thing Ptr needs to know about it is how to
00236 delete it. This knowledge is encapsulated in the *deleter* - an auxiliary object that is associated
00237 with the owned pointer and shared between all Ptr instances that own it. The default deleter is an
00238 instance of DefaultDeleter, which uses the standard C++ delete operator; as such it will work with
00239 any pointer allocated with the standard new operator.
00240 
00241 However, if the pointer must be deleted in a different way, you must specify a custom deleter upon
00242 Ptr construction. A deleter is simply a callable object that accepts the pointer as its sole
00243 argument. For example, if you want to wrap FILE, you may do so as follows:
00244 @code
00245     Ptr<FILE> f(fopen("myfile.txt", "w"), fclose);
00246     if(!f) throw ...;
00247     fprintf(f, ....);
00248     ...
00249     // the file will be closed automatically by f's destructor.
00250 @endcode
00251 Alternatively, if you want all pointers of a particular type to be deleted the same way, you can
00252 specialize DefaultDeleter<T>::operator() for that type, like this:
00253 @code
00254     namespace cv {
00255     template<> void DefaultDeleter<FILE>::operator ()(FILE * obj) const
00256     {
00257         fclose(obj);
00258     }
00259     }
00260 @endcode
00261 For convenience, the following types from the OpenCV C API already have such a specialization that
00262 calls the appropriate release function:
00263 -   CvCapture
00264 -   CvFileStorage
00265 -   CvHaarClassifierCascade
00266 -   CvMat
00267 -   CvMatND
00268 -   CvMemStorage
00269 -   CvSparseMat
00270 -   CvVideoWriter
00271 -   IplImage
00272 @note The shared ownership mechanism is implemented with reference counting. As such, cyclic
00273 ownership (e.g. when object a contains a Ptr to object b, which contains a Ptr to object a) will
00274 lead to all involved objects never being cleaned up. Avoid such situations.
00275 @note It is safe to concurrently read (but not write) a Ptr instance from multiple threads and
00276 therefore it is normally safe to use it in multi-threaded applications. The same is true for Mat and
00277 other C++ OpenCV classes that use internal reference counts.
00278 */
00279 template<typename T>
00280 struct Ptr
00281 {
00282     /** Generic programming support. */
00283     typedef T element_type;
00284 
00285     /** The default constructor creates a null Ptr - one that owns and stores a null pointer.
00286     */
00287     Ptr();
00288 
00289     /**
00290     If p is null, these are equivalent to the default constructor.
00291     Otherwise, these constructors assume ownership of p - that is, the created Ptr owns and stores p
00292     and assumes it is the sole owner of it. Don't use them if p is already owned by another Ptr, or
00293     else p will get deleted twice.
00294     With the first constructor, DefaultDeleter<Y>() becomes the associated deleter (so p will
00295     eventually be deleted with the standard delete operator). Y must be a complete type at the point
00296     of invocation.
00297     With the second constructor, d becomes the associated deleter.
00298     Y\* must be convertible to T\*.
00299     @param p Pointer to own.
00300     @note It is often easier to use makePtr instead.
00301      */
00302     template<typename Y>
00303 #ifdef DISABLE_OPENCV_24_COMPATIBILITY
00304     explicit
00305 #endif
00306     Ptr(Y* p);
00307 
00308     /** @overload
00309     @param d Deleter to use for the owned pointer.
00310     @param p Pointer to own.
00311     */
00312     template<typename Y, typename D>
00313     Ptr(Y* p, D d);
00314 
00315     /**
00316     These constructors create a Ptr that shares ownership with another Ptr - that is, own the same
00317     pointer as o.
00318     With the first two, the same pointer is stored, as well; for the second, Y\* must be convertible
00319     to T\*.
00320     With the third, p is stored, and Y may be any type. This constructor allows to have completely
00321     unrelated owned and stored pointers, and should be used with care to avoid confusion. A relatively
00322     benign use is to create a non-owning Ptr, like this:
00323     @code
00324         ptr = Ptr<T>(Ptr<T>(), dont_delete_me); // owns nothing; will not delete the pointer.
00325     @endcode
00326     @param o Ptr to share ownership with.
00327     */
00328     Ptr(const Ptr& o);
00329 
00330     /** @overload
00331     @param o Ptr to share ownership with.
00332     */
00333     template<typename Y>
00334     Ptr(const Ptr<Y>& o);
00335 
00336     /** @overload
00337     @param o Ptr to share ownership with.
00338     @param p Pointer to store.
00339     */
00340     template<typename Y>
00341     Ptr(const Ptr<Y>& o, T* p);
00342 
00343     /** The destructor is equivalent to calling Ptr::release. */
00344     ~Ptr();
00345 
00346     /**
00347     Assignment replaces the current Ptr instance with one that owns and stores same pointers as o and
00348     then destroys the old instance.
00349     @param o Ptr to share ownership with.
00350      */
00351     Ptr& operator = (const Ptr& o);
00352 
00353     /** @overload */
00354     template<typename Y>
00355     Ptr& operator = (const Ptr<Y>& o);
00356 
00357     /** If no other Ptr instance owns the owned pointer, deletes it with the associated deleter. Then sets
00358     both the owned and the stored pointers to NULL.
00359     */
00360     void release();
00361 
00362     /**
00363     `ptr.reset(...)` is equivalent to `ptr = Ptr<T>(...)`.
00364     @param p Pointer to own.
00365     */
00366     template<typename Y>
00367     void reset(Y* p);
00368 
00369     /** @overload
00370     @param d Deleter to use for the owned pointer.
00371     @param p Pointer to own.
00372     */
00373     template<typename Y, typename D>
00374     void reset(Y* p, D d);
00375 
00376     /**
00377     Swaps the owned and stored pointers (and deleters, if any) of this and o.
00378     @param o Ptr to swap with.
00379     */
00380     void swap(Ptr& o);
00381 
00382     /** Returns the stored pointer. */
00383     T* get() const;
00384 
00385     /** Ordinary pointer emulation. */
00386     typename detail::RefOrVoid<T>::type operator * () const;
00387 
00388     /** Ordinary pointer emulation. */
00389     T* operator -> () const;
00390 
00391     /** Equivalent to get(). */
00392     operator T* () const;
00393 
00394     /** ptr.empty() is equivalent to `!ptr.get()`. */
00395     bool empty() const;
00396 
00397     /** Returns a Ptr that owns the same pointer as this, and stores the same
00398        pointer as this, except converted via static_cast to Y*.
00399     */
00400     template<typename Y>
00401     Ptr<Y> staticCast() const;
00402 
00403     /** Ditto for const_cast. */
00404     template<typename Y>
00405     Ptr<Y> constCast() const;
00406 
00407     /** Ditto for dynamic_cast. */
00408     template<typename Y>
00409     Ptr<Y> dynamicCast() const;
00410 
00411 #ifdef CV_CXX_MOVE_SEMANTICS
00412     Ptr(Ptr&& o);
00413     Ptr& operator = (Ptr&& o);
00414 #endif
00415 
00416 private:
00417     detail::PtrOwner* owner;
00418     T* stored;
00419 
00420     template<typename Y>
00421     friend struct Ptr; // have to do this for the cross-type copy constructor
00422 };
00423 
00424 /** Equivalent to ptr1.swap(ptr2). Provided to help write generic algorithms. */
00425 template<typename T>
00426 void swap(Ptr<T>& ptr1, Ptr<T>& ptr2);
00427 
00428 /** Return whether ptr1.get() and ptr2.get() are equal and not equal, respectively. */
00429 template<typename T>
00430 bool operator == (const Ptr<T>& ptr1, const Ptr<T>& ptr2);
00431 template<typename T>
00432 bool operator != (const Ptr<T>& ptr1, const Ptr<T>& ptr2);
00433 
00434 /** `makePtr<T>(...)` is equivalent to `Ptr<T>(new T(...))`. It is shorter than the latter, and it's
00435 marginally safer than using a constructor or Ptr::reset, since it ensures that the owned pointer
00436 is new and thus not owned by any other Ptr instance.
00437 Unfortunately, perfect forwarding is impossible to implement in C++03, and so makePtr is limited
00438 to constructors of T that have up to 10 arguments, none of which are non-const references.
00439  */
00440 template<typename T>
00441 Ptr<T> makePtr();
00442 /** @overload */
00443 template<typename T, typename A1>
00444 Ptr<T> makePtr(const A1& a1);
00445 /** @overload */
00446 template<typename T, typename A1, typename A2>
00447 Ptr<T> makePtr(const A1& a1, const A2& a2);
00448 /** @overload */
00449 template<typename T, typename A1, typename A2, typename A3>
00450 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3);
00451 /** @overload */
00452 template<typename T, typename A1, typename A2, typename A3, typename A4>
00453 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4);
00454 /** @overload */
00455 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
00456 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5);
00457 /** @overload */
00458 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
00459 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6);
00460 /** @overload */
00461 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
00462 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7);
00463 /** @overload */
00464 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
00465 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8);
00466 /** @overload */
00467 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
00468 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9);
00469 /** @overload */
00470 template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
00471 Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10);
00472 
00473 //////////////////////////////// string class ////////////////////////////////
00474 
00475 class CV_EXPORTS FileNode; //for string constructor from FileNode
00476 
00477 class CV_EXPORTS String
00478 {
00479 public:
00480     typedef char value_type;
00481     typedef char& reference;
00482     typedef const char& const_reference;
00483     typedef char* pointer;
00484     typedef const char* const_pointer;
00485     typedef ptrdiff_t difference_type;
00486     typedef size_t size_type;
00487     typedef char* iterator;
00488     typedef const char* const_iterator;
00489 
00490     static const size_t npos = size_t(-1);
00491 
00492     explicit String();
00493     String(const String& str);
00494     String(const String& str, size_t pos, size_t len = npos);
00495     String(const char* s);
00496     String(const char* s, size_t n);
00497     String(size_t n, char c);
00498     String(const char* first, const char* last);
00499     template<typename Iterator> String(Iterator first, Iterator last);
00500     explicit String(const FileNode& fn);
00501     ~String();
00502 
00503     String& operator=(const String& str);
00504     String& operator=(const char* s);
00505     String& operator=(char c);
00506 
00507     String& operator+=(const String& str);
00508     String& operator+=(const char* s);
00509     String& operator+=(char c);
00510 
00511     size_t size() const;
00512     size_t length() const;
00513 
00514     char operator[](size_t idx) const;
00515     char operator[](int idx) const;
00516 
00517     const char* begin() const;
00518     const char* end() const;
00519 
00520     const char* c_str() const;
00521 
00522     bool empty() const;
00523     void clear();
00524 
00525     int compare(const char* s) const;
00526     int compare(const String& str) const;
00527 
00528     void swap(String& str);
00529     String substr(size_t pos = 0, size_t len = npos) const;
00530 
00531     size_t find(const char* s, size_t pos, size_t n) const;
00532     size_t find(char c, size_t pos = 0) const;
00533     size_t find(const String& str, size_t pos = 0) const;
00534     size_t find(const char* s, size_t pos = 0) const;
00535 
00536     size_t rfind(const char* s, size_t pos, size_t n) const;
00537     size_t rfind(char c, size_t pos = npos) const;
00538     size_t rfind(const String& str, size_t pos = npos) const;
00539     size_t rfind(const char* s, size_t pos = npos) const;
00540 
00541     size_t find_first_of(const char* s, size_t pos, size_t n) const;
00542     size_t find_first_of(char c, size_t pos = 0) const;
00543     size_t find_first_of(const String& str, size_t pos = 0) const;
00544     size_t find_first_of(const char* s, size_t pos = 0) const;
00545 
00546     size_t find_last_of(const char* s, size_t pos, size_t n) const;
00547     size_t find_last_of(char c, size_t pos = npos) const;
00548     size_t find_last_of(const String& str, size_t pos = npos) const;
00549     size_t find_last_of(const char* s, size_t pos = npos) const;
00550 
00551     friend String operator+ (const String& lhs, const String& rhs);
00552     friend String operator+ (const String& lhs, const char*   rhs);
00553     friend String operator+ (const char*   lhs, const String& rhs);
00554     friend String operator+ (const String& lhs, char          rhs);
00555     friend String operator+ (char          lhs, const String& rhs);
00556 
00557     String toLowerCase() const;
00558 
00559 #ifndef OPENCV_NOSTL
00560     String(const std::string& str);
00561     String(const std::string& str, size_t pos, size_t len = npos);
00562     String& operator=(const std::string& str);
00563     String& operator+=(const std::string& str);
00564     operator std::string() const;
00565 
00566     friend String operator+ (const String& lhs, const std::string& rhs);
00567     friend String operator+ (const std::string& lhs, const String& rhs);
00568 #endif
00569 
00570 private:
00571     char*  cstr_;
00572     size_t len_;
00573 
00574     char* allocate(size_t len); // len without trailing 0
00575     void deallocate();
00576 
00577     String(int); // disabled and invalid. Catch invalid usages like, commandLineParser.has(0) problem
00578 };
00579 
00580 //! @} core_basic
00581 
00582 ////////////////////////// cv::String implementation /////////////////////////
00583 
00584 //! @cond IGNORED
00585 
00586 inline
00587 String::String()
00588     : cstr_(0), len_(0)
00589 {}
00590 
00591 inline
00592 String::String(const String& str)
00593     : cstr_(str.cstr_), len_(str.len_)
00594 {
00595     if (cstr_)
00596         CV_XADD(((int*)cstr_)-1, 1);
00597 }
00598 
00599 inline
00600 String::String(const String& str, size_t pos, size_t len)
00601     : cstr_(0), len_(0)
00602 {
00603     pos = min(pos, str.len_);
00604     len = min(str.len_ - pos, len);
00605     if (!len) return;
00606     if (len == str.len_)
00607     {
00608         CV_XADD(((int*)str.cstr_)-1, 1);
00609         cstr_ = str.cstr_;
00610         len_ = str.len_;
00611         return;
00612     }
00613     memcpy(allocate(len), str.cstr_ + pos, len);
00614 }
00615 
00616 inline
00617 String::String(const char* s)
00618     : cstr_(0), len_(0)
00619 {
00620     if (!s) return;
00621     size_t len = strlen(s);
00622     memcpy(allocate(len), s, len);
00623 }
00624 
00625 inline
00626 String::String(const char* s, size_t n)
00627     : cstr_(0), len_(0)
00628 {
00629     if (!n) return;
00630     memcpy(allocate(n), s, n);
00631 }
00632 
00633 inline
00634 String::String(size_t n, char c)
00635     : cstr_(0), len_(0)
00636 {
00637     memset(allocate(n), c, n);
00638 }
00639 
00640 inline
00641 String::String(const char* first, const char* last)
00642     : cstr_(0), len_(0)
00643 {
00644     size_t len = (size_t)(last - first);
00645     memcpy(allocate(len), first, len);
00646 }
00647 
00648 template<typename Iterator> inline
00649 String::String(Iterator first, Iterator last)
00650     : cstr_(0), len_(0)
00651 {
00652     size_t len = (size_t)(last - first);
00653     char* str = allocate(len);
00654     while (first != last)
00655     {
00656         *str++ = *first;
00657         ++first;
00658     }
00659 }
00660 
00661 inline
00662 String::~String()
00663 {
00664     deallocate();
00665 }
00666 
00667 inline
00668 String& String::operator=(const String& str)
00669 {
00670     if (&str == this) return *this;
00671 
00672     deallocate();
00673     if (str.cstr_) CV_XADD(((int*)str.cstr_)-1, 1);
00674     cstr_ = str.cstr_;
00675     len_ = str.len_;
00676     return *this;
00677 }
00678 
00679 inline
00680 String& String::operator=(const char* s)
00681 {
00682     deallocate();
00683     if (!s) return *this;
00684     size_t len = strlen(s);
00685     memcpy(allocate(len), s, len);
00686     return *this;
00687 }
00688 
00689 inline
00690 String& String::operator=(char c)
00691 {
00692     deallocate();
00693     allocate(1)[0] = c;
00694     return *this;
00695 }
00696 
00697 inline
00698 String& String::operator+=(const String& str)
00699 {
00700     *this = *this + str;
00701     return *this;
00702 }
00703 
00704 inline
00705 String& String::operator+=(const char* s)
00706 {
00707     *this = *this + s;
00708     return *this;
00709 }
00710 
00711 inline
00712 String& String::operator+=(char c)
00713 {
00714     *this = *this + c;
00715     return *this;
00716 }
00717 
00718 inline
00719 size_t String::size() const
00720 {
00721     return len_;
00722 }
00723 
00724 inline
00725 size_t String::length() const
00726 {
00727     return len_;
00728 }
00729 
00730 inline
00731 char String::operator[](size_t idx) const
00732 {
00733     return cstr_[idx];
00734 }
00735 
00736 inline
00737 char String::operator[](int idx) const
00738 {
00739     return cstr_[idx];
00740 }
00741 
00742 inline
00743 const char* String::begin() const
00744 {
00745     return cstr_;
00746 }
00747 
00748 inline
00749 const char* String::end() const
00750 {
00751     return len_ ? cstr_ + 1 : 0;
00752 }
00753 
00754 inline
00755 bool String::empty() const
00756 {
00757     return len_ == 0;
00758 }
00759 
00760 inline
00761 const char* String::c_str() const
00762 {
00763     return cstr_ ? cstr_ : "";
00764 }
00765 
00766 inline
00767 void String::swap(String& str)
00768 {
00769     cv::swap(cstr_, str.cstr_);
00770     cv::swap(len_, str.len_);
00771 }
00772 
00773 inline
00774 void String::clear()
00775 {
00776     deallocate();
00777 }
00778 
00779 inline
00780 int String::compare(const char* s) const
00781 {
00782     if (cstr_ == s) return 0;
00783     return strcmp(c_str(), s);
00784 }
00785 
00786 inline
00787 int String::compare(const String& str) const
00788 {
00789     if (cstr_ == str.cstr_) return 0;
00790     return strcmp(c_str(), str.c_str());
00791 }
00792 
00793 inline
00794 String String::substr(size_t pos, size_t len) const
00795 {
00796     return String(*this, pos, len);
00797 }
00798 
00799 inline
00800 size_t String::find(const char* s, size_t pos, size_t n) const
00801 {
00802     if (n == 0 || pos + n > len_) return npos;
00803     const char* lmax = cstr_ + len_ - n;
00804     for (const char* i = cstr_ + pos; i <= lmax; ++i)
00805     {
00806         size_t j = 0;
00807         while (j < n && s[j] == i[j]) ++j;
00808         if (j == n) return (size_t)(i - cstr_);
00809     }
00810     return npos;
00811 }
00812 
00813 inline
00814 size_t String::find(char c, size_t pos) const
00815 {
00816     return find(&c, pos, 1);
00817 }
00818 
00819 inline
00820 size_t String::find(const String& str, size_t pos) const
00821 {
00822     return find(str.c_str(), pos, str.len_);
00823 }
00824 
00825 inline
00826 size_t String::find(const char* s, size_t pos) const
00827 {
00828     if (pos >= len_ || !s[0]) return npos;
00829     const char* lmax = cstr_ + len_;
00830     for (const char* i = cstr_ + pos; i < lmax; ++i)
00831     {
00832         size_t j = 0;
00833         while (s[j] && s[j] == i[j])
00834         {   if(i + j >= lmax) return npos;
00835             ++j;
00836         }
00837         if (!s[j]) return (size_t)(i - cstr_);
00838     }
00839     return npos;
00840 }
00841 
00842 inline
00843 size_t String::rfind(const char* s, size_t pos, size_t n) const
00844 {
00845     if (n > len_) return npos;
00846     if (pos > len_ - n) pos = len_ - n;
00847     for (const char* i = cstr_ + pos; i >= cstr_; --i)
00848     {
00849         size_t j = 0;
00850         while (j < n && s[j] == i[j]) ++j;
00851         if (j == n) return (size_t)(i - cstr_);
00852     }
00853     return npos;
00854 }
00855 
00856 inline
00857 size_t String::rfind(char c, size_t pos) const
00858 {
00859     return rfind(&c, pos, 1);
00860 }
00861 
00862 inline
00863 size_t String::rfind(const String& str, size_t pos) const
00864 {
00865     return rfind(str.c_str(), pos, str.len_);
00866 }
00867 
00868 inline
00869 size_t String::rfind(const char* s, size_t pos) const
00870 {
00871     return rfind(s, pos, strlen(s));
00872 }
00873 
00874 inline
00875 size_t String::find_first_of(const char* s, size_t pos, size_t n) const
00876 {
00877     if (n == 0 || pos + n > len_) return npos;
00878     const char* lmax = cstr_ + len_;
00879     for (const char* i = cstr_ + pos; i < lmax; ++i)
00880     {
00881         for (size_t j = 0; j < n; ++j)
00882             if (s[j] == *i)
00883                 return (size_t)(i - cstr_);
00884     }
00885     return npos;
00886 }
00887 
00888 inline
00889 size_t String::find_first_of(char c, size_t pos) const
00890 {
00891     return find_first_of(&c, pos, 1);
00892 }
00893 
00894 inline
00895 size_t String::find_first_of(const String& str, size_t pos) const
00896 {
00897     return find_first_of(str.c_str(), pos, str.len_);
00898 }
00899 
00900 inline
00901 size_t String::find_first_of(const char* s, size_t pos) const
00902 {
00903     if (len_ == 0) return npos;
00904     if (pos >= len_ || !s[0]) return npos;
00905     const char* lmax = cstr_ + len_;
00906     for (const char* i = cstr_ + pos; i < lmax; ++i)
00907     {
00908         for (size_t j = 0; s[j]; ++j)
00909             if (s[j] == *i)
00910                 return (size_t)(i - cstr_);
00911     }
00912     return npos;
00913 }
00914 
00915 inline
00916 size_t String::find_last_of(const char* s, size_t pos, size_t n) const
00917 {
00918     if (len_ == 0) return npos;
00919     if (pos >= len_) pos = len_ - 1;
00920     for (const char* i = cstr_ + pos; i >= cstr_; --i)
00921     {
00922         for (size_t j = 0; j < n; ++j)
00923             if (s[j] == *i)
00924                 return (size_t)(i - cstr_);
00925     }
00926     return npos;
00927 }
00928 
00929 inline
00930 size_t String::find_last_of(char c, size_t pos) const
00931 {
00932     return find_last_of(&c, pos, 1);
00933 }
00934 
00935 inline
00936 size_t String::find_last_of(const String& str, size_t pos) const
00937 {
00938     return find_last_of(str.c_str(), pos, str.len_);
00939 }
00940 
00941 inline
00942 size_t String::find_last_of(const char* s, size_t pos) const
00943 {
00944     if (len_ == 0) return npos;
00945     if (pos >= len_) pos = len_ - 1;
00946     for (const char* i = cstr_ + pos; i >= cstr_; --i)
00947     {
00948         for (size_t j = 0; s[j]; ++j)
00949             if (s[j] == *i)
00950                 return (size_t)(i - cstr_);
00951     }
00952     return npos;
00953 }
00954 
00955 inline
00956 String String::toLowerCase() const
00957 {
00958     String res(cstr_, len_);
00959 
00960     for (size_t i = 0; i < len_; ++i)
00961         res.cstr_[i] = (char) ::tolower(cstr_[i]);
00962 
00963     return res;
00964 }
00965 
00966 //! @endcond
00967 
00968 // ************************* cv::String non-member functions *************************
00969 
00970 //! @relates cv::String
00971 //! @{
00972 
00973 inline
00974 String operator + (const String& lhs, const String& rhs)
00975 {
00976     String s;
00977     s.allocate(lhs.len_ + rhs.len_);
00978     memcpy(s.cstr_, lhs.cstr_, lhs.len_);
00979     memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
00980     return s;
00981 }
00982 
00983 inline
00984 String operator + (const String& lhs, const char* rhs)
00985 {
00986     String s;
00987     size_t rhslen = strlen(rhs);
00988     s.allocate(lhs.len_ + rhslen);
00989     memcpy(s.cstr_, lhs.cstr_, lhs.len_);
00990     memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
00991     return s;
00992 }
00993 
00994 inline
00995 String operator + (const char* lhs, const String& rhs)
00996 {
00997     String s;
00998     size_t lhslen = strlen(lhs);
00999     s.allocate(lhslen + rhs.len_);
01000     memcpy(s.cstr_, lhs, lhslen);
01001     memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
01002     return s;
01003 }
01004 
01005 inline
01006 String operator + (const String& lhs, char rhs)
01007 {
01008     String s;
01009     s.allocate(lhs.len_ + 1);
01010     memcpy(s.cstr_, lhs.cstr_, lhs.len_);
01011     s.cstr_[lhs.len_] = rhs;
01012     return s;
01013 }
01014 
01015 inline
01016 String operator + (char lhs, const String& rhs)
01017 {
01018     String s;
01019     s.allocate(rhs.len_ + 1);
01020     s.cstr_[0] = lhs;
01021     memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
01022     return s;
01023 }
01024 
01025 static inline bool operator== (const String& lhs, const String& rhs) { return 0 == lhs.compare(rhs); }
01026 static inline bool operator== (const char*   lhs, const String& rhs) { return 0 == rhs.compare(lhs); }
01027 static inline bool operator== (const String& lhs, const char*   rhs) { return 0 == lhs.compare(rhs); }
01028 static inline bool operator!= (const String& lhs, const String& rhs) { return 0 != lhs.compare(rhs); }
01029 static inline bool operator!= (const char*   lhs, const String& rhs) { return 0 != rhs.compare(lhs); }
01030 static inline bool operator!= (const String& lhs, const char*   rhs) { return 0 != lhs.compare(rhs); }
01031 static inline bool operator<  (const String& lhs, const String& rhs) { return lhs.compare(rhs) <  0; }
01032 static inline bool operator<  (const char*   lhs, const String& rhs) { return rhs.compare(lhs) >  0; }
01033 static inline bool operator<  (const String& lhs, const char*   rhs) { return lhs.compare(rhs) <  0; }
01034 static inline bool operator<= (const String& lhs, const String& rhs) { return lhs.compare(rhs) <= 0; }
01035 static inline bool operator<= (const char*   lhs, const String& rhs) { return rhs.compare(lhs) >= 0; }
01036 static inline bool operator<= (const String& lhs, const char*   rhs) { return lhs.compare(rhs) <= 0; }
01037 static inline bool operator>  (const String& lhs, const String& rhs) { return lhs.compare(rhs) >  0; }
01038 static inline bool operator>  (const char*   lhs, const String& rhs) { return rhs.compare(lhs) <  0; }
01039 static inline bool operator>  (const String& lhs, const char*   rhs) { return lhs.compare(rhs) >  0; }
01040 static inline bool operator>= (const String& lhs, const String& rhs) { return lhs.compare(rhs) >= 0; }
01041 static inline bool operator>= (const char*   lhs, const String& rhs) { return rhs.compare(lhs) <= 0; }
01042 static inline bool operator>= (const String& lhs, const char*   rhs) { return lhs.compare(rhs) >= 0; }
01043 
01044 //! @} relates cv::String
01045 
01046 } // cv
01047 
01048 #ifndef OPENCV_NOSTL_TRANSITIONAL
01049 namespace std
01050 {
01051     static inline void swap(cv::String& a, cv::String& b) { a.swap(b); }
01052 }
01053 #else
01054 namespace cv
01055 {
01056     template<> inline
01057     void swap<cv::String>(cv::String& a, cv::String& b)
01058     {
01059         a.swap(b);
01060     }
01061 }
01062 #endif
01063 
01064 #include "opencv2/core/ptr.inl.hpp"
01065 
01066 #endif //OPENCV_CORE_CVSTD_HPP