ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
IJsonConvertibleIO.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // LOCAL
11 #include "CV_io.h"
12 
13 // CV_CORE_LIB
14 #include <IJsonConvertible.h>
15 
16 // SYSTEM
17 #include <string>
18 
19 namespace cloudViewer {
20 namespace io {
21 
25 bool CV_IO_LIB_API
26 ReadIJsonConvertible(const std::string &filename,
28 
32 bool CV_IO_LIB_API
33 WriteIJsonConvertible(const std::string &filename,
35 
36 bool CV_IO_LIB_API
39 
41  const std::string &filename,
43 
45  const std::string &json_string,
47 
49  std::string &json_string,
51 
69 #define DECLARE_STRINGIFY_ENUM(ENUM_TYPE) \
70  std::string enum_to_string(ENUM_TYPE e); \
71  void enum_from_string(const std::string &str, ENUM_TYPE &e); \
72  inline auto format_as(ENUM_TYPE e) { return enum_to_string(e); }
73 
74 #define STRINGIFY_ENUM(ENUM_TYPE, ...) \
75  std::string enum_to_string(ENUM_TYPE e) { \
76  static_assert(std::is_enum<ENUM_TYPE>::value, \
77  #ENUM_TYPE " must be an enum!"); \
78  static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
79  auto it = std::find_if( \
80  std::begin(m), std::end(m), \
81  [e](const std::pair<ENUM_TYPE, std::string> &es_pair) \
82  -> bool { return es_pair.first == e; }); \
83  return ((it != std::end(m)) ? it : std::begin(m))->second; \
84  } \
85  void enum_from_string(const std::string &str, ENUM_TYPE &e) { \
86  static_assert(std::is_enum<ENUM_TYPE>::value, \
87  #ENUM_TYPE " must be an enum!"); \
88  static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
89  auto it = std::find_if( \
90  std::begin(m), std::end(m), \
91  [&str](const std::pair<ENUM_TYPE, std::string> &es_pair) \
92  -> bool { return es_pair.second == str; }); \
93  e = ((it != std::end(m)) ? it : std::begin(m))->first; \
94  cloudViewer::utility::LogDebug("{} -> {}", str, enum_to_string(e)); \
95  }
96 
97 } // namespace io
98 } // namespace cloudViewer
std::string filename
#define CV_IO_LIB_API
Definition: CV_io.h:15
bool ReadIJsonConvertibleFromJSONString(const std::string &json_string, cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertible(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertibleToJSONString(std::string &json_string, const cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertibleToJSON(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool ReadIJsonConvertibleFromJSON(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
bool ReadIJsonConvertible(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
Generic file read and write utility for python interface.