Open3D (C++ API)  0.18.0+90810a3
Namespaces | Macros | Functions
IJsonConvertibleIO.h File Reference

(90810a3 (Tue Jul 23 08:44:20 2024 -0700))

#include <algorithm>
#include <iterator>
#include <string>
#include "open3d/utility/IJsonConvertible.h"

Go to the source code of this file.

Namespaces

 open3d
 
 open3d::io
 

Macros

#define DECLARE_STRINGIFY_ENUM(ENUM_TYPE)
 
#define STRINGIFY_ENUM(ENUM_TYPE, ...)
 

Functions

bool open3d::io::ReadIJsonConvertible (const std::string &filename, utility::IJsonConvertible &object)
 
bool open3d::io::WriteIJsonConvertible (const std::string &filename, const utility::IJsonConvertible &object)
 
bool open3d::io::ReadIJsonConvertibleFromJSON (const std::string &filename, utility::IJsonConvertible &object)
 
bool open3d::io::WriteIJsonConvertibleToJSON (const std::string &filename, const utility::IJsonConvertible &object)
 
bool open3d::io::ReadIJsonConvertibleFromJSONString (const std::string &json_string, utility::IJsonConvertible &object)
 
bool open3d::io::WriteIJsonConvertibleToJSONString (std::string &json_string, const utility::IJsonConvertible &object)
 

Macro Definition Documentation

◆ DECLARE_STRINGIFY_ENUM

#define DECLARE_STRINGIFY_ENUM (   ENUM_TYPE)
Value:
std::string enum_to_string(ENUM_TYPE e); \
void enum_from_string(const std::string &str, ENUM_TYPE &e); \
inline auto format_as(ENUM_TYPE e) { return enum_to_string(e); }

String to and from enum mapping, based on https://github.com/nlohmann/json/blob/master/include/nlohmann/detail/macro_scope.hpp (MIT license) If you have an enum: enum IMAGE_FORMAT {FORMAT_PNG, FORMAT_JPG}; Use as STRINGIFY_ENUM(IMAGE_FORMAT, { {FORMAT_INVALID, nullptr}, {FORMAT_PNG, "png"}, {FORMAT_JPG, "jpg"} }) in the cpp file and DECLARE_STRINGIFY_ENUM(IMAGE_FORMAT) in the header file. This creates the functions

  • enum_to_string(const ENUM_TYPE &e) -> std::string
  • enum_from_string(const std::string &str, ENUM_TYPE &e) -> void for conversion between the enum and string. Invalid string values are mapped to the first specified option in the macro.

◆ STRINGIFY_ENUM

#define STRINGIFY_ENUM (   ENUM_TYPE,
  ... 
)
Value:
std::string enum_to_string(ENUM_TYPE e) { \
static_assert(std::is_enum<ENUM_TYPE>::value, \
#ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
auto it = std::find_if( \
std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, std::string> &es_pair) \
-> bool { return es_pair.first == e; }); \
return ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
void enum_from_string(const std::string &str, ENUM_TYPE &e) { \
static_assert(std::is_enum<ENUM_TYPE>::value, \
#ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, std::string> m[] = __VA_ARGS__; \
auto it = std::find_if( \
std::begin(m), std::end(m), \
[&str](const std::pair<ENUM_TYPE, std::string> &es_pair) \
-> bool { return es_pair.second == str; }); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
utility::LogDebug("{} -> {}", str, enum_to_string(e)); \
}
#define LogDebug(...)
Definition: Logging.h:82