DigitalCurling3  1.0.0
A curling simulation system for curling AIs
common.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2022 UEC Takeshi Ito Laboratory
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
25 
26 #ifndef DIGITALCURLING3_JSON_COMMON_HPP
27 #define DIGITALCURLING3_JSON_COMMON_HPP
28 
29 #include <optional>
30 #include <chrono>
31 #include "nlohmann/json.hpp"
32 #include "polymorphic_json.hpp"
33 
35 // std::optional
36 namespace nlohmann {
37 template <typename T>
38 struct adl_serializer<std::optional<T>> {
39  static void to_json(json & j, std::optional<T> const& opt)
40  {
41  if (opt) {
42  j = *opt;
43  } else {
44  j = nullptr;
45  }
46  }
47 
48  static void from_json(json const& j, std::optional<T> & opt)
49  {
50  if (j.is_null()) {
51  opt = std::nullopt;
52  } else {
53  opt = j.get<T>();
54  }
55  }
56 };
57 } // namespace nlohmann
59 
60 
62 // std::chrono::milliseconds
63 namespace nlohmann {
64 template <>
65 struct adl_serializer<std::chrono::milliseconds> {
66  static void to_json(json & j, std::chrono::milliseconds const& ms)
67  {
68  j = static_cast<double>(ms.count()) / 1000.0;
69  }
70 
71  static void from_json(json const& j, std::chrono::milliseconds & ms)
72  {
73  ms = std::chrono::milliseconds(
74  static_cast<std::chrono::milliseconds::rep>(
75  std::round(
76  j.get<double>() * 1000.0)));
77  }
78 };
79 } // namespace nlohmann
81 
82 #endif // DIGITALCURLING3_JSON_COMMON_HPP
polymorphic_json.hpp
多態性を持つクラスのJSON変換のための仕組みを提供します