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