Lazy initialisation
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
#include <iostream>
#include <map>
#include <mutex> // for call_once
#include <numeric>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <mutex>
#include <thread>
using TSalary = long; // might be replaced into decimal, fixed point type...
class CompanyDatabase {
public:
struct EmployeeRecord {
std::string _name;
std::string _surname;
std::string _city;
TSalary _salary{ 0 };
};
public:
size_t MaxEntries() const { return s_table.size(); }
static constexpr size_t InvalidID = 666;
EmployeeRecord FetchRecord(int id) const {
std::cout << "Fetching record: " << id << '\n';
return s_table.at(id);
}
private:
static inline const std::vector<EmployeeRecord> s_table{
{ "John", "Doe", "Cracow", 120 },
{ "Kate", "Doe", "Cracow", 80 },
{ "Linda", "Doe", "Warsaw", 140 },
{ "Marc", "Doe", "Warsaw", 100 }
};
};
class Employee {
public:
explicit Employee(size_t id, const CompanyDatabase& db) : _id(id), _db(&db) { }
std::string Name() const { MakeSureWereReady(); return _rec->_name; }
std::string Surname() const { MakeSureWereReady(); return _rec->_surname; }
std::string City() const { MakeSureWereReady(); return _rec->_city; }
TSalary Salary() const { MakeSureWereReady(); return _rec->_salary; }
friend std::ostream& operator<<(std::ostream& os, const Employee& em)
{
Enter to Rename, Shift+Enter to Preview