]> git.stg.codes - stg.git/blobdiff - include/stg/service_conf.h
Fix build on Darwin.
[stg.git] / include / stg / service_conf.h
index 473830e44393636b301efdb16cc97a1a926acf99..d5661c09d7d182f230e598ecb7ba110f72f37f3a 100644 (file)
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
-#ifndef SERVICE_CONF_H
-#define SERVICE_CONF_H
+#pragma once
 
-#include "resetable.h"
-#include "os_int.h"
+#include "splice.h"
 
 #include <string>
+#include <optional>
+#include <cstdint>
 
-struct SERVICE_CONF
+namespace STG
 {
-SERVICE_CONF()
-    : name(), comment(), cost(0), payDay(0)
-{}
-SERVICE_CONF(const std::string & n)
-    : name(n), comment(), cost(0), payDay(0)
-{}
-SERVICE_CONF(const std::string & n, double c)
-    : name(n), comment(), cost(c), payDay(0)
-{}
-SERVICE_CONF(const std::string & n, double c, unsigned p)
-    : name(n), comment(), cost(c), payDay(static_cast<uint8_t>(p))
-{}
-SERVICE_CONF(const std::string & n, double c,
-             unsigned p, const std::string & com)
-    : name(n), comment(com), cost(c), payDay(static_cast<uint8_t>(p))
-{}
-
-std::string name;
-std::string comment;
-double      cost;
-uint8_t     payDay;
-};
 
-struct SERVICE_CONF_RES
+struct ServiceConf
 {
-SERVICE_CONF_RES()
-    : name(), comment(),
-      cost(), payDay()
-{}
+    ServiceConf()
+        : cost(0), payDay(0)
+    {}
+    explicit ServiceConf(const std::string & n)
+        : name(n), cost(0), payDay(0)
+    {}
+    ServiceConf(const std::string & n, double c)
+        : name(n), cost(c), payDay(0)
+    {}
+    ServiceConf(const std::string & n, double c, unsigned p)
+        : name(n), cost(c), payDay(static_cast<uint8_t>(p))
+    {}
+    ServiceConf(const std::string & n, double c,
+                 unsigned p, const std::string & com)
+        : name(n), comment(com), cost(c), payDay(static_cast<uint8_t>(p))
+    {}
 
-SERVICE_CONF_RES & operator=(const SERVICE_CONF & conf)
-{
-name = conf.name;
-comment = conf.comment;
-cost = conf.cost;
-payDay = conf.payDay;
-return *this;
-}
+    ServiceConf(const ServiceConf&) = default;
+    ServiceConf& operator=(const ServiceConf&) = default;
+    ServiceConf(ServiceConf&&) = default;
+    ServiceConf& operator=(ServiceConf&&) = default;
 
-SERVICE_CONF GetData() const
-{
-SERVICE_CONF sc;
-sc.name = name.data();
-sc.comment = comment.data();
-sc.cost = cost.data();
-sc.payDay = payDay.data();
-return sc;
-}
+    bool operator==(const ServiceConf& rhs) const noexcept { return name == rhs.name; }
 
-RESETABLE<std::string> name;
-RESETABLE<std::string> comment;
-RESETABLE<double>      cost;
-RESETABLE<uint8_t>     payDay;
+    std::string name;
+    std::string comment;
+    double      cost;
+    uint8_t     payDay;
 };
 
-inline
-bool operator==(const SERVICE_CONF & a, const SERVICE_CONF & b)
+struct ServiceConfOpt
 {
-return a.name == b.name;
-}
+    ServiceConfOpt() = default;
+
+    explicit ServiceConfOpt(const ServiceConf& rhs)
+        : name(rhs.name), comment(rhs.comment),
+          cost(rhs.cost), payDay(rhs.payDay)
+    {}
 
-#endif //SERVICE_CONF_H
+    ServiceConfOpt(const ServiceConfOpt&) = default;
+    ServiceConfOpt& operator=(const ServiceConfOpt&) = default;
+    ServiceConfOpt(ServiceConfOpt&&) = default;
+    ServiceConfOpt& operator=(ServiceConfOpt&&) = default;
 
+    ServiceConfOpt& operator=(const ServiceConf& conf)
+    {
+        name = conf.name;
+        comment = conf.comment;
+        cost = conf.cost;
+        payDay = conf.payDay;
+        return *this;
+    }
+
+    void splice(const ServiceConfOpt& rhs)
+    {
+        STG::splice(name, rhs.name);
+        STG::splice(comment, rhs.comment);
+        STG::splice(cost, rhs.cost);
+        STG::splice(payDay, rhs.payDay);
+    }
+
+    ServiceConf get(const ServiceConf& defaultValue) const noexcept
+    {
+        ServiceConf res;
+        res.name = name.value_or(defaultValue.name);
+        res.comment = comment.value_or(defaultValue.comment);
+        res.cost = cost.value_or(defaultValue.cost);
+        res.payDay = payDay.value_or(defaultValue.payDay);
+        return res;
+    }
+
+    std::optional<std::string> name;
+    std::optional<std::string> comment;
+    std::optional<double>      cost;
+    std::optional<uint8_t>     payDay;
+};
+
+}