dict.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include <opencv2/core.hpp>
  42. #include <map>
  43. #include <ostream>
  44. #include <opencv2/dnn/dnn.hpp>
  45. #ifndef OPENCV_DNN_DNN_DICT_HPP
  46. #define OPENCV_DNN_DNN_DICT_HPP
  47. namespace cv {
  48. namespace dnn {
  49. CV__DNN_INLINE_NS_BEGIN
  50. //! @addtogroup dnn
  51. //! @{
  52. /** @brief This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64.
  53. * @todo Maybe int64 is useless because double type exactly stores at least 2^52 integers.
  54. */
  55. struct CV_EXPORTS_W DictValue
  56. {
  57. DictValue(const DictValue &r);
  58. DictValue(bool i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i ? 1 : 0; } //!< Constructs integer scalar
  59. DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
  60. CV_WRAP DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
  61. DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar
  62. CV_WRAP DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
  63. CV_WRAP DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
  64. DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload
  65. template<typename TypeIter>
  66. static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array
  67. template<typename TypeIter>
  68. static DictValue arrayReal(TypeIter begin, int size); //!< Constructs floating point array
  69. template<typename TypeIter>
  70. static DictValue arrayString(TypeIter begin, int size); //!< Constructs array of strings
  71. template<typename T>
  72. T get(int idx = -1) const; //!< Tries to convert array element with specified index to requested type and returns its.
  73. int size() const;
  74. CV_WRAP bool isInt() const;
  75. CV_WRAP bool isString() const;
  76. CV_WRAP bool isReal() const;
  77. CV_WRAP int getIntValue(int idx = -1) const;
  78. CV_WRAP double getRealValue(int idx = -1) const;
  79. CV_WRAP String getStringValue(int idx = -1) const;
  80. DictValue &operator=(const DictValue &r);
  81. friend std::ostream &operator<<(std::ostream &stream, const DictValue &dictv);
  82. ~DictValue();
  83. private:
  84. Param type;
  85. union
  86. {
  87. AutoBuffer<int64, 1> *pi;
  88. AutoBuffer<double, 1> *pd;
  89. AutoBuffer<String, 1> *ps;
  90. void *pv;
  91. };
  92. DictValue(Param _type, void *_p) : type(_type), pv(_p) {}
  93. void release();
  94. };
  95. /** @brief This class implements name-value dictionary, values are instances of DictValue. */
  96. class CV_EXPORTS Dict
  97. {
  98. typedef std::map<String, DictValue> _Dict;
  99. _Dict dict;
  100. public:
  101. //! Checks a presence of the @p key in the dictionary.
  102. bool has(const String &key) const;
  103. //! If the @p key in the dictionary then returns pointer to its value, else returns NULL.
  104. DictValue *ptr(const String &key);
  105. /** @overload */
  106. const DictValue *ptr(const String &key) const;
  107. //! If the @p key in the dictionary then returns its value, else an error will be generated.
  108. const DictValue &get(const String &key) const;
  109. /** @overload */
  110. template <typename T>
  111. T get(const String &key) const;
  112. //! If the @p key in the dictionary then returns its value, else returns @p defaultValue.
  113. template <typename T>
  114. T get(const String &key, const T &defaultValue) const;
  115. //! Sets new @p value for the @p key, or adds new key-value pair into the dictionary.
  116. template<typename T>
  117. const T &set(const String &key, const T &value);
  118. //! Erase @p key from the dictionary.
  119. void erase(const String &key);
  120. friend std::ostream &operator<<(std::ostream &stream, const Dict &dict);
  121. std::map<String, DictValue>::const_iterator begin() const;
  122. std::map<String, DictValue>::const_iterator end() const;
  123. };
  124. //! @}
  125. CV__DNN_INLINE_NS_END
  126. }
  127. }
  128. #endif