Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | // Copyright 2019-2022 Cambridge Quantum Computing | |||
2 | // | |||
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 | #include "OpJsonFactory.hpp" | |||
16 | ||||
17 | #include "OpType/OpTypeInfo.hpp" | |||
18 | #include "Ops/Op.hpp" | |||
19 | #include "Utils/Json.hpp" | |||
20 | ||||
21 | namespace tket { | |||
22 | 65 | std::map<OpType, OpJsonFactory::JsonConstruct>& OpJsonFactory::c_methods_() { | ||
23 | static std::unique_ptr<std::map<OpType, OpJsonFactory::JsonConstruct>> | |||
24 | methods = | |||
25 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 64 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
65 | std::make_unique<std::map<OpType, OpJsonFactory::JsonConstruct>>(); | |
26 | 65 | return *methods; | ||
27 | } | |||
28 | 39 | std::map<OpType, OpJsonFactory::JsonProduce>& OpJsonFactory::p_methods_() { | ||
29 | static std::unique_ptr<std::map<OpType, OpJsonFactory::JsonProduce>> methods = | |||
30 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
39 | std::make_unique<std::map<OpType, OpJsonFactory::JsonProduce>>(); | |
31 | 39 | return *methods; | ||
32 | } | |||
33 | ||||
34 | 13 | bool OpJsonFactory::register_method( | ||
35 | const OpType& type, JsonConstruct create_method, | |||
36 | JsonProduce produce_method) { | |||
37 |
4/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
|
1/2✓ Decision 'true' taken 13 times.
✗ Decision 'false' not taken.
|
13 | if (auto it = c_methods_().find(type); it == c_methods_().end()) { |
38 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | c_methods_()[type] = create_method; | |
39 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | p_methods_()[type] = produce_method; | |
40 | 13 | return true; | ||
41 | } | |||
42 | ✗ | return false; | ||
43 | } | |||
44 | ||||
45 | 13 | Op_ptr OpJsonFactory::from_json(const nlohmann::json& j) { | ||
46 |
2/4✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 | const auto type = j.at("type").get<OpType>(); | |
47 |
4/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
|
1/2✓ Decision 'true' taken 13 times.
✗ Decision 'false' not taken.
|
13 | if (auto it = c_methods_().find(type); it != c_methods_().end()) { |
48 |
1/2✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | return it->second(j); | |
49 | } | |||
50 | ✗ | throw JsonError( | ||
51 |
0/1? Decision couldn't be analyzed.
|
✗ | "No from_json conversion for type " + optypeinfo().at(type).name); | |
52 | } | |||
53 | ||||
54 | 13 | nlohmann::json OpJsonFactory::to_json(const Op_ptr& op) { | ||
55 | 13 | const OpType& type = op->get_type(); | ||
56 |
4/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 13 times.
✗ Branch 12 not taken.
|
1/2✓ Decision 'true' taken 13 times.
✗ Decision 'false' not taken.
|
13 | if (auto it = p_methods_().find(type); it != p_methods_().end()) { |
57 |
1/2✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 | return it->second(op); | |
58 | } | |||
59 | ✗ | throw JsonError( | ||
60 | ✗ | "No to_json conversion registered for type: " + | ||
61 | ✗ | optypeinfo().at(type).name); | ||
62 | } | |||
63 | ||||
64 | } // namespace tket | |||
65 |