Project

General

Profile

Best Practises » History » Version 3

quintus, 04/18/2021 10:33 AM

1 1 quintus
# Best Practises
2
3
This page is a list of practises usually followed in the project's code base.
4
5
## Character encoding (charset) of strings
6
7
C++ does not have a defined encoding (charset, character set) for its string types, most notably `std::string`. It is thus required to always be aware of what encoding a string is in. This project uses `std::string` as its main string type (as opposed to `std::wstring`) and ensures that the character encoding used in this strings is UTF-8. When interfacing with other software, conversion is applied as required. For example, if a programming library returns strings that are not in UTF-8, they are immediately converted to UTF-8 before storing them for later use. The Win32 API as the most prominent example uses UTF-16LE for example, thus interacting with it requires conversion from and to the UTF-8-encoded `std::string` instances used in this project.
8
9 3 quintus
Encoding conversion functions are provided in `os/encoding.hpp`.
10
11 1 quintus
Likewise, all files written by the programme are written in UTF-8, regardless of the platform. BOMs (byte order marks) are not to be used.
12
13
## Type for filesystem pathes
14
15
Use C++17's `std::filesystem::path` for dealing with pathes on the filesystem. Use `std::filesystem::u8path()` to create an instance of `std::filesystem::path` from an `std::string` encoded in UTF-8.
16
17 3 quintus
Note that it is not required to use encoding conversion functions for creating a `std::filesystem::path` object from a string in filesystem native encoding, because `std::filesystem::path`'s constructor is smart enough to realise this. As per its documentation, it:
18
19
* Assumes its argument to be in native narrow encoding (typically UTF-8 on Linux) if it is of type `char` or `std::string`,
20
* Assumes its argument to be in native wide encoding (typically UTF-16LE on Windows) if its is of type `wchar_t` or `std::wstring`.
21
22
Consequently, as long as you only pass `char`-based types to the `std::filesystem::path` constructor on Linux and `wchar_t`-based types to it on Windows, things should “just work”. Just do not pass `wchar_t`-based strings on Linux or `char`-based strings on Windows.
23
24 1 quintus
## Inclusion of STL and other namespaces
25
26
In header files, do not include the `std` namespace or any other namespace, but write it out in full. This is to prevent unexpected namespace changes on `#include`.
27
28
In implementation files, do include the `std` namespace. Include other namespaces if it is useful and adds to the readability. Inclusion of namespaces should normally be done towards the beginning of a `.cpp` file, though it might be useful to only include a namespace in a single function. Use readability as the goal for decision.
29
30
The `std::filesystem` namespace is annoying to type even with `std` included. In `.cpp` files, abbreviate it as `fs` like this:
31
32
~~~~~ c++
33
namespace fs = std::filesystem;
34
// Now you can access std::filesystem::path more
35
// easily as fs::path.
36 2 quintus
~~~~~
37
38
## System-specific conditional compilation
39 1 quintus
40 3 quintus
When implementing system-specific code, use an `#if/#elif/#else` preprocessor block. The `#else` block should always give the compilation error message "unsupported system" (by using the `#error` preprocessor directive). This eases porting of the software to new platforms, because every system-specific code section will cause a compilation error on new platforms. The porting developer can then look at that and adapt the statements as necessary one by one. 
41 2 quintus
42
Example:
43
44
~~~~~ c++
45 1 quintus
#if defined(_WIN32)
46 2 quintus
// Windows-specific code
47 3 quintus
#elif defined(__unix__)
48 2 quintus
// Unix-specific code
49
#else
50
#error Unsupported system
51
#endif
52
~~~~~
53 1 quintus
54
There is a [list of system-specific compiler macros](https://sourceforge.net/p/predef/wiki/OperatingSystems/) available.
55 3 quintus
56
## Reminding people to change something
57
58
GCC has a nice `#pragma` [directive to print warnings](https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html). This can be used if something is only implemented prelimaryly to get on with other tasks. Each time the file is compiled, it will cause a warning. Since unknown `#pragma` directives are typically ignored by other compilers with a warning, this even works on non-gcc compilers.
59
60
Example:
61
62
~~~~ c++
63
#pragma GCC warning "Do not store data in /tmp, this is only for debugging"
64
~~~~
65
66
That being said, use this trick sparingly.