cvstd.hpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_CVSTD_HPP
  44. #define OPENCV_CORE_CVSTD_HPP
  45. #ifndef __cplusplus
  46. # error cvstd.hpp header must be compiled as C++
  47. #endif
  48. #include "opencv2/core/cvdef.h"
  49. #include <cstddef>
  50. #include <cstring>
  51. #include <cctype>
  52. #include <string>
  53. // import useful primitives from stl
  54. # include <algorithm>
  55. # include <utility>
  56. # include <cstdlib> //for abs(int)
  57. # include <cmath>
  58. namespace cv
  59. {
  60. static inline uchar abs(uchar a) { return a; }
  61. static inline ushort abs(ushort a) { return a; }
  62. static inline unsigned abs(unsigned a) { return a; }
  63. static inline uint64 abs(uint64 a) { return a; }
  64. using std::min;
  65. using std::max;
  66. using std::abs;
  67. using std::swap;
  68. using std::sqrt;
  69. using std::exp;
  70. using std::pow;
  71. using std::log;
  72. }
  73. namespace cv {
  74. //! @addtogroup core_utils
  75. //! @{
  76. //////////////////////////// memory management functions ////////////////////////////
  77. /** @brief Allocates an aligned memory buffer.
  78. The function allocates the buffer of the specified size and returns it. When the buffer size is 16
  79. bytes or more, the returned buffer is aligned to 16 bytes.
  80. @param bufSize Allocated buffer size.
  81. */
  82. CV_EXPORTS void* fastMalloc(size_t bufSize);
  83. /** @brief Deallocates a memory buffer.
  84. The function deallocates the buffer allocated with fastMalloc . If NULL pointer is passed, the
  85. function does nothing. C version of the function clears the pointer *pptr* to avoid problems with
  86. double memory deallocation.
  87. @param ptr Pointer to the allocated buffer.
  88. */
  89. CV_EXPORTS void fastFree(void* ptr);
  90. /*!
  91. The STL-compilant memory Allocator based on cv::fastMalloc() and cv::fastFree()
  92. */
  93. template<typename _Tp> class Allocator
  94. {
  95. public:
  96. typedef _Tp value_type;
  97. typedef value_type* pointer;
  98. typedef const value_type* const_pointer;
  99. typedef value_type& reference;
  100. typedef const value_type& const_reference;
  101. typedef size_t size_type;
  102. typedef ptrdiff_t difference_type;
  103. template<typename U> class rebind { typedef Allocator<U> other; };
  104. explicit Allocator() {}
  105. ~Allocator() {}
  106. explicit Allocator(Allocator const&) {}
  107. template<typename U>
  108. explicit Allocator(Allocator<U> const&) {}
  109. // address
  110. pointer address(reference r) { return &r; }
  111. const_pointer address(const_reference r) { return &r; }
  112. pointer allocate(size_type count, const void* =0) { return reinterpret_cast<pointer>(fastMalloc(count * sizeof (_Tp))); }
  113. void deallocate(pointer p, size_type) { fastFree(p); }
  114. void construct(pointer p, const _Tp& v) { new(static_cast<void*>(p)) _Tp(v); }
  115. void destroy(pointer p) { p->~_Tp(); }
  116. size_type max_size() const { return cv::max(static_cast<_Tp>(-1)/sizeof(_Tp), 1); }
  117. };
  118. //! @} core_utils
  119. //! @cond IGNORED
  120. namespace detail
  121. {
  122. // Metafunction to avoid taking a reference to void.
  123. template<typename T>
  124. struct RefOrVoid { typedef T& type; };
  125. template<>
  126. struct RefOrVoid<void>{ typedef void type; };
  127. template<>
  128. struct RefOrVoid<const void>{ typedef const void type; };
  129. template<>
  130. struct RefOrVoid<volatile void>{ typedef volatile void type; };
  131. template<>
  132. struct RefOrVoid<const volatile void>{ typedef const volatile void type; };
  133. // This class would be private to Ptr, if it didn't have to be a non-template.
  134. struct PtrOwner;
  135. }
  136. template<typename Y>
  137. struct DefaultDeleter
  138. {
  139. void operator () (Y* p) const;
  140. };
  141. //! @endcond
  142. //! @addtogroup core_basic
  143. //! @{
  144. /** @brief Template class for smart pointers with shared ownership
  145. A Ptr\<T\> pretends to be a pointer to an object of type T. Unlike an ordinary pointer, however, the
  146. object will be automatically cleaned up once all Ptr instances pointing to it are destroyed.
  147. Ptr is similar to boost::shared_ptr that is part of the Boost library
  148. (<http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm>) and std::shared_ptr from
  149. the [C++11](http://en.wikipedia.org/wiki/C++11) standard.
  150. This class provides the following advantages:
  151. - Default constructor, copy constructor, and assignment operator for an arbitrary C++ class or C
  152. structure. For some objects, like files, windows, mutexes, sockets, and others, a copy
  153. constructor or an assignment operator are difficult to define. For some other objects, like
  154. complex classifiers in OpenCV, copy constructors are absent and not easy to implement. Finally,
  155. some of complex OpenCV and your own data structures may be written in C. However, copy
  156. constructors and default constructors can simplify programming a lot. Besides, they are often
  157. required (for example, by STL containers). By using a Ptr to such an object instead of the
  158. object itself, you automatically get all of the necessary constructors and the assignment
  159. operator.
  160. - *O(1)* complexity of the above-mentioned operations. While some structures, like std::vector,
  161. provide a copy constructor and an assignment operator, the operations may take a considerable
  162. amount of time if the data structures are large. But if the structures are put into a Ptr, the
  163. overhead is small and independent of the data size.
  164. - Automatic and customizable cleanup, even for C structures. See the example below with FILE\*.
  165. - Heterogeneous collections of objects. The standard STL and most other C++ and OpenCV containers
  166. can store only objects of the same type and the same size. The classical solution to store
  167. objects of different types in the same container is to store pointers to the base class (Base\*)
  168. instead but then you lose the automatic memory management. Again, by using Ptr\<Base\> instead
  169. of raw pointers, you can solve the problem.
  170. A Ptr is said to *own* a pointer - that is, for each Ptr there is a pointer that will be deleted
  171. once all Ptr instances that own it are destroyed. The owned pointer may be null, in which case
  172. nothing is deleted. Each Ptr also *stores* a pointer. The stored pointer is the pointer the Ptr
  173. pretends to be; that is, the one you get when you use Ptr::get or the conversion to T\*. It's
  174. usually the same as the owned pointer, but if you use casts or the general shared-ownership
  175. constructor, the two may diverge: the Ptr will still own the original pointer, but will itself point
  176. to something else.
  177. The owned pointer is treated as a black box. The only thing Ptr needs to know about it is how to
  178. delete it. This knowledge is encapsulated in the *deleter* - an auxiliary object that is associated
  179. with the owned pointer and shared between all Ptr instances that own it. The default deleter is an
  180. instance of DefaultDeleter, which uses the standard C++ delete operator; as such it will work with
  181. any pointer allocated with the standard new operator.
  182. However, if the pointer must be deleted in a different way, you must specify a custom deleter upon
  183. Ptr construction. A deleter is simply a callable object that accepts the pointer as its sole
  184. argument. For example, if you want to wrap FILE, you may do so as follows:
  185. @code
  186. Ptr<FILE> f(fopen("myfile.txt", "w"), fclose);
  187. if(!f) throw ...;
  188. fprintf(f, ....);
  189. ...
  190. // the file will be closed automatically by f's destructor.
  191. @endcode
  192. Alternatively, if you want all pointers of a particular type to be deleted the same way, you can
  193. specialize DefaultDeleter<T>::operator() for that type, like this:
  194. @code
  195. namespace cv {
  196. template<> void DefaultDeleter<FILE>::operator ()(FILE * obj) const
  197. {
  198. fclose(obj);
  199. }
  200. }
  201. @endcode
  202. For convenience, the following types from the OpenCV C API already have such a specialization that
  203. calls the appropriate release function:
  204. - CvCapture
  205. - CvFileStorage
  206. - CvHaarClassifierCascade
  207. - CvMat
  208. - CvMatND
  209. - CvMemStorage
  210. - CvSparseMat
  211. - CvVideoWriter
  212. - IplImage
  213. @note The shared ownership mechanism is implemented with reference counting. As such, cyclic
  214. ownership (e.g. when object a contains a Ptr to object b, which contains a Ptr to object a) will
  215. lead to all involved objects never being cleaned up. Avoid such situations.
  216. @note It is safe to concurrently read (but not write) a Ptr instance from multiple threads and
  217. therefore it is normally safe to use it in multi-threaded applications. The same is true for Mat and
  218. other C++ OpenCV classes that use internal reference counts.
  219. */
  220. template<typename T>
  221. struct Ptr
  222. {
  223. /** Generic programming support. */
  224. typedef T element_type;
  225. /** The default constructor creates a null Ptr - one that owns and stores a null pointer.
  226. */
  227. Ptr();
  228. /**
  229. If p is null, these are equivalent to the default constructor.
  230. Otherwise, these constructors assume ownership of p - that is, the created Ptr owns and stores p
  231. and assumes it is the sole owner of it. Don't use them if p is already owned by another Ptr, or
  232. else p will get deleted twice.
  233. With the first constructor, DefaultDeleter\<Y\>() becomes the associated deleter (so p will
  234. eventually be deleted with the standard delete operator). Y must be a complete type at the point
  235. of invocation.
  236. With the second constructor, d becomes the associated deleter.
  237. Y\* must be convertible to T\*.
  238. @param p Pointer to own.
  239. @note It is often easier to use makePtr instead.
  240. */
  241. template<typename Y>
  242. #ifdef DISABLE_OPENCV_24_COMPATIBILITY
  243. explicit
  244. #endif
  245. Ptr(Y* p);
  246. /** @overload
  247. @param d Deleter to use for the owned pointer.
  248. @param p Pointer to own.
  249. */
  250. template<typename Y, typename D>
  251. Ptr(Y* p, D d);
  252. /**
  253. These constructors create a Ptr that shares ownership with another Ptr - that is, own the same
  254. pointer as o.
  255. With the first two, the same pointer is stored, as well; for the second, Y\* must be convertible
  256. to T\*.
  257. With the third, p is stored, and Y may be any type. This constructor allows to have completely
  258. unrelated owned and stored pointers, and should be used with care to avoid confusion. A relatively
  259. benign use is to create a non-owning Ptr, like this:
  260. @code
  261. ptr = Ptr<T>(Ptr<T>(), dont_delete_me); // owns nothing; will not delete the pointer.
  262. @endcode
  263. @param o Ptr to share ownership with.
  264. */
  265. Ptr(const Ptr& o);
  266. /** @overload
  267. @param o Ptr to share ownership with.
  268. */
  269. template<typename Y>
  270. Ptr(const Ptr<Y>& o);
  271. /** @overload
  272. @param o Ptr to share ownership with.
  273. @param p Pointer to store.
  274. */
  275. template<typename Y>
  276. Ptr(const Ptr<Y>& o, T* p);
  277. /** The destructor is equivalent to calling Ptr::release. */
  278. ~Ptr();
  279. /**
  280. Assignment replaces the current Ptr instance with one that owns and stores same pointers as o and
  281. then destroys the old instance.
  282. @param o Ptr to share ownership with.
  283. */
  284. Ptr& operator = (const Ptr& o);
  285. /** @overload */
  286. template<typename Y>
  287. Ptr& operator = (const Ptr<Y>& o);
  288. /** If no other Ptr instance owns the owned pointer, deletes it with the associated deleter. Then sets
  289. both the owned and the stored pointers to NULL.
  290. */
  291. void release();
  292. /**
  293. `ptr.reset(...)` is equivalent to `ptr = Ptr<T>(...)`.
  294. @param p Pointer to own.
  295. */
  296. template<typename Y>
  297. void reset(Y* p);
  298. /** @overload
  299. @param d Deleter to use for the owned pointer.
  300. @param p Pointer to own.
  301. */
  302. template<typename Y, typename D>
  303. void reset(Y* p, D d);
  304. /**
  305. Swaps the owned and stored pointers (and deleters, if any) of this and o.
  306. @param o Ptr to swap with.
  307. */
  308. void swap(Ptr& o);
  309. /** Returns the stored pointer. */
  310. T* get() const;
  311. /** Ordinary pointer emulation. */
  312. typename detail::RefOrVoid<T>::type operator * () const;
  313. /** Ordinary pointer emulation. */
  314. T* operator -> () const;
  315. /** Equivalent to get(). */
  316. operator T* () const;
  317. /** ptr.empty() is equivalent to `!ptr.get()`. */
  318. bool empty() const;
  319. /** Returns a Ptr that owns the same pointer as this, and stores the same
  320. pointer as this, except converted via static_cast to Y*.
  321. */
  322. template<typename Y>
  323. Ptr<Y> staticCast() const;
  324. /** Ditto for const_cast. */
  325. template<typename Y>
  326. Ptr<Y> constCast() const;
  327. /** Ditto for dynamic_cast. */
  328. template<typename Y>
  329. Ptr<Y> dynamicCast() const;
  330. #ifdef CV_CXX_MOVE_SEMANTICS
  331. Ptr(Ptr&& o);
  332. Ptr& operator = (Ptr&& o);
  333. #endif
  334. private:
  335. detail::PtrOwner* owner;
  336. T* stored;
  337. template<typename Y>
  338. friend struct Ptr; // have to do this for the cross-type copy constructor
  339. };
  340. /** Equivalent to ptr1.swap(ptr2). Provided to help write generic algorithms. */
  341. template<typename T>
  342. void swap(Ptr<T>& ptr1, Ptr<T>& ptr2);
  343. /** Return whether ptr1.get() and ptr2.get() are equal and not equal, respectively. */
  344. template<typename T>
  345. bool operator == (const Ptr<T>& ptr1, const Ptr<T>& ptr2);
  346. template<typename T>
  347. bool operator != (const Ptr<T>& ptr1, const Ptr<T>& ptr2);
  348. /** `makePtr<T>(...)` is equivalent to `Ptr<T>(new T(...))`. It is shorter than the latter, and it's
  349. marginally safer than using a constructor or Ptr::reset, since it ensures that the owned pointer
  350. is new and thus not owned by any other Ptr instance.
  351. Unfortunately, perfect forwarding is impossible to implement in C++03, and so makePtr is limited
  352. to constructors of T that have up to 10 arguments, none of which are non-const references.
  353. */
  354. template<typename T>
  355. Ptr<T> makePtr();
  356. /** @overload */
  357. template<typename T, typename A1>
  358. Ptr<T> makePtr(const A1& a1);
  359. /** @overload */
  360. template<typename T, typename A1, typename A2>
  361. Ptr<T> makePtr(const A1& a1, const A2& a2);
  362. /** @overload */
  363. template<typename T, typename A1, typename A2, typename A3>
  364. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3);
  365. /** @overload */
  366. template<typename T, typename A1, typename A2, typename A3, typename A4>
  367. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4);
  368. /** @overload */
  369. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
  370. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5);
  371. /** @overload */
  372. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
  373. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6);
  374. /** @overload */
  375. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
  376. Ptr<T> makePtr(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7);
  377. /** @overload */
  378. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
  379. 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);
  380. /** @overload */
  381. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9>
  382. 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);
  383. /** @overload */
  384. template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9, typename A10>
  385. 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);
  386. //////////////////////////////// string class ////////////////////////////////
  387. class CV_EXPORTS FileNode; //for string constructor from FileNode
  388. class CV_EXPORTS String
  389. {
  390. public:
  391. typedef char value_type;
  392. typedef char& reference;
  393. typedef const char& const_reference;
  394. typedef char* pointer;
  395. typedef const char* const_pointer;
  396. typedef ptrdiff_t difference_type;
  397. typedef size_t size_type;
  398. typedef char* iterator;
  399. typedef const char* const_iterator;
  400. static const size_t npos = size_t(-1);
  401. String();
  402. String(const String& str);
  403. String(const String& str, size_t pos, size_t len = npos);
  404. String(const char* s);
  405. String(const char* s, size_t n);
  406. String(size_t n, char c);
  407. String(const char* first, const char* last);
  408. template<typename Iterator> String(Iterator first, Iterator last);
  409. explicit String(const FileNode& fn);
  410. ~String();
  411. String& operator=(const String& str);
  412. String& operator=(const char* s);
  413. String& operator=(char c);
  414. String& operator+=(const String& str);
  415. String& operator+=(const char* s);
  416. String& operator+=(char c);
  417. size_t size() const;
  418. size_t length() const;
  419. char operator[](size_t idx) const;
  420. char operator[](int idx) const;
  421. const char* begin() const;
  422. const char* end() const;
  423. const char* c_str() const;
  424. bool empty() const;
  425. void clear();
  426. int compare(const char* s) const;
  427. int compare(const String& str) const;
  428. void swap(String& str);
  429. String substr(size_t pos = 0, size_t len = npos) const;
  430. size_t find(const char* s, size_t pos, size_t n) const;
  431. size_t find(char c, size_t pos = 0) const;
  432. size_t find(const String& str, size_t pos = 0) const;
  433. size_t find(const char* s, size_t pos = 0) const;
  434. size_t rfind(const char* s, size_t pos, size_t n) const;
  435. size_t rfind(char c, size_t pos = npos) const;
  436. size_t rfind(const String& str, size_t pos = npos) const;
  437. size_t rfind(const char* s, size_t pos = npos) const;
  438. size_t find_first_of(const char* s, size_t pos, size_t n) const;
  439. size_t find_first_of(char c, size_t pos = 0) const;
  440. size_t find_first_of(const String& str, size_t pos = 0) const;
  441. size_t find_first_of(const char* s, size_t pos = 0) const;
  442. size_t find_last_of(const char* s, size_t pos, size_t n) const;
  443. size_t find_last_of(char c, size_t pos = npos) const;
  444. size_t find_last_of(const String& str, size_t pos = npos) const;
  445. size_t find_last_of(const char* s, size_t pos = npos) const;
  446. friend String operator+ (const String& lhs, const String& rhs);
  447. friend String operator+ (const String& lhs, const char* rhs);
  448. friend String operator+ (const char* lhs, const String& rhs);
  449. friend String operator+ (const String& lhs, char rhs);
  450. friend String operator+ (char lhs, const String& rhs);
  451. String toLowerCase() const;
  452. String(const std::string& str);
  453. String(const std::string& str, size_t pos, size_t len = npos);
  454. String& operator=(const std::string& str);
  455. String& operator+=(const std::string& str);
  456. operator std::string() const;
  457. friend String operator+ (const String& lhs, const std::string& rhs);
  458. friend String operator+ (const std::string& lhs, const String& rhs);
  459. private:
  460. char* cstr_;
  461. size_t len_;
  462. char* allocate(size_t len); // len without trailing 0
  463. void deallocate();
  464. String(int); // disabled and invalid. Catch invalid usages like, commandLineParser.has(0) problem
  465. };
  466. //! @} core_basic
  467. ////////////////////////// cv::String implementation /////////////////////////
  468. //! @cond IGNORED
  469. inline
  470. String::String()
  471. : cstr_(0), len_(0)
  472. {}
  473. inline
  474. String::String(const String& str)
  475. : cstr_(str.cstr_), len_(str.len_)
  476. {
  477. if (cstr_)
  478. CV_XADD(((int*)cstr_)-1, 1);
  479. }
  480. inline
  481. String::String(const String& str, size_t pos, size_t len)
  482. : cstr_(0), len_(0)
  483. {
  484. pos = min(pos, str.len_);
  485. len = min(str.len_ - pos, len);
  486. if (!len) return;
  487. if (len == str.len_)
  488. {
  489. CV_XADD(((int*)str.cstr_)-1, 1);
  490. cstr_ = str.cstr_;
  491. len_ = str.len_;
  492. return;
  493. }
  494. memcpy(allocate(len), str.cstr_ + pos, len);
  495. }
  496. inline
  497. String::String(const char* s)
  498. : cstr_(0), len_(0)
  499. {
  500. if (!s) return;
  501. size_t len = strlen(s);
  502. if (!len) return;
  503. memcpy(allocate(len), s, len);
  504. }
  505. inline
  506. String::String(const char* s, size_t n)
  507. : cstr_(0), len_(0)
  508. {
  509. if (!n) return;
  510. if (!s) return;
  511. memcpy(allocate(n), s, n);
  512. }
  513. inline
  514. String::String(size_t n, char c)
  515. : cstr_(0), len_(0)
  516. {
  517. if (!n) return;
  518. memset(allocate(n), c, n);
  519. }
  520. inline
  521. String::String(const char* first, const char* last)
  522. : cstr_(0), len_(0)
  523. {
  524. size_t len = (size_t)(last - first);
  525. if (!len) return;
  526. memcpy(allocate(len), first, len);
  527. }
  528. template<typename Iterator> inline
  529. String::String(Iterator first, Iterator last)
  530. : cstr_(0), len_(0)
  531. {
  532. size_t len = (size_t)(last - first);
  533. if (!len) return;
  534. char* str = allocate(len);
  535. while (first != last)
  536. {
  537. *str++ = *first;
  538. ++first;
  539. }
  540. }
  541. inline
  542. String::~String()
  543. {
  544. deallocate();
  545. }
  546. inline
  547. String& String::operator=(const String& str)
  548. {
  549. if (&str == this) return *this;
  550. deallocate();
  551. if (str.cstr_) CV_XADD(((int*)str.cstr_)-1, 1);
  552. cstr_ = str.cstr_;
  553. len_ = str.len_;
  554. return *this;
  555. }
  556. inline
  557. String& String::operator=(const char* s)
  558. {
  559. deallocate();
  560. if (!s) return *this;
  561. size_t len = strlen(s);
  562. if (len) memcpy(allocate(len), s, len);
  563. return *this;
  564. }
  565. inline
  566. String& String::operator=(char c)
  567. {
  568. deallocate();
  569. allocate(1)[0] = c;
  570. return *this;
  571. }
  572. inline
  573. String& String::operator+=(const String& str)
  574. {
  575. *this = *this + str;
  576. return *this;
  577. }
  578. inline
  579. String& String::operator+=(const char* s)
  580. {
  581. *this = *this + s;
  582. return *this;
  583. }
  584. inline
  585. String& String::operator+=(char c)
  586. {
  587. *this = *this + c;
  588. return *this;
  589. }
  590. inline
  591. size_t String::size() const
  592. {
  593. return len_;
  594. }
  595. inline
  596. size_t String::length() const
  597. {
  598. return len_;
  599. }
  600. inline
  601. char String::operator[](size_t idx) const
  602. {
  603. return cstr_[idx];
  604. }
  605. inline
  606. char String::operator[](int idx) const
  607. {
  608. return cstr_[idx];
  609. }
  610. inline
  611. const char* String::begin() const
  612. {
  613. return cstr_;
  614. }
  615. inline
  616. const char* String::end() const
  617. {
  618. return len_ ? cstr_ + len_ : NULL;
  619. }
  620. inline
  621. bool String::empty() const
  622. {
  623. return len_ == 0;
  624. }
  625. inline
  626. const char* String::c_str() const
  627. {
  628. return cstr_ ? cstr_ : "";
  629. }
  630. inline
  631. void String::swap(String& str)
  632. {
  633. cv::swap(cstr_, str.cstr_);
  634. cv::swap(len_, str.len_);
  635. }
  636. inline
  637. void String::clear()
  638. {
  639. deallocate();
  640. }
  641. inline
  642. int String::compare(const char* s) const
  643. {
  644. if (cstr_ == s) return 0;
  645. return strcmp(c_str(), s);
  646. }
  647. inline
  648. int String::compare(const String& str) const
  649. {
  650. if (cstr_ == str.cstr_) return 0;
  651. return strcmp(c_str(), str.c_str());
  652. }
  653. inline
  654. String String::substr(size_t pos, size_t len) const
  655. {
  656. return String(*this, pos, len);
  657. }
  658. inline
  659. size_t String::find(const char* s, size_t pos, size_t n) const
  660. {
  661. if (n == 0 || pos + n > len_) return npos;
  662. const char* lmax = cstr_ + len_ - n;
  663. for (const char* i = cstr_ + pos; i <= lmax; ++i)
  664. {
  665. size_t j = 0;
  666. while (j < n && s[j] == i[j]) ++j;
  667. if (j == n) return (size_t)(i - cstr_);
  668. }
  669. return npos;
  670. }
  671. inline
  672. size_t String::find(char c, size_t pos) const
  673. {
  674. return find(&c, pos, 1);
  675. }
  676. inline
  677. size_t String::find(const String& str, size_t pos) const
  678. {
  679. return find(str.c_str(), pos, str.len_);
  680. }
  681. inline
  682. size_t String::find(const char* s, size_t pos) const
  683. {
  684. if (pos >= len_ || !s[0]) return npos;
  685. const char* lmax = cstr_ + len_;
  686. for (const char* i = cstr_ + pos; i < lmax; ++i)
  687. {
  688. size_t j = 0;
  689. while (s[j] && s[j] == i[j])
  690. { if(i + j >= lmax) return npos;
  691. ++j;
  692. }
  693. if (!s[j]) return (size_t)(i - cstr_);
  694. }
  695. return npos;
  696. }
  697. inline
  698. size_t String::rfind(const char* s, size_t pos, size_t n) const
  699. {
  700. if (n > len_) return npos;
  701. if (pos > len_ - n) pos = len_ - n;
  702. for (const char* i = cstr_ + pos; i >= cstr_; --i)
  703. {
  704. size_t j = 0;
  705. while (j < n && s[j] == i[j]) ++j;
  706. if (j == n) return (size_t)(i - cstr_);
  707. }
  708. return npos;
  709. }
  710. inline
  711. size_t String::rfind(char c, size_t pos) const
  712. {
  713. return rfind(&c, pos, 1);
  714. }
  715. inline
  716. size_t String::rfind(const String& str, size_t pos) const
  717. {
  718. return rfind(str.c_str(), pos, str.len_);
  719. }
  720. inline
  721. size_t String::rfind(const char* s, size_t pos) const
  722. {
  723. return rfind(s, pos, strlen(s));
  724. }
  725. inline
  726. size_t String::find_first_of(const char* s, size_t pos, size_t n) const
  727. {
  728. if (n == 0 || pos + n > len_) return npos;
  729. const char* lmax = cstr_ + len_;
  730. for (const char* i = cstr_ + pos; i < lmax; ++i)
  731. {
  732. for (size_t j = 0; j < n; ++j)
  733. if (s[j] == *i)
  734. return (size_t)(i - cstr_);
  735. }
  736. return npos;
  737. }
  738. inline
  739. size_t String::find_first_of(char c, size_t pos) const
  740. {
  741. return find_first_of(&c, pos, 1);
  742. }
  743. inline
  744. size_t String::find_first_of(const String& str, size_t pos) const
  745. {
  746. return find_first_of(str.c_str(), pos, str.len_);
  747. }
  748. inline
  749. size_t String::find_first_of(const char* s, size_t pos) const
  750. {
  751. if (len_ == 0) return npos;
  752. if (pos >= len_ || !s[0]) return npos;
  753. const char* lmax = cstr_ + len_;
  754. for (const char* i = cstr_ + pos; i < lmax; ++i)
  755. {
  756. for (size_t j = 0; s[j]; ++j)
  757. if (s[j] == *i)
  758. return (size_t)(i - cstr_);
  759. }
  760. return npos;
  761. }
  762. inline
  763. size_t String::find_last_of(const char* s, size_t pos, size_t n) const
  764. {
  765. if (len_ == 0) return npos;
  766. if (pos >= len_) pos = len_ - 1;
  767. for (const char* i = cstr_ + pos; i >= cstr_; --i)
  768. {
  769. for (size_t j = 0; j < n; ++j)
  770. if (s[j] == *i)
  771. return (size_t)(i - cstr_);
  772. }
  773. return npos;
  774. }
  775. inline
  776. size_t String::find_last_of(char c, size_t pos) const
  777. {
  778. return find_last_of(&c, pos, 1);
  779. }
  780. inline
  781. size_t String::find_last_of(const String& str, size_t pos) const
  782. {
  783. return find_last_of(str.c_str(), pos, str.len_);
  784. }
  785. inline
  786. size_t String::find_last_of(const char* s, size_t pos) const
  787. {
  788. if (len_ == 0) return npos;
  789. if (pos >= len_) pos = len_ - 1;
  790. for (const char* i = cstr_ + pos; i >= cstr_; --i)
  791. {
  792. for (size_t j = 0; s[j]; ++j)
  793. if (s[j] == *i)
  794. return (size_t)(i - cstr_);
  795. }
  796. return npos;
  797. }
  798. inline
  799. String String::toLowerCase() const
  800. {
  801. if (!cstr_)
  802. return String();
  803. String res(cstr_, len_);
  804. for (size_t i = 0; i < len_; ++i)
  805. res.cstr_[i] = (char) ::tolower(cstr_[i]);
  806. return res;
  807. }
  808. //! @endcond
  809. // ************************* cv::String non-member functions *************************
  810. //! @relates cv::String
  811. //! @{
  812. inline
  813. String operator + (const String& lhs, const String& rhs)
  814. {
  815. String s;
  816. s.allocate(lhs.len_ + rhs.len_);
  817. if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
  818. if (rhs.len_) memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
  819. return s;
  820. }
  821. inline
  822. String operator + (const String& lhs, const char* rhs)
  823. {
  824. String s;
  825. size_t rhslen = strlen(rhs);
  826. s.allocate(lhs.len_ + rhslen);
  827. if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
  828. if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
  829. return s;
  830. }
  831. inline
  832. String operator + (const char* lhs, const String& rhs)
  833. {
  834. String s;
  835. size_t lhslen = strlen(lhs);
  836. s.allocate(lhslen + rhs.len_);
  837. if (lhslen) memcpy(s.cstr_, lhs, lhslen);
  838. if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
  839. return s;
  840. }
  841. inline
  842. String operator + (const String& lhs, char rhs)
  843. {
  844. String s;
  845. s.allocate(lhs.len_ + 1);
  846. if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
  847. s.cstr_[lhs.len_] = rhs;
  848. return s;
  849. }
  850. inline
  851. String operator + (char lhs, const String& rhs)
  852. {
  853. String s;
  854. s.allocate(rhs.len_ + 1);
  855. s.cstr_[0] = lhs;
  856. if (rhs.len_) memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
  857. return s;
  858. }
  859. static inline bool operator== (const String& lhs, const String& rhs) { return 0 == lhs.compare(rhs); }
  860. static inline bool operator== (const char* lhs, const String& rhs) { return 0 == rhs.compare(lhs); }
  861. static inline bool operator== (const String& lhs, const char* rhs) { return 0 == lhs.compare(rhs); }
  862. static inline bool operator!= (const String& lhs, const String& rhs) { return 0 != lhs.compare(rhs); }
  863. static inline bool operator!= (const char* lhs, const String& rhs) { return 0 != rhs.compare(lhs); }
  864. static inline bool operator!= (const String& lhs, const char* rhs) { return 0 != lhs.compare(rhs); }
  865. static inline bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
  866. static inline bool operator< (const char* lhs, const String& rhs) { return rhs.compare(lhs) > 0; }
  867. static inline bool operator< (const String& lhs, const char* rhs) { return lhs.compare(rhs) < 0; }
  868. static inline bool operator<= (const String& lhs, const String& rhs) { return lhs.compare(rhs) <= 0; }
  869. static inline bool operator<= (const char* lhs, const String& rhs) { return rhs.compare(lhs) >= 0; }
  870. static inline bool operator<= (const String& lhs, const char* rhs) { return lhs.compare(rhs) <= 0; }
  871. static inline bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
  872. static inline bool operator> (const char* lhs, const String& rhs) { return rhs.compare(lhs) < 0; }
  873. static inline bool operator> (const String& lhs, const char* rhs) { return lhs.compare(rhs) > 0; }
  874. static inline bool operator>= (const String& lhs, const String& rhs) { return lhs.compare(rhs) >= 0; }
  875. static inline bool operator>= (const char* lhs, const String& rhs) { return rhs.compare(lhs) <= 0; }
  876. static inline bool operator>= (const String& lhs, const char* rhs) { return lhs.compare(rhs) >= 0; }
  877. //! @} relates cv::String
  878. } // cv
  879. namespace std
  880. {
  881. static inline void swap(cv::String& a, cv::String& b) { a.swap(b); }
  882. }
  883. #include "opencv2/core/ptr.inl.hpp"
  884. #endif //OPENCV_CORE_CVSTD_HPP