dict.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_EXPERIMENTAL_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(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
  59. CV_WRAP DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
  60. DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar
  61. CV_WRAP DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
  62. CV_WRAP DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
  63. DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload
  64. template<typename TypeIter>
  65. static DictValue arrayInt(TypeIter begin, int size); //!< Constructs integer array
  66. template<typename TypeIter>
  67. static DictValue arrayReal(TypeIter begin, int size); //!< Constructs floating point array
  68. template<typename TypeIter>
  69. static DictValue arrayString(TypeIter begin, int size); //!< Constructs array of strings
  70. template<typename T>
  71. T get(int idx = -1) const; //!< Tries to convert array element with specified index to requested type and returns its.
  72. int size() const;
  73. CV_WRAP bool isInt() const;
  74. CV_WRAP bool isString() const;
  75. CV_WRAP bool isReal() const;
  76. CV_WRAP int getIntValue(int idx = -1) const;
  77. CV_WRAP double getRealValue(int idx = -1) const;
  78. CV_WRAP String getStringValue(int idx = -1) const;
  79. DictValue &operator=(const DictValue &r);
  80. friend std::ostream &operator<<(std::ostream &stream, const DictValue &dictv);
  81. ~DictValue();
  82. private:
  83. int type;
  84. union
  85. {
  86. AutoBuffer<int64, 1> *pi;
  87. AutoBuffer<double, 1> *pd;
  88. AutoBuffer<String, 1> *ps;
  89. void *pv;
  90. };
  91. DictValue(int _type, void *_p) : type(_type), pv(_p) {}
  92. void release();
  93. };
  94. /** @brief This class implements name-value dictionary, values are instances of DictValue. */
  95. class CV_EXPORTS Dict
  96. {
  97. typedef std::map<String, DictValue> _Dict;
  98. _Dict dict;
  99. public:
  100. //! Checks a presence of the @p key in the dictionary.
  101. bool has(const String &key) const;
  102. //! If the @p key in the dictionary then returns pointer to its value, else returns NULL.
  103. DictValue *ptr(const String &key);
  104. /** @overload */
  105. const DictValue *ptr(const String &key) const;
  106. //! If the @p key in the dictionary then returns its value, else an error will be generated.
  107. const DictValue &get(const String &key) const;
  108. /** @overload */
  109. template <typename T>
  110. T get(const String &key) const;
  111. //! If the @p key in the dictionary then returns its value, else returns @p defaultValue.
  112. template <typename T>
  113. T get(const String &key, const T &defaultValue) const;
  114. //! Sets new @p value for the @p key, or adds new key-value pair into the dictionary.
  115. template<typename T>
  116. const T &set(const String &key, const T &value);
  117. friend std::ostream &operator<<(std::ostream &stream, const Dict &dict);
  118. };
  119. //! @}
  120. CV__DNN_EXPERIMENTAL_NS_END
  121. }
  122. }
  123. #endif