bbm::string_converter

We already saw that one can override the serialization/deserialization behavior by adding a toString and a static fromString method to a bsdfmodel. However, this is not limited to only bsdfmodels and any class can add serialization/deserialization support by adding these two methods.

However, in some cases one might not access to the class declaration. BBM offers an addition method for adding serialization support through a sepecialzation of the bbm::string_converter class:

template<typename T>
struct string_converter

forward decalaration

Default converter class; should be specialized. Has a fall through for types with reflection; if not it will throw a compile error when trying to call the static to/fromString methods.

Public Static Functions

template<typename = T>
static inline T fromString(const std::string &str)

convert a string to a type T

If T has a constructor_args_t, then use this to convert from a string. Otherwise, if T supports reflection AND a trivial constructor, then use reflection to set the attributes. In both cases, if T meets concepts::named then the name must match the keyword. If T supports enumerate reflection, then use this.

Note: these cases are defined in the base case for string_converter such that they can be overwritten by type specific specializations.

template<typename = T>
static inline std::string toString(const T &obj)

convert an object to a type T

If T support reflection, then convert the attributes to a string. If T also meets concepts::named, then prefix the name.

Note

Core string_converter specialization are defined in include/core/stringconvert.h. However, any method that implements a specialization should not directly include this header file, but instead include concepts/stringconvert.h.

To aid in parsing strings, bbm includes the following helper methods defined in include/util/string_util.h:

std::string bbm::string::remove_whitespace(const std::string &str)

remove the white space at the front and back of a string

std::string bbm::string::remove_brackets(const std::string &str)

remove surrounding backets

std::string bbm::string::remove_comment(const std::string &str, const std::string &comment_marker)

Remove comments from string.

std::pair<std::string, std::string> bbm::string::get_keyword(const std::string &str)

Return the keyword substring appearing an open bracket, and the arguments appearing in the brackets: e.g., keyword(arguments)

std::pair<std::string, std::string> bbm::string::split_eq(const std::string &str)

split a string of the form “key = val” in key and value. If no ‘=’, then return an empty key.

std::vector<std::string> bbm::string::split_args(const std::string &str)

Split a string based on comma’s if not surrounded by brackets.