base_object.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef INFER_SERVER_BASE_OBJECT_H_
  21. #define INFER_SERVER_BASE_OBJECT_H_
  22. #include <map>
  23. #include <string>
  24. #include <type_traits>
  25. #include <utility>
  26. #include <vector>
  27. #include "any.h"
  28. namespace infer_server {
  29. /**
  30. * @brief Params object base class
  31. */
  32. class BaseObject {
  33. public:
  34. /**
  35. * @brief Set param
  36. *
  37. * @tparam T Param type
  38. * @param param_name Unique param name
  39. * @param param Param value
  40. */
  41. template <typename T>
  42. void SetParams(const std::string& param_name, T&& param) {
  43. params_[param_name] = std::forward<T>(param);
  44. }
  45. /**
  46. * @brief Set params
  47. *
  48. * @tparam T Param type
  49. * @tparam Args Param_name:param values' type
  50. * @param param_name Unique param name
  51. * @param param Param value
  52. * @param args Param_name:param values
  53. */
  54. template <typename T, typename... Args>
  55. void SetParams(const std::string& param_name, T&& param, Args&&... args) {
  56. params_[param_name] = std::forward<T>(param);
  57. SetParams(std::forward<Args>(args)...);
  58. }
  59. /**
  60. * @brief Get param
  61. *
  62. * @tparam T Param type
  63. * @param param_name Param name
  64. * @return T Specified param
  65. */
  66. template <typename T>
  67. auto GetParam(const std::string& param_name) const -> typename std::remove_reference<T>::type {
  68. return any_cast<typename std::remove_reference<T>::type>(params_.at(param_name));
  69. }
  70. /**
  71. * @brief Pop param
  72. *
  73. * @tparam T Param type
  74. * @param param_name Param name
  75. * @return T&& Specified param
  76. */
  77. template <typename T>
  78. T PopParam(const std::string& param_name) {
  79. T tmp = any_cast<typename std::remove_reference<T>::type>(params_.at(param_name));
  80. params_.erase(param_name);
  81. return tmp;
  82. }
  83. /**
  84. * @brief Check if object has specified param
  85. *
  86. * @param param_name Param name
  87. * @retval true Have specified param
  88. * @retval false Donot have specified param
  89. */
  90. bool HaveParam(const std::string& param_name) noexcept { return params_.find(param_name) != params_.cend(); }
  91. /**
  92. * @brief Get name of stored params
  93. *
  94. * @return std::vector<std::string> Name of stored params
  95. */
  96. std::vector<std::string> GetParamNames() noexcept {
  97. std::vector<std::string> names;
  98. for (auto& p : params_) {
  99. names.emplace_back(p.first);
  100. }
  101. return names;
  102. }
  103. /**
  104. * @brief Copy params form another object
  105. *
  106. * @param other another object
  107. */
  108. void CopyParamsFrom(const BaseObject& other) noexcept { params_ = other.params_; }
  109. /**
  110. * @brief Destroy the BaseObject object
  111. */
  112. virtual ~BaseObject() = default;
  113. protected:
  114. std::map<std::string, any> params_;
  115. };
  116. } // namespace infer_server
  117. #endif // INFER_SERVER_BASE_OBJECT_H_