1 /** 2 * Copyright 2019 Huawei Technologies Co., Ltd 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 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CORE_USER_DATA_H_ 18 #define MINDSPORE_CORE_USER_DATA_H_ 19 20 #include <string> 21 #include <memory> 22 #include <map> 23 24 namespace mindspore { 25 class UserData { 26 public: 27 template <typename T> set(const std::string & key,const std::shared_ptr<T> & value)28 void set(const std::string &key, const std::shared_ptr<T> &value) { 29 if (value == nullptr) { 30 data_.erase(key); 31 } else { 32 data_.insert_or_assign(key, value); 33 } 34 } 35 36 template <typename T> get(const std::string & key)37 std::shared_ptr<T> get(const std::string &key) const { 38 auto iter = data_.find(key); 39 if (iter == data_.end()) { 40 return nullptr; 41 } 42 return std::static_pointer_cast<T>(iter->second); 43 } 44 has(const std::string & key)45 bool has(const std::string &key) const { return data_.find(key) != data_.end(); } 46 47 private: 48 std::map<std::string, std::shared_ptr<void>> data_; 49 }; 50 } // namespace mindspore 51 52 #endif // MINDSPORE_CORE_USER_DATA_H_ 53