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) 2013, OpenCV Foundation, all rights reserved. |
14 | // Third party copyrights are property of their respective owners. |
15 | // |
16 | // Redistribution and use in source and binary forms, with or without modification, |
17 | // are permitted provided that the following conditions are met: |
18 | // |
19 | // * Redistribution's of source code must retain the above copyright notice, |
20 | // this list of conditions and the following disclaimer. |
21 | // |
22 | // * Redistribution's in binary form must reproduce the above copyright notice, |
23 | // this list of conditions and the following disclaimer in the documentation |
24 | // and/or other materials provided with the distribution. |
25 | // |
26 | // * The name of the copyright holders may not be used to endorse or promote products |
27 | // derived from this software without specific prior written permission. |
28 | // |
29 | // This software is provided by the copyright holders and contributors "as is" and |
30 | // any express or implied warranties, including, but not limited to, the implied |
31 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
32 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
33 | // indirect, incidental, special, exemplary, or consequential damages |
34 | // (including, but not limited to, procurement of substitute goods or services; |
35 | // loss of use, data, or profits; or business interruption) however caused |
36 | // and on any theory of liability, whether in contract, strict liability, |
37 | // or tort (including negligence or otherwise) arising in any way out of |
38 | // the use of this software, even if advised of the possibility of such damage. |
39 | // |
40 | //M*/ |
41 | |
42 | #include <opencv2/core.hpp> |
43 | #include <map> |
44 | #include <ostream> |
45 | |
46 | #include <opencv2/dnn/dnn.hpp> |
47 | |
48 | #ifndef OPENCV_DNN_DNN_DICT_HPP |
49 | #define OPENCV_DNN_DNN_DICT_HPP |
50 | |
51 | namespace cv { |
52 | namespace dnn { |
53 | CV__DNN_INLINE_NS_BEGIN |
54 | //! @addtogroup dnn |
55 | //! @{ |
56 | |
57 | /** @brief This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64. |
58 | * @todo Maybe int64 is useless because double type exactly stores at least 2^52 integers. |
59 | */ |
60 | struct CV_EXPORTS_W DictValue |
61 | { |
62 | DictValue(const DictValue &r); |
63 | explicit DictValue(bool i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i ? 1 : 0; } //!< Constructs integer scalar |
64 | explicit DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar |
65 | CV_WRAP explicit DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar |
66 | explicit DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar |
67 | CV_WRAP explicit DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar |
68 | CV_WRAP explicit DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar |
69 | explicit DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload |
70 | |
71 | template<typename TypeIter> |
72 | static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array |
73 | template<typename TypeIter> |
74 | static DictValue arrayReal(TypeIter begin, int size); //!< Constructs floating point array |
75 | template<typename TypeIter> |
76 | static DictValue arrayString(TypeIter begin, int size); //!< Constructs array of strings |
77 | |
78 | template<typename T> |
79 | T get(int idx = -1) const; //!< Tries to convert array element with specified index to requested type and returns its. |
80 | |
81 | int size() const; |
82 | |
83 | CV_WRAP bool isInt() const; |
84 | CV_WRAP bool isString() const; |
85 | CV_WRAP bool isReal() const; |
86 | |
87 | CV_WRAP int getIntValue(int idx = -1) const; |
88 | CV_WRAP double getRealValue(int idx = -1) const; |
89 | CV_WRAP String getStringValue(int idx = -1) const; |
90 | |
91 | DictValue &operator=(const DictValue &r); |
92 | |
93 | friend std::ostream &operator<<(std::ostream &stream, const DictValue &dictv); |
94 | |
95 | ~DictValue(); |
96 | |
97 | private: |
98 | |
99 | Param type; |
100 | |
101 | union |
102 | { |
103 | AutoBuffer<int64, 1> *pi; |
104 | AutoBuffer<double, 1> *pd; |
105 | AutoBuffer<String, 1> *ps; |
106 | void *pv; |
107 | }; |
108 | |
109 | DictValue(Param _type, void *_p) : type(_type), pv(_p) {} |
110 | void release(); |
111 | }; |
112 | |
113 | /** @brief This class implements name-value dictionary, values are instances of DictValue. */ |
114 | class CV_EXPORTS Dict |
115 | { |
116 | typedef std::map<String, DictValue> _Dict; |
117 | _Dict dict; |
118 | |
119 | public: |
120 | |
121 | //! Checks a presence of the @p key in the dictionary. |
122 | bool has(const String &key) const; |
123 | |
124 | //! If the @p key in the dictionary then returns pointer to its value, else returns NULL. |
125 | DictValue *ptr(const String &key); |
126 | |
127 | /** @overload */ |
128 | const DictValue *ptr(const String &key) const; |
129 | |
130 | //! If the @p key in the dictionary then returns its value, else an error will be generated. |
131 | const DictValue &get(const String &key) const; |
132 | |
133 | /** @overload */ |
134 | template <typename T> |
135 | T get(const String &key) const; |
136 | |
137 | //! If the @p key in the dictionary then returns its value, else returns @p defaultValue. |
138 | template <typename T> |
139 | T get(const String &key, const T &defaultValue) const; |
140 | |
141 | //! Sets new @p value for the @p key, or adds new key-value pair into the dictionary. |
142 | template<typename T> |
143 | const T &set(const String &key, const T &value); |
144 | |
145 | //! Erase @p key from the dictionary. |
146 | void erase(const String &key); |
147 | |
148 | friend std::ostream &operator<<(std::ostream &stream, const Dict &dict); |
149 | |
150 | std::map<String, DictValue>::const_iterator begin() const; |
151 | |
152 | std::map<String, DictValue>::const_iterator end() const; |
153 | }; |
154 | |
155 | //! @} |
156 | CV__DNN_INLINE_NS_END |
157 | } |
158 | } |
159 | |
160 | #endif |
161 | |