1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PREFERENCES_H 17 #define PREFERENCES_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "preferences_observer.h" 25 #include "preferences_value.h" 26 27 namespace OHOS { 28 namespace NativePreferences { 29 class Preferences { 30 public: ~Preferences()31 virtual ~Preferences() 32 { 33 } 34 35 static const unsigned int MAX_KEY_LENGTH = 80; 36 37 static const unsigned int MAX_VALUE_LENGTH = 8 * 1024; 38 39 virtual PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) = 0; 40 41 virtual int Put(const std::string &key, const PreferencesValue &value) = 0; 42 43 virtual int GetInt(const std::string &key, const int &defValue = {}) = 0; 44 45 virtual std::string GetString(const std::string &key, const std::string &defValue = {}) = 0; 46 47 virtual bool GetBool(const std::string &key, const bool &defValue = {}) = 0; 48 49 virtual float GetFloat(const std::string &key, const float &defValue = {}) = 0; 50 51 virtual double GetDouble(const std::string &key, const double &defValue = {}) = 0; 52 53 virtual int64_t GetLong(const std::string &key, const int64_t &defValue = {}) = 0; 54 55 virtual std::map<std::string, PreferencesValue> GetAll() = 0; 56 57 virtual bool HasKey(const std::string &key) = 0; 58 59 virtual int PutInt(const std::string &key, int value) = 0; 60 61 virtual int PutString(const std::string &key, const std::string &value) = 0; 62 63 virtual int PutBool(const std::string &key, bool value) = 0; 64 65 virtual int PutLong(const std::string &key, int64_t value) = 0; 66 67 virtual int PutFloat(const std::string &key, float value) = 0; 68 69 virtual int PutDouble(const std::string &key, double value) = 0; 70 71 virtual int Delete(const std::string &key) = 0; 72 73 virtual int Clear() = 0; 74 75 virtual void Flush() = 0; 76 77 virtual int FlushSync() = 0; 78 79 virtual void RegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) = 0; 80 81 virtual void UnRegisterObserver(std::shared_ptr<PreferencesObserver> preferencesObserver) = 0; 82 }; 83 } // End of namespace NativePreferences 84 } // End of namespace OHOS 85 #endif // End of #ifndef PREFERENCES_H 86