1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 04:23:20 +01:00

countof with type safety

For reference see http://www.g-truc.net/post-0708.html and
http://lxr.free-electrons.com/source/include/linux/kernel.h#L54

This will provide a type-safe mechanism for counting elements of array.
If you try passing something which cannot be counted, compiler will
frown at you right away.
This commit is contained in:
Michał Janiszewski
2015-12-14 23:46:36 +01:00
parent e0195fcffd
commit 98e204552a
7 changed files with 62 additions and 16 deletions

View File

@@ -36,6 +36,7 @@ extern "C" {
#include <algorithm>
#include <set>
#include <string>
#include "../core/Util.hpp"
extern "C" {
#include "../config.h"
#include "../game.h"
@@ -844,10 +845,10 @@ std::string Network::GenerateAdvertiseKey()
static char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char key[17];
for (int i = 0; i < 16; i++) {
int hexCharIndex = util_rand() % countof(hexChars);
int hexCharIndex = util_rand() % Util::CountOf(hexChars);
key[i] = hexChars[hexCharIndex];
}
key[countof(key) - 1] = 0;
key[Util::CountOf(key) - 1] = 0;
return key;
}