[offtopic] user-defined raw string literals
Hi,
I have some idea related to the usage of UDLs in Boost.Geometry library
but the way I'd like to handle it is AFAIK not supported.
A little introduction first. There are many text-based languages of
defining data. One of them is WKT
(http://en.wikipedia.org/wiki/Well-known_text) which allows to define
vector data e.g. a polygon like this:
|POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))|
So my thought was: why not use C++11 UDLs for this purpose? There are
operators in the WKT language so I should probably use string literal,
put WKT in it and be happy with it. The use case could look like this:
|auto ||polygon = "POLYGON((30 10,40 40,20 40,10 20,30 10))|"_wkt;
Ok so now the tricky part. It should be parsed in compile-time, the type
of the geometry retrieved from the string literal and proper type
returned (bg::polygon<> in this case).
Unfortunately string literals can't be handled as raw literals, e.g.
using the variadic-template version of operator"":
template
Hi Adam, On 2014-01-30 17:25, Adam Wulkiewicz wrote:
Ok so now the tricky part. It should be parsed in compile-time, the type of the geometry retrieved from the string literal and proper type returned (bg::polygon<> in this case). Unfortunately string literals can't be handled as raw literals, e.g. using the variadic-template version of operator"":
template
void operator "" () Why? Is there a reason for this? If we had the ability to use the compile-time version with string literals it would be possible to implement various compile-time parsers, not only for the mentioned WKT.
So what do you think about it?
I don't know the reason for the string literal limitation, but I have a workaround for it. You can use the MPLLIBS_STRING macro from Metaparse - not in Boost yet. (see http://abel.web.elte.hu/mpllibs/metaparse/manual.html#the-input-of-the-parse... and http://abel.web.elte.hu/mpllibs/metaparse/MPLLIBS_STRING.html). Using that you can parse string literals at compile-time. Metaparse itself is a library supporting the creation of parsers for DSLs that are parsed at compile time. You might be able to create an interface like this using that: auto polygon = WKT("POLYGON((30 10,40 40,20 40,10 20,30 10))"); auto point = WKT("POINT(30 10)"); // ... where WKT is a macro expanding to a template metaprogram-based parser invocation. If you are interested in trying it out, there is a tutorial which helps you getting started: https://github.com/sabel83/metaparse_tutorial#metaparse-tutorial. Regards, Ábel
Hi Abel, Abel Sinkovics wrote:
On 2014-01-30 17:25, Adam Wulkiewicz wrote:
Ok so now the tricky part. It should be parsed in compile-time, the type of the geometry retrieved from the string literal and proper type returned (bg::polygon<> in this case). Unfortunately string literals can't be handled as raw literals, e.g. using the variadic-template version of operator"":
template
void operator "" () Why? Is there a reason for this? If we had the ability to use the compile-time version with string literals it would be possible to implement various compile-time parsers, not only for the mentioned WKT.
So what do you think about it?
I don't know the reason for the string literal limitation,
AFAIU it would be difficult to implement consistent behavior because string literals are concatenated by the preprocessor automatically.
but I have a workaround for it. You can use the MPLLIBS_STRING macro from Metaparse - not in Boost yet. (see http://abel.web.elte.hu/mpllibs/metaparse/manual.html#the-input-of-the-parse... and http://abel.web.elte.hu/mpllibs/metaparse/MPLLIBS_STRING.html). Using that you can parse string literals at compile-time.
Metaparse itself is a library supporting the creation of parsers for DSLs that are parsed at compile time. You might be able to create an interface like this using that:
auto polygon = WKT("POLYGON((30 10,40 40,20 40,10 20,30 10))"); auto point = WKT("POINT(30 10)"); // ...
where WKT is a macro expanding to a template metaprogram-based parser invocation. If you are interested in trying it out, there is a tutorial which helps you getting started: https://github.com/sabel83/metaparse_tutorial#metaparse-tutorial.
Thanks for the info. However using macros isn't exactly the thing I wanted to do because it would be less readable: auto polygon = BOOST_GEOMETRY_WKT("POLYGON((30 10,40 40,20 40,10 20,30 10))"); vs auto polygon = "POLYGON((30 10,40 40,20 40,10 20,30 10))"_wkt; but I'll for sure check Metaparse out in a free time. I may let you know when it's done. Even if it wasn't released with Geometry you'd be able to put it as an example in the docs. Regards, Adam
participants (2)
-
Abel Sinkovics
-
Adam Wulkiewicz