<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jesyspa</id>
	<title>The Battle for Wesnoth Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.wesnoth.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Jesyspa"/>
	<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/Special:Contributions/Jesyspa"/>
	<updated>2026-04-14T00:03:01Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=HackingWesnoth&amp;diff=47619</id>
		<title>HackingWesnoth</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=HackingWesnoth&amp;diff=47619"/>
		<updated>2012-10-20T11:03:26Z</updated>

		<summary type="html">&lt;p&gt;Jesyspa: /* C++ Quiz */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is designed to be a guide for aspiring Wesnoth programmers on what they need to do to be able to productively contribute to the Wesnoth code base.&lt;br /&gt;
&lt;br /&gt;
Wesnoth is written in C++, a large and complicated language. There are many C++ programmers, but they tend to have widely varying levels of skills and styles. This page is designed to be a guide as to the skillset needed to contribute to Wesnoth.&lt;br /&gt;
&lt;br /&gt;
== C++ Quiz ==&lt;br /&gt;
&lt;br /&gt;
Below is a short C++ quiz that assesses your skills in areas of C++ that are used a lot in Wesnoth. If you know all the answers to the questions, you are probably ready to start working on the Wesnoth code base. If you know most of the answers to the questions, you can probably work on the Wesnoth code base with some oversight from the senior developers. There are good C++ guides for both knowledgeable programmers (such as [http://www.icce.rug.nl/documents/cplusplus/ The C++ Annotations]) and beginners (such as one of the books [http://stackoverflow.com/q/388242/559931 suggested here]).&lt;br /&gt;
&lt;br /&gt;
# What is a virtual destructor? Why is it needed?&lt;br /&gt;
# What does the standard class template &amp;lt;tt&amp;gt;auto_ptr&amp;lt;/tt&amp;gt; do?  What is its purpose?&lt;br /&gt;
# What is a vector, and why would you use it?&lt;br /&gt;
# What is a map, and why would you use it?&lt;br /&gt;
# What are the differences between a reference and a pointer? When should each be used?&lt;br /&gt;
# What is an iterator, and when is it used?&lt;br /&gt;
# What are the memory areas in a C++ program, and what is the purpose of each?&lt;br /&gt;
# When should a member function be &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt;?  What effect does this have?&lt;br /&gt;
# What is a copy constructor, and what is an assignment operator? When must you define them in a class?&lt;br /&gt;
&lt;br /&gt;
== Wesnoth Coding Style ==&lt;br /&gt;
&lt;br /&gt;
=== Guideline 1: Don't prematurely optimize ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;When I was a new and inexperienced coder, I read the following rather famous saying: &amp;quot;Premature optimization is the root of all evil.&amp;quot; -- Donald Knuth.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While I wouldn't then -- and would be hesitant to now -- dispute the opinion of Donald Knuth on any programming topic, this absolute statement confused me. Premature optimization certainly doesn't sound like a wise thing to do, but it sounds somewhat obscure to be the root of all problems in programming. Had Knuth written this phrase after spending too long correcting problems caused by a programmer who was hell-bent on optimization at every turn?&lt;br /&gt;
&lt;br /&gt;
Now I realize that Knuth was correct. Time and time again programming problems spring from developers making their code too complicated because they are trying to optimize unnecessarily.&lt;br /&gt;
&lt;br /&gt;
By the [http://en.wikipedia.org/wiki/Pareto_principle Pareto Principle] we know that a minority of the code will occupy a majority of the running time. That is, perhaps 90% of the running time of the program will be spend executing just 5% of the code. It is thus generally unnecessary to apply many optimizations at all to most of the program. One can just write the code in a way that is the most readable and maintainable and forget about optimization altogether most of the time.&lt;br /&gt;
&lt;br /&gt;
Remember also, C++ compilers are good, and smart, and they can execute code fast. Most code will run fast enough, unless you go out of your way to make it run slowly.&lt;br /&gt;
&lt;br /&gt;
So, the first rule of Wesnoth programming is that unless you have a very good reason to think you need to optimize, do whatever is simplest and most maintainable.&lt;br /&gt;
&lt;br /&gt;
Many programmers seem to have an obsession with using the control C++ gives them to do all sorts of crazy things. Like make their own memory management routines, or write their own containers, and so forth. Simply, don't. It's possible to write good C++ code very fast using the standard components supplied as part of the language.&lt;br /&gt;
&lt;br /&gt;
This leads us to our next topic...&lt;br /&gt;
&lt;br /&gt;
=== Guideline 2: Know the Standard containers and use them ===&lt;br /&gt;
&lt;br /&gt;
One of the best things about C++ is that the standard supplies some great containers. Always prefer to use them first to store data. That is,&lt;br /&gt;
&lt;br /&gt;
* prefer to use std::vector to store dynamic arrays&lt;br /&gt;
* prefer to use std::string to store strings (we also provide t_string for a translatable string -- it's based on std::basic_string, which std::string is also based on)&lt;br /&gt;
* prefer to use std::map to store key/value pairs&lt;br /&gt;
* prefer to use std::set to store sets of items (roughly equivalent to the mathematical concept of a set).&lt;br /&gt;
&lt;br /&gt;
There are other C++ containers, and you should know about all of them. A good source of documentation on the portion of the C++ standard library known as the Standard Template Library (STL) which contains the standard containers can be found [http://www.sgi.com/tech/stl/ here].&lt;br /&gt;
&lt;br /&gt;
By using the standard containers, you should almost never have to manually allocate and delete memory yourself by hand. The containers will automatically allocate and deallocate memory for you. This leads us to our next guideline...&lt;br /&gt;
&lt;br /&gt;
=== Guideline 3: Use the resource acquisition is initialization (RAII) idiom ===&lt;br /&gt;
&lt;br /&gt;
In C, you might write some code like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void myfunction()&lt;br /&gt;
{&lt;br /&gt;
    char* mystring = (char*)malloc(n);&lt;br /&gt;
    /*some code, which manipulates mystring*/&lt;br /&gt;
    ...&lt;br /&gt;
    free(mystring);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code works, but is error prone. What if the code in the middle exited using a return statement, or a longjmp statement, or in C++, threw an exception? The memory allocated would never be cleaned up, and we would have a memory leak.&lt;br /&gt;
&lt;br /&gt;
Sure, you're a good programmer, and you probably make sure you're very careful about freeing the string. But what about when this function grows to be 100 lines long (and functions have a way of doing that), and then another programmer who isn't that familiar with the function is trying to fix a critical bug as quickly as they can, that requires adding an early return statement to this function? What about when someone is trying to read the code to scan for memory leaks? Once they see the malloc at the top, they will have to carefully read the entire function, to make sure it's all okay.&lt;br /&gt;
&lt;br /&gt;
Now if we had this C++ version instead,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void myfunction()&lt;br /&gt;
{&lt;br /&gt;
    std::string mystring(n,'x');&lt;br /&gt;
    /*some code, which manipulates mystring*/&lt;br /&gt;
} //mystring is destroyed and memory automatically released&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
None of these problems can possibly happen. mystring will ''always'' be released at the end of the function.&lt;br /&gt;
&lt;br /&gt;
So, this just re-iterates how important C++ containers are, right? Well, yes, but it also shows us that we should always avoid code written like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void myfunction()&lt;br /&gt;
{&lt;br /&gt;
    ...allocate resources...&lt;br /&gt;
    ...use resources&lt;br /&gt;
    ...release resources...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you have code like this, you should put the resources in a class that will manage them and release them at the end of the function. If there is no class like that, then you should write one. This goes for all resources: memory, files, threads, and so forth.&lt;br /&gt;
&lt;br /&gt;
It might sound like a silly mistake that we're protecting against here, one that an experienced programmer wouldn't make. However, almost all programming bugs are due to 'silly mistakes'. This leads us to our next item...&lt;br /&gt;
&lt;br /&gt;
=== Guideline 4: Lean on the compiler as heavily as you can ===&lt;br /&gt;
&lt;br /&gt;
Humans make mistakes. Even very stupid mistakes. Computers are as dumb as toast, but they are at least very very consistent.&lt;br /&gt;
&lt;br /&gt;
For Wesnoth, we try to write code that will make the compiler guard against mistakes as much as possible. The use of RAII as described above is one example of this: writing the code so the compiler and language rules will guarantee it's correct.&lt;br /&gt;
&lt;br /&gt;
There are some more ways to do this. One big way is to make all code const correct. That is, always define something as 'const' when it shouldn't change its value. If you have a pointer that is meant to be read only, define it as a const pointer. Then if the code is later changed so that your pointer changes the value, the compiler will produce an error.&lt;br /&gt;
&lt;br /&gt;
Another way is to avoid implicit conversions between objects, and always use the C++-style casts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[CodingStandards]]&lt;br /&gt;
* [[PatchSubmissionGuidelines]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Jesyspa</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.wesnoth.org/index.php?title=CodingStandards&amp;diff=47618</id>
		<title>CodingStandards</title>
		<link rel="alternate" type="text/html" href="https://wiki.wesnoth.org/index.php?title=CodingStandards&amp;diff=47618"/>
		<updated>2012-10-20T10:48:30Z</updated>

		<summary type="html">&lt;p&gt;Jesyspa: /* Use the const keyword */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Wesnoth uses modern/advanced C++ that is portable to modern C++ compilers targeting various commonly used platforms.&lt;br /&gt;
&lt;br /&gt;
== Formatting ==&lt;br /&gt;
&lt;br /&gt;
When working on C++ for Wesnoth, indent your code with a tab character. After fully indenting, if you still need to line up the text with a specific character on the line above, you may further align it using space characters.&lt;br /&gt;
&lt;br /&gt;
You may use long lines.&lt;br /&gt;
&lt;br /&gt;
== Evil things to avoid ==&lt;br /&gt;
&lt;br /&gt;
=== Avoid implicit conversions ===&lt;br /&gt;
&lt;br /&gt;
Make all constructors which only take one argument that is of a different type to the class &amp;lt;tt&amp;gt;explicit&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Do not use &amp;lt;tt&amp;gt;operator T()&amp;lt;/tt&amp;gt; (where &amp;lt;tt&amp;gt;T&amp;lt;/tt&amp;gt; is a type) to allow an implicit conversion to a different type. For example:&lt;br /&gt;
&lt;br /&gt;
 t_string(const std::string&amp;amp;);&lt;br /&gt;
&lt;br /&gt;
This can cause many situations where a temporary t_string is implicitly created and then gets destroyed unexpectedly.&lt;br /&gt;
&lt;br /&gt;
=== Do not declare class data members as non-private ===&lt;br /&gt;
&lt;br /&gt;
It's okay to have a ''struct'' with only public members, if that's what you want.&lt;br /&gt;
&lt;br /&gt;
However, once something is a ''class'' with private data members, do not add public (or even protected) data members to the class. Doing this breaks encapsulation and can cause all kinds of confusing and evil things to happen.&lt;br /&gt;
&lt;br /&gt;
== Naming ==&lt;br /&gt;
&lt;br /&gt;
=== End non-public class data members with an underscore ===&lt;br /&gt;
&lt;br /&gt;
All non-public data members of classes should have their names terminated with an underscore, to show that they are a class member. This makes for more readable code, once one is familiar with the convention.&lt;br /&gt;
&lt;br /&gt;
== Idioms ==&lt;br /&gt;
&lt;br /&gt;
=== Use references when a value may not be NULL ===&lt;br /&gt;
&lt;br /&gt;
If a value passed to a function can never be &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt;, use a reference instead of a pointer. For example:&lt;br /&gt;
&lt;br /&gt;
 void my_function(T&amp;amp; obj);&lt;br /&gt;
&lt;br /&gt;
rather than&lt;br /&gt;
&lt;br /&gt;
 void my_function(T* obj);&lt;br /&gt;
&lt;br /&gt;
This more clearly shows prospective users of the function that &amp;lt;tt&amp;gt;obj&amp;lt;/tt&amp;gt; may never be &amp;lt;tt&amp;gt;NULL&amp;lt;/tt&amp;gt;, without them having to consult documentation or the implementation of the function.&lt;br /&gt;
&lt;br /&gt;
=== Use the const keyword ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; keyword in C++ allows interfaces to more clearly specify how they treat objects. &lt;br /&gt;
Always use &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; when you are not going to modify an object. For example:&lt;br /&gt;
&lt;br /&gt;
 void my_function(const T&amp;amp; obj);&lt;br /&gt;
&lt;br /&gt;
This shows to the caller that &amp;lt;tt&amp;gt;obj&amp;lt;/tt&amp;gt; will not be modified. If &amp;lt;tt&amp;gt;my_function()&amp;lt;/tt&amp;gt; may modify &amp;lt;tt&amp;gt;obj&amp;lt;/tt&amp;gt;, then use the following instead:&lt;br /&gt;
&lt;br /&gt;
 void my_function(T&amp;amp; obj);&lt;br /&gt;
&lt;br /&gt;
Likewise, if a variable is not changed after initialization, make it &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt;, and mark member functions as &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; if they do not modify their object.&lt;br /&gt;
&lt;br /&gt;
=== Know the behavior of const references when types differ ===&lt;br /&gt;
&lt;br /&gt;
If you assign something to a &amp;lt;tt&amp;gt;const&amp;lt;/tt&amp;gt; reference of a different type, if necessary (if the type is different but there is a conversion) the compiler will create a temporary and guarantee it lasts for the lifetime of the reference. So&lt;br /&gt;
&lt;br /&gt;
 char c = 0;&lt;br /&gt;
 const int&amp;amp; i = c;&lt;br /&gt;
 c = 5;&lt;br /&gt;
&lt;br /&gt;
will result in c == 5 and i == 0, which may not be what you expect.&lt;br /&gt;
&lt;br /&gt;
=== Write exception-safe code ===&lt;br /&gt;
&lt;br /&gt;
Wesnoth code should be exception-safe, even if you do not use exceptions directly. That is, you should be able to assume that an exception is thrown almost anywhere from within the code, with well-defined results (i.e. no resource leaks).&lt;br /&gt;
&lt;br /&gt;
Code that uses a pattern like the following is bad:&lt;br /&gt;
&lt;br /&gt;
 {&lt;br /&gt;
     SDL_Surface* image = IMG_Load(&amp;quot;image.bmp&amp;quot;);&lt;br /&gt;
     ...some code, which uses 'image'...&lt;br /&gt;
     SDL_FreeSurface(image);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The code may throw an exception, and &amp;lt;tt&amp;gt;image&amp;lt;/tt&amp;gt; will never be freed. Instead, use wrapper objects which free the object in their destructor.&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;tt&amp;gt;SDL_Surface&amp;lt;/tt&amp;gt; objects, the &amp;lt;tt&amp;gt;surface&amp;lt;/tt&amp;gt; type is used throughout the Wesnoth source code to achieve this purpose. So you could rewrite the above code as follows:&lt;br /&gt;
&lt;br /&gt;
 {&lt;br /&gt;
     surface image(IMG_Load(&amp;quot;image.bmp&amp;quot;));&lt;br /&gt;
     ...some code, which uses 'image'...&lt;br /&gt;
 } ''the image is automatically freed here when 'image' is destroyed&lt;br /&gt;
&lt;br /&gt;
Instead of allocating memory directly using &amp;lt;tt&amp;gt;new[]&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;malloc()&amp;lt;/tt&amp;gt;, use language-provided containers, such as vector.&lt;br /&gt;
&lt;br /&gt;
=== Do not use sprintf ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;sprintf()&amp;lt;/tt&amp;gt; function does not check whether or not it is writing past the end of the space allocated.  This is a security problem if someone other than the person running the game can cause &amp;lt;tt&amp;gt;sprintf()&amp;lt;/tt&amp;gt; to write very long strings. In Wesnoth, this untrusted data could come potentially from other players in a multiplayer game, or from downloaded add-ons. Instead you should use &amp;lt;tt&amp;gt;snprintf()&amp;lt;/tt&amp;gt; with the second argument being the &amp;lt;tt&amp;gt;sizeof&amp;lt;/tt&amp;gt; of the buffer that will hold the result.&lt;br /&gt;
&lt;br /&gt;
== Standard C++ to avoid ==&lt;br /&gt;
&lt;br /&gt;
=== Do not use wstring ===&lt;br /&gt;
&lt;br /&gt;
The standard C++ &amp;lt;tt&amp;gt;std::wstring&amp;lt;/tt&amp;gt; class (defined as a &amp;lt;tt&amp;gt;std::basic_string&amp;lt; wchar_t &amp;gt;&amp;lt;/tt&amp;gt;) does not exist in some platforms supported by Wesnoth. Use &amp;lt;tt&amp;gt;wide_string&amp;lt;/tt&amp;gt; instead (defined in &amp;lt;tt&amp;gt;language.hpp&amp;lt;/tt&amp;gt;). The &amp;lt;tt&amp;gt;wide_string&amp;lt;/tt&amp;gt; type is actually defined as &amp;lt;tt&amp;gt;std::vector&amp;lt; wchar_t &amp;gt;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Do not use 0 when you mean NULL ===&lt;br /&gt;
&lt;br /&gt;
Several Wesnoth developers, including Dave, find the number 0 to be very ambiguous when used in a non-numeric context. In keeping with the precedent that has already been established in the Wesnoth source code, you should avoid using literal zero for initializing and/or comparing null pointers.&lt;br /&gt;
&lt;br /&gt;
== C legacy to be avoided ==&lt;br /&gt;
&lt;br /&gt;
=== Use util::array instead of C-style Arrays ===&lt;br /&gt;
&lt;br /&gt;
C-style arrays are very efficient, but their interface is ugly. Use &amp;lt;tt&amp;gt;util::array&amp;lt;/tt&amp;gt; defined in &amp;lt;tt&amp;gt;array.hpp&amp;lt;/tt&amp;gt; instead. It is a wrapper for an array which has a C++ container-style interface. If you need to, extend it to make it fit your needs.&lt;br /&gt;
&lt;br /&gt;
=== Do not use C-style casts ===&lt;br /&gt;
&lt;br /&gt;
The following code,&lt;br /&gt;
&lt;br /&gt;
 if(i-&amp;gt;second.side() == (size_t)player_number_) {&lt;br /&gt;
&lt;br /&gt;
is considered bad practice in C++ since a C-style cast is overpowered -- if types change around it could end up casting away constness, or performing an implementation-defined data reinterpretation (basically a C-style cast is a compiler-generated combination of &amp;lt;tt&amp;gt;static_cast&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;reinterpret_cast&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;const_cast&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Good programming style is to use the least powerful tool available that does what you want. For example:&lt;br /&gt;
&lt;br /&gt;
 if(i-&amp;gt;second.side() == static_cast&amp;lt;size_t&amp;gt;(player_number_)) {&lt;br /&gt;
&lt;br /&gt;
Alternatively, a constructor call may be used for non-built-in types.&lt;br /&gt;
&lt;br /&gt;
''Note: there may be some obscure cases where a C-style cast is desirable, such as converting a pointer to an integer type of unspecified size.''&lt;br /&gt;
&lt;br /&gt;
=== Do not use #define for constants ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;#&amp;lt;/nowiki&amp;gt;define foo X&amp;lt;/tt&amp;gt; is not a typesafe approach to define constants. Instead, you can something like the following (in an anonymous namespace) to achieve the same goal in a typesafe fashion.&lt;br /&gt;
&lt;br /&gt;
 namespace {&lt;br /&gt;
     const T foo = X;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Documentation ==&lt;br /&gt;
&lt;br /&gt;
=== Document config preconditions and postconditions ===&lt;br /&gt;
&lt;br /&gt;
In the Wesnoth code you will commonly encounter a data container type known as &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt;, which contains hierarchical string data (such as WML contents or game settings). The tagged ''children'' of the &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; object and their string ''attributes'' are arranged in an ordered and mapped format, internally implemented using the C++ STL.&lt;br /&gt;
&lt;br /&gt;
Because &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; data is utilized in so many ways and places, it can be difficult to track across the scope of the entire program. Thus, you should document all public functions that take/return &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; objects, specifying content expectations and updating any related entries in the [[ReferenceWML]] wiki pages. In particular, if your function requires a &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; parameter, specify where/how the &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; object should be created. This will be a great help to any future coders who need to call or modify your function.&lt;br /&gt;
&lt;br /&gt;
=== Doxygen ===&lt;br /&gt;
&lt;br /&gt;
See [[Doxygen]] for tips on how to comment the code, so that Doxygen can nicely document it.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
&lt;br /&gt;
* [[HackingWesnoth]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Jesyspa</name></author>
		
	</entry>
</feed>