-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.cpp
More file actions
121 lines (104 loc) · 4.06 KB
/
Copy pathmain.cpp
File metadata and controls
121 lines (104 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2014-2015 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <ctime> // std::time
#include <iostream>
#include <random> // std::mt19937
#include <vector>
#include "../database.hpp" // for the databases
#include "../error.hpp" // for error handling
#include "../generator.hpp" // for the generator classes
#include "../string_id.hpp" // for the string_id
// namespace alias
namespace sid = foonathan::string_id;
int main() try
{
// this allows using the literal
using namespace sid::literals;
// create database to store the strings in
// it must stay valid as long as each string_id using it
sid::default_database database;
//=== string_id usage ===//
// create an id
sid::string_id sid("Test0815", database);
std::cout << "Hash code " << sid.hash_code() << " belongs to string \"" << sid.string() << "\"\n";
// Output (Database supports retrieving): Hash code 16741300784925887095 belongs to string "Test0815"
// Output (Database doesn't): Hash code 16741300784925887095 belongs to string "string_id database disabled"
sid::string_id a("Hello", database), b("World", database);
// compare two ids
std::cout << std::boolalpha << (a == b) << '\n';
// Output: false
// compare id with constant
#if FOONATHAN_STRING_ID_HAS_LITERAL
std::cout << (a == "Hello"_id) << '\n';
#else
std::cout << (a == id("Hello")) << '\n';
#endif
// Output: true
#if FOONATHAN_STRING_ID_HAS_LITERAL
// literal is compile-time
switch (b.hash_code())
{
case "Hello"_id:
std::cout << "Hello\n";
break;
case "world"_id: // case-sensitive
std::cout << "world\n";
break;
case "World"_id:
std::cout << "World\n";
break;
}
#endif
//=== generation ===//
// the prefix for all generated ids
sid::string_id prefix("entity-", database);
try
{
// a generator type appending 8 random characters to the prefix
// it uses the std::mt19937 generator for the actual generation
typedef sid::random_generator<std::mt19937, 8> generator_t;
// create a generator, seed the random number generator with the current time
generator_t generator(prefix, std::mt19937(std::int_fast32_t(std::time(nullptr))));
std::vector<sid::string_id> ids;
for (auto i = 0; i != 10; ++i)
// generate new identifier
// it is guaranteed unique and will be stored in the database of the prefix
ids.push_back(generator());
// print the name of all the ids
for (auto &id : ids)
std::cout << id.string() << '\n';
// possible generated name: entity-jXRnZAVG
}
catch (sid::generation_error &ex)
{
// the generator was unable to generate a new unique identifier (very unlikely)
std::cerr << "[ERROR] " << ex.what() << '\n';
}
try
{
// a generator appending an increasing number to the prefix
typedef sid::counter_generator generator_t;
// create a generator starting with 0, each number will be 4 digits long
generator_t generator(prefix, 0, 4);
std::vector<sid::string_id> ids;
for (auto i = 0; i != 10; ++i)
// generate new identifier
// it is guaranteed unique and will be stored in the database of the prefix
ids.push_back(generator());
// print the name of all the ids
for (auto &id : ids)
std::cout << id.string() << '\n';
// possible generated name: entity-0006
}
catch (sid::generation_error &ex)
{
// the generator was unable to generate a new unique identifier (very unlikely)
std::cerr << "[ERROR] " << ex.what() << '\n';
}
}
catch (sid::collision_error &ex)
{
// two different strings are resulting in the same hash value (very unlikely)
std::cerr << "[ERROR] " << ex.what() << '\n';
}