Lazy initialisation
#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)
{
os << em._id << ": " << em.Name() << ", " << em.Surname() << ", " << em.City() << ", " << em.Salary() << '\n';
return os;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX