Loading...
Searching...
No Matches
bsdf_string_convert.h
Go to the documentation of this file.
1#ifndef _BBM_BSDF_STRING_CONVERT_H_
2#define _BBM_BSDF_STRING_CONVERT_H_
3
5
6#include "bbm/bsdf.h"
7#include "bbm/bsdf_ptr.h"
8#include "bbm/aggregatebsdf.h"
9
10/************************************************************************/
11/*! \file bsdf_string_convert.h
12
13 \brief Specialized conversion between strings and bsdfs not handeled by the
14 default string convertion defined in core/stringconvert.h.
15*************************************************************************/
16
17namespace bbm {
18
19 /**********************************************************************/
20 /*! \brief converting a bsdf_ptr to and from a string
21
22 Define string_convert outside bsdf_ptr.h due to the circular relation
23 between aggregatebsdf and bsdf_ptr.
24 *********************************************************************/
25 template<typename BSDF_PTR> requires concepts::bsdf_ptr<BSDF_PTR>
26 struct string_converter<BSDF_PTR>
27 {
28 BBM_IMPORT_CONFIG( BSDF_PTR );
29
30 /********************************************************************/
31 /*! \brief toString: forward to toStrig method of bsdf_ptr
32 ********************************************************************/
33 static inline std::string toString(const BSDF_PTR& obj) { return obj.toString(); }
34
35 /********************************************************************/
36 /*! \brief fromString converts a string to a bsdf_ptr to a bsfmodel.
37
38 Strategy:
39
40 1. created a sorted named tuple where the name is the bsdfmodel, and the
41 type is the function call to the corresponding fromString<bsdfmodel>
42
43 2. perform a binary search to match the key (object name in string) to
44 the name of the bsdfmodels.
45
46 3. Once found, create the bsdfmodel and correspodning bsdf_ptr.
47
48 To do: Speed up compilation: this code for some reason compiles very
49 slowly (multiple seconds) when called for the first time in the
50 translation unit.
51 *********************************************************************/
52 static inline BSDF_PTR fromString(const std::string& str)
53 {
55
56 // create lookup at compile time as a sorted named tuple
57 auto dict = sort_named( named_cat(
58 #include "bbm_bsdfmodels.h"
59 make_named<aggregatebsdf<Config>::name>( bbm::fromString<aggregatebsdf<Config>> ) // Needs aggregatebsdf instead of aggregatemodel
60 ) );
61
62 #include "export/clear_export.h"
63
64 // get keyword
65 auto [key, value] = string::get_keyword(str);
66
67 // binary search
68 return binary_search_named(key, dict, []<size_t IDX, typename NAMED>(const std::string& key, NAMED&& named, const std::string& str) -> BSDF_PTR
69 {
70 // if not found (outside range) => throw exception
71 if constexpr (IDX == std::decay_t<NAMED>::size) throw std::invalid_argument(std::string("BBM: unrecognized bsdf_ptr type ") + key + " in: " + str);
72 else
73 {
74 // if not found (key does not match) => throw throw exception
75 if(key != std::string(std::decay_t<NAMED>::template name<IDX>)) throw std::invalid_argument(std::string("BBM: unrecognized bsdf_ptr type ") + key + " in: " + str);
76
77 // if found; call the corresponding (stored) fromString to create the BSDF_PTR.
78 else return make_bsdf_ptr(std::get<IDX>(std::forward<NAMED>(named))(str));
79 }
80 }, str);
81
82 // Done.
83 }
84 };
85
86} // end bbm namespace
87
88#endif /* _BBM_BSDF_STRING_CONVERT_H_ */
A run-time aggregation of BSDFs.
Connects a BSDF model and a BSDF.
A shared_ptr wrapper for bsdfs.
fromString support for BBM
A BSDF that is the aggregate of different BSDFs.
Definition: aggregatebsdf.h:39
Clears and defines the export macros.
Convert a string to an object and vice versa.
std::pair< std::string, std::string > get_keyword(const std::string &str)
Return the keyword substring appearing an open bracket, and the arguments appearing in the brackets: ...
Definition: string_util.h:65
Definition: aggregatebsdf.h:29
constexpr auto named_cat(T &&... t)
cat named types
Definition: named_util.h:40
constexpr named< anonymize_t< T >, NAMES... > make_named(T &&t)
Make a named of a gettable type (with size == #NAMES); renames if the type is a named container.
Definition: named.h:293
constexpr auto sort_named(NAMED &&named, PARTIAL &&partial=PARTIAL{})
sort a named tuple by name using insert-sort.
Definition: named_util.h:251
auto binary_search_named(const std::string &str, NAMED &&named, PROCESS &&process, Ts &&... context)
Run-time binary search for a matching name in a named tuple based on a string. The (index of the) fou...
Definition: named_util.h:359
T fromString(const std::string &)
fromString alias
Definition: stringconvert.h:584
decltype(auto) value(T &&t)
return the value of an attribute, or if not an attribute the object
Definition: attribute_value.h:20
bsdf_ptr< get_config< BSDFTYPE > > make_bsdf_ptr(ARGS... args)
Helper method for making a bsdf_ptr from a BSDF (new construction)
Definition: bsdf_ptr.h:180
named container
Definition: named.h:131
static BSDF_PTR fromString(const std::string &str)
fromString converts a string to a bsdf_ptr to a bsfmodel.
Definition: bsdf_string_convert.h:52
static std::string toString(const BSDF_PTR &obj)
toString: forward to toStrig method of bsdf_ptr
Definition: bsdf_string_convert.h:33
forward decalaration
Definition: stringconvert.h:47