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