Open3D (C++ API)  0.18.0
IJsonConvertibleIO.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <algorithm>
11 #include <iterator>
12 #include <string>
13 
15 
16 namespace open3d {
17 namespace io {
18 
22 bool ReadIJsonConvertible(const std::string &filename,
23  utility::IJsonConvertible &object);
24 
28 bool WriteIJsonConvertible(const std::string &filename,
29  const utility::IJsonConvertible &object);
30 
31 bool ReadIJsonConvertibleFromJSON(const std::string &filename,
32  utility::IJsonConvertible &object);
33 
34 bool WriteIJsonConvertibleToJSON(const std::string &filename,
35  const utility::IJsonConvertible &object);
36 
37 bool ReadIJsonConvertibleFromJSONString(const std::string &json_string,
38  utility::IJsonConvertible &object);
39 
40 bool WriteIJsonConvertibleToJSONString(std::string &json_string,
41  const utility::IJsonConvertible &object);
42 
60 #define DECLARE_STRINGIFY_ENUM(ENUM_TYPE) \
61  std::string enum_to_string(ENUM_TYPE e); \
62  void enum_from_string(const std::string &str, ENUM_TYPE &e); \
63  inline auto format_as(ENUM_TYPE e) { return enum_to_string(e); }
64 
65 #define STRINGIFY_ENUM(ENUM_TYPE, ...) \
66  std::string enum_to_string(ENUM_TYPE e) { \
67  static_assert(std::is_enum<ENUM_TYPE>::value, \
68  #ENUM_TYPE " must be an enum!"); \
69  static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
70  auto it = std::find_if( \
71  std::begin(m), std::end(m), \
72  [e](const std::pair<ENUM_TYPE, std::string> &es_pair) \
73  -> bool { return es_pair.first == e; }); \
74  return ((it != std::end(m)) ? it : std::begin(m))->second; \
75  } \
76  void enum_from_string(const std::string &str, ENUM_TYPE &e) { \
77  static_assert(std::is_enum<ENUM_TYPE>::value, \
78  #ENUM_TYPE " must be an enum!"); \
79  static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
80  auto it = std::find_if( \
81  std::begin(m), std::end(m), \
82  [&str](const std::pair<ENUM_TYPE, std::string> &es_pair) \
83  -> bool { return es_pair.second == str; }); \
84  e = ((it != std::end(m)) ? it : std::begin(m))->first; \
85  utility::LogDebug("{} -> {}", str, e); \
86  }
87 
88 } // namespace io
89 } // namespace open3d
bool ReadIJsonConvertibleFromJSON(const std::string &filename, utility::IJsonConvertible &object)
Definition: FileJSON.cpp:54
bool WriteIJsonConvertibleToJSONString(std::string &json_string, const utility::IJsonConvertible &object)
Definition: FileJSON.cpp:86
bool ReadIJsonConvertibleFromJSONString(const std::string &json_string, utility::IJsonConvertible &object)
Definition: FileJSON.cpp:80
bool ReadIJsonConvertible(const std::string &filename, utility::IJsonConvertible &object)
Definition: IJsonConvertibleIO.cpp:39
bool WriteIJsonConvertible(const std::string &filename, const utility::IJsonConvertible &object)
Definition: IJsonConvertibleIO.cpp:60
bool WriteIJsonConvertibleToJSON(const std::string &filename, const utility::IJsonConvertible &object)
Definition: FileJSON.cpp:67
Definition: PinholeCameraIntrinsic.cpp:16