Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cargo-sqlite-json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Pawel Kubik (p.kubik@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
26 #ifndef CARGO_SQLITE_JSON_CARGO_SQLITE_JSON_HPP
27 #define CARGO_SQLITE_JSON_CARGO_SQLITE_JSON_HPP
28 
34 #include "cargo-json/fs-utils.hpp"
35 
36 namespace cargo {
37 
48 template <class Cargo>
49 void loadFromKVStoreWithJson(const std::string& kvfile,
50  const std::string& json,
51  Cargo& visitable,
52  const std::string& kvVisitableName)
53 {
54  static_assert(isVisitable<Cargo>::value, "Use CARGO_REGISTER macro");
55 
56  KVStore store(kvfile);
57  KVStore::Transaction transaction(store);
58  FromJsonVisitor fromJsonVisitor(json);
59  FromKVStoreIgnoringVisitor fromKVStoreVisitor(store, kvVisitableName);
60  visitable.accept(fromJsonVisitor);
61  visitable.accept(fromKVStoreVisitor);
62  transaction.commit();
63 }
64 
73 template <class Cargo>
74 void loadFromKVStoreWithJsonFile(const std::string& kvfile,
75  const std::string& jsonfile,
76  Cargo& visitable,
77  const std::string& kvVisitableName)
78 {
79  std::string content;
80  if (!fsutils::readFileContent(jsonfile, content)) {
81  throw CargoException("Could not load " + jsonfile);
82  }
83  try {
84  loadFromKVStoreWithJson(kvfile, content, visitable, kvVisitableName);
85  } catch (CargoException& e) {
86  throw CargoException("Error in " + jsonfile + ": " + e.what());
87  }
88 }
89 
90 } // namespace cargo
91 
94 #endif // CARGO_SQLITE_JSON_CARGO_SQLITE_JSON_HPP
Definition: from-json-visitor.hpp:43
JSON visitor.
A guard struct for thread synchronization and transaction management.
Definition: kvstore.hpp:49
JSON visitor.
Definition: kvstore.hpp:43
Visitor for saving to KVStore.
Visitor for loading from KVStore that doesn't fail on missing values.
bool readFileContent(const std::string &path, std::string &result)
Definition: fs-utils.cpp:34
Default visitor for loading from KVStore.
Helper for compile-time checking against existance of template method 'accept'.
Definition: is-visitable.hpp:49
void loadFromKVStoreWithJson(const std::string &kvfile, const std::string &json, Cargo &visitable, const std::string &kvVisitableName)
Load the visitable from KVStore with defaults given in json.
Definition: cargo-sqlite-json.hpp:49
Base class for exceptions in libcargo.
Definition: exception.hpp:35
A variant of KVStoreVisitor that ignore non-existing fields.
Definition: from-kvstore-ignoring-visitor.hpp:38
void commit()
Definition: kvstore.cpp:126
src/cargo/manager.hpp
void loadFromKVStoreWithJsonFile(const std::string &kvfile, const std::string &jsonfile, Cargo &visitable, const std::string &kvVisitableName)
Load the data from KVStore with defaults given in json file.
Definition: cargo-sqlite-json.hpp:74