Remove sqlite dependency from pkgmgr_parser_db (#167)
[platform/core/appfw/pkgmgr-info.git] / src / common / parcel / parcelable_factory.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics 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 #include "parcelable_factory.hh"
18
19 #include <parcel.hh>
20
21 #include "abstract_parcelable.hh"
22 #include "appinfo_parcelable.hh"
23 #include "certinfo_parcelable.hh"
24 #include "depinfo_parcelable.hh"
25 #include "filter_parcelable.hh"
26 #include "create_db_parcelable.hh"
27 #include "pkginfo_parcelable.hh"
28 #include "query_parcelable.hh"
29 #include "result_parcelable.hh"
30 #include "command_parcelable.hh"
31
32 namespace pkgmgr_common {
33 namespace parcel {
34
35 ParcelableFactory::ParcelableFactory() {}
36 ParcelableFactory::~ParcelableFactory() {}
37
38 ParcelableFactory& ParcelableFactory::GetInst() {
39   static ParcelableFactory instance;
40   return instance;
41 }
42
43 std::unique_ptr<AbstractParcelable> ParcelableFactory::CreateParcel(
44     const unsigned char* data, int size) {
45   int tmp;
46   std::unique_ptr<AbstractParcelable> res = nullptr;
47   tizen_base::Parcel parcel(data, size);
48
49   parcel.ReadInt32(&tmp);
50   parcel.ResetReader();
51   ParcelableType type = static_cast<ParcelableType>(tmp);
52
53   switch (type) {
54     case ParcelableType::AppInfo:
55       res = std::make_unique<AppInfoParcelable>();
56       parcel.ReadParcelable(res.get());
57       break;
58     case ParcelableType::CertInfo:
59       res = std::make_unique<CertInfoParcelable>();
60       parcel.ReadParcelable(res.get());
61       break;
62     case ParcelableType::Filter:
63       res = std::make_unique<FilterParcelable>();
64       parcel.ReadParcelable(res.get());
65       break;
66     case ParcelableType::PkgInfo:
67       res = std::make_unique<PkgInfoParcelable>();
68       parcel.ReadParcelable(res.get());
69       break;
70     case ParcelableType::DepInfo:
71       res = std::make_unique<DepInfoParcelable>();
72       parcel.ReadParcelable(res.get());
73       break;
74     case ParcelableType::Query:
75       res = std::make_unique<QueryParcelable>();
76       parcel.ReadParcelable(res.get());
77       break;
78     case ParcelableType::Result:
79       res = std::make_unique<ResultParcelable>();
80       parcel.ReadParcelable(res.get());
81       break;
82     case ParcelableType::Command:
83       res = std::make_unique<CommandParcelable>();
84       parcel.ReadParcelable(res.get());
85       break;
86     case ParcelableType::CreateDB:
87       res = std::make_unique<CreateDBParcelable>();
88       parcel.ReadParcelable(res.get());
89       break;
90     case ParcelableType::Unknown:
91       res = std::make_unique<AbstractParcelable>();
92       parcel.ReadParcelable(res.get());
93       break;
94     default:
95       break;
96   }
97   return res;
98 }
99
100 }  // namespace parcel
101 }  // namespace pkgmgr_common