Coding Standards

Everything about development and the OpenMW source code.
Post Reply
blunted2night
Posts: 63
Joined: 28 Dec 2012, 08:29

Coding Standards

Post by blunted2night »

I wanted to get a feel for some specifics:

template meta-programming

I love to use template as compile time maps. It work I created a library that allows for something similar to C# attributes except they can be read at compile time. I also created a companion library that allows me to walk each member of a structure, and thus automate many repetitive and error prone tasks with highly optimized code. The only cost is that the data members of structure effectively need to be defined twice. Using the two together, I can create a generic operation to apply to all the members of a structure, then mark certain members with meta-data that can alter the behavior of a specific member.

conditionally compiled C++11 features

Move semantics for key data-structures can, and does in one of my branches, significantly reduce cost of operations in a nearly transparent way.

source code generation

I have written a tool over the last several years that provides an agile work flow for source to source translators.

Code: Select all


           (grammar)            (script)
              \/                   \/
 (input) -> [parse] -> {AST} -> [execute] -> (output)

You define a grammar to parse your input which produces an abstract syntax tree (AST). Once you have your AST, you run a script on it to produce your final output. The parser generator is an SLR parser generator with a context sensitive regular expression based lexer and multiple white-space handlers. The grammar syntax is similar to ANTLR and allows you to define the shape of the tree produced when matching a production.

The abstract syntax tree can store arbitrary .NET objects as values and can be saved and loaded (like an intermediate file). While the parser will only ever produce text, scripts can manipulate the tree to simplify other scripts.

The scripting language is C-ish with curly braces and semicolons. It has a syntax that allows you to search the AST using an XPath like query language. It allows you to define functions using a text template syntax so that when you call the function, the template will be expanded into the output. Within the template syntax, their are numerous shortcuts for easily producing output. Script code can create and access .NET objects if needed.

I could see creating a concise description of the NIF format, then using the tool to produce code to read/write/translate the files.

I have not publicly released the tool yet, but I plan to under a GPL license (only the tool, not the code it generates, if that is possible or a concern)
LizTail
Posts: 27
Joined: 31 Oct 2012, 21:50

Re: Coding Standards

Post by LizTail »

blunted2night wrote:I could see creating a concise description of the NIF format, then using the tool to produce code to read/write/translate the files.
That's actually kind of what we did at NifTools. We had an XML file that represents the format of NIF files, and used that to generate C++ and Python code for Niflib and the Blender importer/exporter. NifSkope actually has a different C++ library that reads the XML directly and manipulates NIF files as if they were a DOM-type thing.

Niflib was pure-compiled C++, with IO code for reading and writing the NIF files generated from the XML. This was done by an offline tool that read the XML file and inserted updated IO code between certain comments in the source and header files. But this created a static understanding of the NIF format that couldn't be generically traversed by external tools. We didn't have anything like the C++ reflection mechanism you described, so a tool that wanted to visualize the contents of a NIF file would need to call all the right get/set functions. NifSkope, on the other hand, could re-load the XML file after an edit, which let us work rapidly on reverse-engineering the file format. However, it was several orders of magnitude slower than Niflib.

In retrospect, it would have been nice to have a single C++ library that worked for everything, but the performance characteristics of the two libraries were so different that they were never merged. A more concise description with a C-type syntax would probably have worked better too.
User avatar
Zini
Posts: 5538
Joined: 06 Aug 2011, 15:16

Re: Coding Standards

Post by Zini »

template meta-programming

I love to use template as compile time maps. It work I created a library that allows for something similar to C# attributes except they can be read at compile time. I also created a companion library that allows me to walk each member of a structure, and thus automate many repetitive and error prone tasks with highly optimized code. The only cost is that the data members of structure effectively need to be defined twice. Using the two together, I can create a generic operation to apply to all the members of a structure, then mark certain members with meta-data that can alter the behavior of a specific member.
Sounds very un-C++-ish. Templates are okay, but don't let the meta-programming get out of hand.
conditionally compiled C++11 features

Move semantics for key data-structures can, and does in one of my branches, significantly reduce cost of operations in a nearly transparent way.
No C++11 (yet). And definitely not conditionally compiled. This kind of stuff is a nightmare to maintain with such a diverse selection of target platforms.

We will run a survey in the near future to determine which are the lowest compiler versions we have to consider. Then we will determine (very carefully) the least common denominator and that is what we will be using.
source code generation
...
Why would we need that? Not keen on expanding the number of required tools anyway. People (on some platforms) already have enough trouble with setting up their development environment. The last thing we need to do is making it more complex for them.
blunted2night
Posts: 63
Joined: 28 Dec 2012, 08:29

Re: Coding Standards

Post by blunted2night »

Zini wrote: Sounds very un-C++-ish. Templates are okay, but don't let the meta-programming get out of hand.
Be careful where you say that, your liable to start a war... Who doesn't want to be greeted by a 300 line long error message that tries to find the most cryptic way of telling you that a call to some function you never heard, from some function you never heard of is ambiguous.
Zini wrote: We will run a survey in the near future to determine which are the lowest compiler versions we have to consider. Then we will determine (very carefully) the least common denominator and that is what we will be using.
auto, and r-value references will give us a lot of bang for our buck. They where also some of the first bits of the new standard to become available in released compilers.

GCC: 4.4 - released in early 2009
CLang: 2.9 - released in late 2010
MSVC: 10 - released early in 2010

I only brought up move semantics because, for the scenarios I would like to use them, they can be left out without breaking the code, it just slows down.
Toc1r
Posts: 7
Joined: 07 Jan 2013, 12:48

Re: Coding Standards

Post by Toc1r »

Well, the problem with C++11 is that GCC just accepts it in the current config, while others (i.e. clang) don't and would need an extra parameter.

So at the moment we have some C++11 usage in the repo which breaks simply replacing the compiler with clang:
- a use of std::move() in components/bsa/bsa_archive.cpp:120 (btw: the std::move() isn't needed there since the parameters already are l-values)
- a use of the new optional enum-name before the identifier (that came together with the strongly typed enums) in apps/openmw/mwworld/actionequip.cpp:57

One option would be to convert the code back to C++03 (trivial in that case) but then we should also change the settings to produce a compile error in these cases.
Another option would be to allow C++11 features and adjust the settings that they work with all the usual compilers.

If C++11 is allowed, it should also be specified whether the additional libraries are allowed (since some of them aren't available everywhere) or just the new language features themselves.
blunted2night
Posts: 63
Joined: 28 Dec 2012, 08:29

Re: Coding Standards

Post by blunted2night »

Toc1r wrote: - a use of std::move() in components/bsa/bsa_archive.cpp:120 (btw: the std::move() isn't needed there since the parameters already are l-values)
Oops, I didn't mean to leave those in, though I have some optimizations of certain sorts that require either a custom sort, or allowing move semantics to avoid deep copies of structures when elements in a collection are swapped.

In the case you pointed out, I believe those moves are necessary. Unless I am missing something, a named value is always an l-value, and I want an r-value so that the std::make_pair can move construct its content, then be moved into the map, thus avoiding 4 memory allocations.
Toc1r
Posts: 7
Joined: 07 Jan 2013, 12:48

Re: Coding Standards

Post by Toc1r »

Oh, you're right... Maybe we could use Boost::move() instead? We've got Boost dependencies anyway and it also works in C++03. (Though I've never tried it ;-))
http://www.boost.org/doc/libs/1_52_0/doc/html/move.html
User avatar
psi29a
Posts: 5362
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: Coding Standards

Post by psi29a »

Post Reply