~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/core/include/opencv2/core/cvstd.hpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

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