1 /*
2  * Copyright 2020 The Android Open Source Project
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 #pragma once
18 
19 #include <optional>
20 #include <string>
21 #include <type_traits>
22 
23 namespace bluetooth {
24 namespace storage {
25 
26 // A config serializable module
27 template <typename T>
28 class Serializable {
29  public:
30   Serializable() = default;
31   virtual ~Serializable() = default;
32 
33   // GD stack code
34 
35   // Serialize to string used in GD stack
36   virtual std::string ToString() const = 0;
37   // T must implement FromString(const std::string&), otherwise, it will fail to compile
38   // Parse string from GD stack
FromString(const std::string & str)39   static std::optional<T> FromString(const std::string& str) {
40     return T::FromString(str);
41   }
42 
43   // Legacy handling
44 
45   // Serialize to string used in legacy stack config, this may not be the same as ToString()
46   virtual std::string ToLegacyConfigString() const = 0;
47   // T must implement FromLegacyConfigString(const std::string&), otherwise, it will fail to compile
48   // Parse string from legacy config
FromLegacyConfigString(const std::string & str)49   static std::optional<T> FromLegacyConfigString(const std::string& str) {
50     return T::FromLegacyConfigString(str);
51   }
52 };
53 
54 }  // namespace storage
55 }  // namespace bluetooth