 And I was walking through Germany when I came past this, and at the same time I was writing this code. And this is a DOM in German. So DOM, XML, yeah, okay. It's very much German. Excellent, okay. Yes, welcome to this presentation. We're going to talk about a program of friendly XML in C++. C++ 11, you need to have some new features of the C++ language to use the features that I'm going to talk about. And what we're going to talk about is a way of writing C++ that you can have a safe writing of XML so that you're sure that you are writing the valid XML, which is kind of a problem at the moment. So ODF is a XML that you all know, and here's a small fragment of XML from ODF. This is going to be our example in this presentation. So what we're doing here is we make a date style, which we'll call a minimum year, and it will show the year with a less than sign, the front of it, and it should be bold. So we're going to, this is a very simple thing we want to do. And here you can see the action in calc. Yeah, so now in this presentation we're going to be writing this fragment using C++ code. The ODF specification has something to say about this element. Number of date style. Well, it can be used in automatic styles, it can have a number of attributes, it can have a number of child elements. This is what the programmers are reading when they're writing the software. This is your navigable, and it's derived from a schema, the relaxNG schema, which we use to specify the ODF file format. Who of you has ever seen the relaxNG? Two guys. Three guys. Okay, yeah. So what are we doing here? We are defining something called number date style, which is this element, and this element can have, for example, style tech properties, but they're optional, and it can have a number of text elements, which is also optional. But what's not optional is one or more of any date, and any date is defined somewhere else, and this can be year or month components of a way to style a date. So this kind of XML defines how the ODF XML is written. So what can you do with relaxNG? A number of things. So you define what the element names are, you can define what the attribute names are, the nesting, which is allowed, so a head has to go into an HTML, and a body has to go after a head, and a DC date, which is part of ODF, has to be a valid date string, so the data type is defined, and for attributes also, this show has to be fully available, for example. All of these things are specified in the relaxNG. So you say, oh, nice, we have a file, which is computer-readable, which you can use to validate our document, and that's what we do. We have validators, they use the relaxNG, and they check if everything is okay. Well, let's try to write out this example, which we had. Here it's a very simple API for writing XML. So write start element, and then we have a string with the tq tag, then another element, we write the attribute, we write the start element, then we write out this form here, and then we close the elements. I wrote here the nine errors. Can anybody spot these one of them? Any errors, yes? Yes, I'm missing an element, exactly. However, this will compile, right? You'll only see this form at runtime. Another error, the nine error, eight left. Ah, I'm using font weight, heavy, but it should be bold. Okay, these are all the errors. If I would run this code, I would get this. So I misspelled this element name. I'm using the wrong namespace here. This is not escaped. This is wrong nesting. It should go behind, not inside. This is a missing element, not my year. It has to be there. So nine errors in just several lines of code. Wow, and the compiler just takes it. It likes it, no problem. Now, if I translate this to a little Office API, it has some signature checks. So then I only get six errors left because I'm using these constants here, so I can't mistype the element name. That's a win, so that's good. There are still seven, six errors left in only six lines of code. Wow, that's pretty, pretty bad. So, how can we improve this? Here is another way in which we could write the XML. It's a special syntax. This is the element you want to write, and this is the C++. Well, it looks a bit crazy, I guess. It looks almost the same as the XML because we are using operator overloading. But what you see here is number date style, and here is also number date style. Here is a style name with a string to it. Text properties is defined, number text, and here I'm sending this one character as a little string into the stream. So this is a different way of writing our C++ code which will actually validate at compiler time that I'm following the ODF specification. Now, let's compare this to the new office code if it would be correct, right? So the example I gave previously and six errors in it, now I made it correct, and then it looks like this. So it's very long. To be honest, I find it quite ugly, and this is what you would have with Leading just as we had on the previous slide. Other questions so far because there's a lot of code fragments here, so if you have any questions, just feel free to ask. So now I'm going to go into a bit of how this actually works. Why? How can I write this like this? Here's a more simple example. In HTML, with just the body element, okay, that's developed, but I want to keep the example simple. This is the C++ code. So first, we have defined somewhere in an include header an HTML object. And inside of this type, there is the actual tag name, so the namespace and the string HTML. And the same for this body here. So I create a sync, and the sync has to be for an HTML document. This type is also defined in the header, and it passed an output stream into it. Then this is the start of the line, and here at the semicolon is the end, so I indented it to look like XML. So I start with the sync, then the HTML object goes into the sync, and the body goes into the sync, then the header stream goes in, and we close the body and we close the HTML. Now, each of these steps, if I write them out, I get something like this. So I'm starting with the HTML sync, then if I put the HTML into the sync, I get a second sync, which is an HTML sync. And the HTML sync will only accept things that are allowed in there. So it will accept the body, but if I try to put an HTML inside the HTML, I will get a part error. So I go through all these things, and in the end I end up again with the HTML document sync. So each step here changes the sync. It puts type information in the sync so that the compiler will check that all the nesting is valid. So here I put the XML writing online, and this is more a traditional way of writing the operators for smaller than and greater than. And this is the implementation for it. So what you see here, the operator gets the HTML doc sync, it gets the tag, which is the HTML, and in the back it just starts the element and it returns a new sync. Then this HTML sync goes in here and it gets a body tag, and it writes a start element and returns another sync. Then this one accepts text. I'm using just a character string here, but you could be using something else. That writes the characters, and then also returns a body sync again. The body sync can be closed with the tag again, with the bigger than, with the smaller than operator. So it calls an element, and this is sync base. So this is the actual implementation. You don't need to see this normally, right? So this is just to explain how this works. How can you use this? Well, Lipple Office has its own function, sorry, it has its own class for doing the exporting, and you can wrap that in this class. And here are the functions which you're currently using in Lipple Office. So the start element, the end element, and attribute and character writing. These are all inline functions, so this is just compile time features. When you're compiling, you're checking the tags, the types, and you're making sure that you're not writing invalid ODF, but the binary will be still the same. Here is a larger example that shows how to use functions. I'll go through this slowly. Still no questions? No, they're still actually still looking at the slides, so that's good, nobody's really far asleep yet. Good. It's not going to get more complicated than this one, so this is what I'm going to do. I want to write a function to create a document, so I want a Q DOM, well, a DOM document out of this. I'm passing a title and a list of paragraphs, which are just strings. So I create a builder, I do an HTML start, add a title, and then I pass a string in, close it, close the head, start the body, and then here I do create paragraphs, and I pass the list of paragraphs. What this does is it holds this factor here, which accepts a sink, anything, and loops over the strings and puts them one by one in a paragraph element. It's just this P here, because I'm using the namespace XHTML, I can just write P, and the text goes in, I close the element again, and then at the end I have a sink, which I return. So the sink then goes into this body, and the body is closed, the HTML is closed, and I've created HTML options. So this is not writing to a string, this is creating a document live, so the same way of writing can be used in two ways. Okay, well, that's basically it. I didn't go into, well, I could have put a lot more interesting details of the implementation in here with a lot of cool C++ 11 features, but I thought that would be a bit too much. There's already a lot of Coach comments in here right now. So, yeah, what do we have now? We have a very small, simple class header library. It's called Blazium, and when you convert your RNG to header file, you can have XML validation at compile time. So instead of having to call the ambulance, we actually have a seatbelt, which prevents invalid ODF at compile time. So, this would really let people office developers sleep better at night, I think. Yeah, prevention is better than the cure, and be strict in what you create is the thought behind all this. I have a longer write-down of what I'm explaining here on my blog, and the code is on GitHub. If somebody's interested in reading it or asking me questions about how it works in more detail or just general or make plans on how to start using this in live or office, I'd be very open to do this. That's it. Very close. Yeah, they can switch to the, which sign is left. Oh, 15 minutes left. Wow, you can go through this. Well, let's not do that. Hello. Shall I also explain how to actually convert the RNG to header file? Yes? Okay. So, I don't have slides for that, but I will get down. I thought that F11 would be maximizing. Does anybody know what the shortcut is to maximize this in Windows? Or can help me with the Danish translation? I want to maximize it. Full scan. The zoom on the right side, the zoom on the right side. Ah, okay. Yes, okay, thanks. So, yeah, we did this part. So now, the metaprogramming part of the whole thing. Yes. Here, we are defining. So this is using Qt as an example. So that's slightly different from what people office has, but you can think of what the equivalent would be. I am defining a stream for the HTML namespace and this is a constant at compile time. So that's some optimization to make the code fast. So this is the namespace, HTML, head, and you can think of the other tags yourself. Then I create HTML tag. So this is a type. It's an instantiation of a template. So there's an XML tag template, which gets a type, which is Q string in this case. But again, people office, you would put a different type here and I pass it these constants. So then this type has all the information that needs to write out the XML Q name for it. Yeah, so now we are going to define the types of the tags because a Q name is just that. It's a namespace and a string, but it's not necessarily always the same type. So HTML would have an HTML tag, but it's a separate concept from the actual tag itself. So the HTML type has a lot of attributes. In this case, it's a tuple and the tuples are things that you can use at compile time in C++11. So it has an ID tag, which is just a string ID and it has a class tag in this example. And here I define the image type, which has a tag image and it has allowed attributes ID and class again because they're allowed for all HTML elements and it has required attributes because it needs a source and it needs an alt. So if you try to compile a code which doesn't define these, it will not compile. Now, we have to also define what the child elements are. An HTML can have a head element inside and it can have a body element inside. I'm also defining this as a subtype within this structure inside of this... Yeah, this is a template and I can instantiate the template. So the syntax is not really that important unless you're really into C++ meta-programming. But what will happen is that at compile time, the compiler can figure out that in the HTML I can have a head or a body. And for the image, nothing is allowed inside so it's an empty tuple here. Yeah, so Blazion, it takes these structs to generate the right overloaded operators. Yeah, that's how you would write a header file from an RNG file or from a DTP file or an XML schema, it doesn't matter. But you just need to know what these rules are and write them down as a header file. Yes, this is the same example we had before and future enhancements. So I wrote this code on holiday and next holiday I will continue it. So for now this is the state. Let's see. Yeah, Xpath is a very popular way of writing and by doing more operator overloading I can get to C++ code which looks a lot like Xpath which has actually syntax checking at compile time as well. But that's like thinking ahead quite far. So that's a bit, that's quite crazy and difficult. So the future enhancements are really like what kind of crazy things can we do as well. If you want to learn how to program like this I learned it from the, simply from the Starstool C++ book for the C++ 11 implementation version of the language. And yeah, are there questions about this? Yes. What do you think of that specifically in the library? What do you think of that specifically in the library? I don't see a variable template. Yes, well, yes. Then we go into the implementation of the header library. Okay. So we go one level deeper still. Okay. That's why I'm using those. I'm using variator templates. Yes. Okay. Yeah. And a few, yeah. That's the main thing. That's the most important thing. Yeah. And that's actually required to do this trick. So this year, this kind of initialization of the funtum that's actually C++ 11 also. What other C++ 11 features are here? No, that's it. Yeah. The tuples of variable length as well. Yes, you're right. Yeah. The tuples are in the background using the variator templates. Yeah. Yeah. Does the inducing came about because of the voltage in which you used this by the office? Does the main version of studio have that? The last time I looked at it, it didn't have that. It didn't have variator templates? The last time I looked at it, it does now. The 13 version is enough for what we use in the office. Okay. Well, it is okay. Yeah. That's the least. So let's take a poll after all the difficult stuff. Who thinks it's a good idea for your office? Ornand. Ornand. Well, I just take one crazy guy to make it, right? He's probably overloading other things. There's we got it. I should have asked who thinks it's a bad idea? Ah, so one against zero. Excellent. Oh, it's great giving talks in the morning after a party. Yeah, yeah, yeah. You can really create a poll. Well, how rainy was the holiday anyway? Yeah, no, it was during the day I was walking and dreaming about all this kind of stuff. And then in the evening, I had to get a phone every day. It's not really a lot of problems, actually. I'm not sure he was happy. Ah, no. Yeah, she was as well. It didn't really take that much time. It's just once you come up with this crazy idea of overloading the operators, the rest just goes naturally. There's one thing, sometimes there's an extension attribute, which is not in the schema, but still had to be written out. How are you doing this? Sometimes there's a new feature, which is not in the standard yet, but it has to be prototype. Yes, then you use a schema with the extension. All right. Yeah. Yeah, yeah, yeah. So the office has the changes. So they would use the extended relax engine to create the habit. Obviously. Yeah. Yeah. So similar code like this is already in Kalina, but without this nice syntax. So this two years ago, I did something like this for Kalina. And then I was using this syntax, which is still quite ugly to write the code. So yeah, this is much nicer, at least in my opinion. Yeah. If you really want to have challenging way of looking at C metaprogramming, this is also an interesting write up. I'm not going to go through this now, but you can translate Haskell directly to C++. And that's actually, Haskell is very short, very compact, and C++ is much more syntax, but it means the same thing. So if you know a bit of Haskell, it's quite easy to write this stuff. And for implementing actually also checking the order of the elements, this is going to be necessary because checking the order, so checking that the hat comes before the body is not, that's not checked right now. So that's one of the possible extensions here, but that's going to take some, well, some complicated hacking. Well, a very long holiday, maybe, maybe pension. Well, that's it. Thank you for watching.