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_CVSTDINL_HPP__
45 #define __OPENCV_CORE_CVSTDINL_HPP__
46 
47 #ifndef OPENCV_NOSTL
48 #  include <complex>
49 #  include <ostream>
50 #endif
51 
52 //! @cond IGNORED
53 
54 namespace cv
55 {
56 #ifndef OPENCV_NOSTL
57 
58 template<typename _Tp> class DataType< std::complex<_Tp> >
59 {
60 public:
61     typedef std::complex<_Tp>  value_type;
62     typedef value_type         work_type;
63     typedef _Tp                channel_type;
64 
65     enum { generic_type = 0,
66            depth        = DataType<channel_type>::depth,
67            channels     = 2,
68            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
69            type         = CV_MAKETYPE(depth, channels) };
70 
71     typedef Vec<channel_type, channels> vec_type;
72 };
73 
74 inline
String(const std::string & str)75 String::String(const std::string& str)
76     : cstr_(0), len_(0)
77 {
78     if (!str.empty())
79     {
80         size_t len = str.size();
81         memcpy(allocate(len), str.c_str(), len);
82     }
83 }
84 
85 inline
String(const std::string & str,size_t pos,size_t len)86 String::String(const std::string& str, size_t pos, size_t len)
87     : cstr_(0), len_(0)
88 {
89     size_t strlen = str.size();
90     pos = max(pos, strlen);
91     len = min(strlen - pos, len);
92     if (!len) return;
93     memcpy(allocate(len), str.c_str() + pos, len);
94 }
95 
96 inline
operator =(const std::string & str)97 String& String::operator = (const std::string& str)
98 {
99     deallocate();
100     if (!str.empty())
101     {
102         size_t len = str.size();
103         memcpy(allocate(len), str.c_str(), len);
104     }
105     return *this;
106 }
107 
108 inline
operator +=(const std::string & str)109 String& String::operator += (const std::string& str)
110 {
111     *this = *this + str;
112     return *this;
113 }
114 
115 inline
operator std::string() const116 String::operator std::string() const
117 {
118     return std::string(cstr_, len_);
119 }
120 
121 inline
operator +(const String & lhs,const std::string & rhs)122 String operator + (const String& lhs, const std::string& rhs)
123 {
124     String s;
125     size_t rhslen = rhs.size();
126     s.allocate(lhs.len_ + rhslen);
127     memcpy(s.cstr_, lhs.cstr_, lhs.len_);
128     memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
129     return s;
130 }
131 
132 inline
operator +(const std::string & lhs,const String & rhs)133 String operator + (const std::string& lhs, const String& rhs)
134 {
135     String s;
136     size_t lhslen = lhs.size();
137     s.allocate(lhslen + rhs.len_);
138     memcpy(s.cstr_, lhs.c_str(), lhslen);
139     memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
140     return s;
141 }
142 
143 inline
operator std::string() const144 FileNode::operator std::string() const
145 {
146     String value;
147     read(*this, value, value);
148     return value;
149 }
150 
151 template<> inline
operator >>(const FileNode & n,std::string & value)152 void operator >> (const FileNode& n, std::string& value)
153 {
154     String val;
155     read(n, val, val);
156     value = val;
157 }
158 
159 template<> inline
operator <<(FileStorage & fs,const std::string & value)160 FileStorage& operator << (FileStorage& fs, const std::string& value)
161 {
162     return fs << cv::String(value);
163 }
164 
165 static inline
operator <<(std::ostream & os,const String & str)166 std::ostream& operator << (std::ostream& os, const String& str)
167 {
168     return os << str.c_str();
169 }
170 
171 static inline
operator <<(std::ostream & out,Ptr<Formatted> fmtd)172 std::ostream& operator << (std::ostream& out, Ptr<Formatted> fmtd)
173 {
174     fmtd->reset();
175     for(const char* str = fmtd->next(); str; str = fmtd->next())
176         out << str;
177     return out;
178 }
179 
180 static inline
operator <<(std::ostream & out,const Mat & mtx)181 std::ostream& operator << (std::ostream& out, const Mat& mtx)
182 {
183     return out << Formatter::get()->format(mtx);
184 }
185 
186 template<typename _Tp> static inline
operator <<(std::ostream & out,const std::vector<Point_<_Tp>> & vec)187 std::ostream& operator << (std::ostream& out, const std::vector<Point_<_Tp> >& vec)
188 {
189     return out << Formatter::get()->format(Mat(vec));
190 }
191 
192 
193 template<typename _Tp> static inline
operator <<(std::ostream & out,const std::vector<Point3_<_Tp>> & vec)194 std::ostream& operator << (std::ostream& out, const std::vector<Point3_<_Tp> >& vec)
195 {
196     return out << Formatter::get()->format(Mat(vec));
197 }
198 
199 
200 template<typename _Tp, int m, int n> static inline
operator <<(std::ostream & out,const Matx<_Tp,m,n> & matx)201 std::ostream& operator << (std::ostream& out, const Matx<_Tp, m, n>& matx)
202 {
203     return out << Formatter::get()->format(Mat(matx));
204 }
205 
206 template<typename _Tp> static inline
operator <<(std::ostream & out,const Point_<_Tp> & p)207 std::ostream& operator << (std::ostream& out, const Point_<_Tp>& p)
208 {
209     out << "[" << p.x << ", " << p.y << "]";
210     return out;
211 }
212 
213 template<typename _Tp> static inline
operator <<(std::ostream & out,const Point3_<_Tp> & p)214 std::ostream& operator << (std::ostream& out, const Point3_<_Tp>& p)
215 {
216     out << "[" << p.x << ", " << p.y << ", " << p.z << "]";
217     return out;
218 }
219 
220 template<typename _Tp, int n> static inline
operator <<(std::ostream & out,const Vec<_Tp,n> & vec)221 std::ostream& operator << (std::ostream& out, const Vec<_Tp, n>& vec)
222 {
223     out << "[";
224 #ifdef _MSC_VER
225 #pragma warning( push )
226 #pragma warning( disable: 4127 )
227 #endif
228     if(Vec<_Tp, n>::depth < CV_32F)
229 #ifdef _MSC_VER
230 #pragma warning( pop )
231 #endif
232     {
233         for (int i = 0; i < n - 1; ++i) {
234             out << (int)vec[i] << ", ";
235         }
236         out << (int)vec[n-1] << "]";
237     }
238     else
239     {
240         for (int i = 0; i < n - 1; ++i) {
241             out << vec[i] << ", ";
242         }
243         out << vec[n-1] << "]";
244     }
245 
246     return out;
247 }
248 
249 template<typename _Tp> static inline
operator <<(std::ostream & out,const Size_<_Tp> & size)250 std::ostream& operator << (std::ostream& out, const Size_<_Tp>& size)
251 {
252     return out << "[" << size.width << " x " << size.height << "]";
253 }
254 
255 template<typename _Tp> static inline
operator <<(std::ostream & out,const Rect_<_Tp> & rect)256 std::ostream& operator << (std::ostream& out, const Rect_<_Tp>& rect)
257 {
258     return out << "[" << rect.width << " x " << rect.height << " from (" << rect.x << ", " << rect.y << ")]";
259 }
260 
261 
262 #endif // OPENCV_NOSTL
263 } // cv
264 
265 //! @endcond
266 
267 #endif // __OPENCV_CORE_CVSTDINL_HPP__
268