1 /*
2  * Copyright (c) 2024 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 CONTENT_READER_FACTORY_H
17 #define CONTENT_READER_FACTORY_H
18 
19 #include <unordered_map>
20 
21 #include "content_reader.h"
22 #include "singleton.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class ContentReaderFactory : public OHOS::DelayedRefSingleton<ContentReaderFactory> {
27 public:
28     void Register(int8_t version, std::shared_ptr<ContentReader> reader);
29     std::shared_ptr<ContentReader> Get(int8_t version);
30 
31 private:
32     std::unordered_map<int8_t, std::shared_ptr<ContentReader>> readerMap_;
33 };
34 
35 class ReaderRegister {
36 public:
ReaderRegister(int8_t version,std::shared_ptr<ContentReader> reader)37     ReaderRegister(int8_t version, std::shared_ptr<ContentReader> reader)
38     {
39         ContentReaderFactory::GetInstance().Register(version, reader);
40     }
~ReaderRegister()41     ~ReaderRegister(){};
42 };
43 
44 #define REGISTER_CONTENT_READER(version, className) \
45 class Register##className { \
46 private: \
47     static const ReaderRegister g_readerGegister; \
48 }; \
49 const ReaderRegister Register##className::g_readerGegister(version, std::make_shared<className>())
50 
51 } // HiviewDFX
52 } // OHOS
53 
54 #endif // CONTENT_READER_FACTORY_H