dservice
Форк от valentingushchin/dservice
1#include <chrono>
2#include <iomanip>
3#include <sstream>
4#include <stdexcept>
5#include <string>
6#include <thread>
7
8#include "DService.h"
9
10DService::DService() {
11
12AddProperty(L"Version", L"ВерсияКомпоненты", [&]() {
13auto s = std::string(Version);
14return std::make_shared<variant_t>(std::move(s));
15});
16
17AddMethod(L"Message", L"Сообщить", this, &DService::message);
18AddMethod(L"CurrentDate", L"ТекущаяДата", this, &DService::currentDate);
19
20// If variant_t would be non-copy you can't use initializer list. Then:
21// std::map<long, variant_t> def_args;
22// def_args.insert({0, 5});
23// AddMethod(u"Sleep", u"Ожидать", this, &DService::sleep, std::move(def_args));
24//
25AddMethod(L"Sleep", L"Ожидать", this, &DService::sleep, {{0, 5}});
26
27/*
28// Universal property. Could store any supported by native api type.
29sample_property = std::make_shared<variant_t>();
30AddProperty(L"SampleProperty", L"ОбразецСвойства", sample_property);
31
32AddMethod(L"Add", L"Сложить", this, &DService::add);
33AddMethod(L"Assign", L"Присвоить", this, &DService::assign);
34AddMethod(L"SamplePropertyValue", L"ЗначениеСвойстваОбразца", this, &DService::samplePropertyValue);
35*/
36}
37
38std::string DService::extensionName() {
39return "Gen";
40}
41
42void DService::message(const variant_t& msg) {
43std::visit(overloaded{
44[&](const std::string& v) { AddError(ADDIN_E_INFO, extensionName(), v, false); },
45[&](const int32_t& v) { AddError(ADDIN_E_INFO, extensionName(), std::to_string(static_cast<int>(v)), false); },
46[&](const double& v) { AddError(ADDIN_E_INFO, extensionName(), std::to_string(v), false); },
47[&](const bool& v) { AddError(ADDIN_E_INFO, extensionName(), std::string(v ? u8"Истина" : u8"Ложь"), false); },
48[&](const std::tm& v) {
49std::ostringstream oss;
50oss.imbue(std::locale("ru_RU.utf8"));
51oss << std::put_time(&v, "%c");
52AddError(ADDIN_E_INFO, extensionName(), oss.str(), false);
53},
54[&](const std::vector<char>& v) {},
55[&](const std::monostate&) {}
56}, msg);
57}
58
59void DService::sleep(const variant_t& delay) {
60// It safe to get any type from variant.
61// Exceptions are handled by component API.
62std::this_thread::sleep_for(std::chrono::seconds(std::get<int32_t>(delay)));
63}
64
65variant_t DService::currentDate() {
66tm current{};
67const time_t t = time(nullptr);
68#ifdef _WINDOWS
69localtime_s(¤t, &t);
70#else
71localtime_r(&t, ¤t);
72#endif
73return current;
74}
75
76/*
77// Sample of addition method. Support both integer and string params.
78// Every exceptions derived from std::exceptions are handled by components API
79variant_t DService::add(const variant_t &a, const variant_t &b) {
80if (std::holds_alternative<int32_t>(a) && std::holds_alternative<int32_t>(b)) {
81return std::get<int32_t>(a) + std::get<int32_t>(b);
82} else if (std::holds_alternative<std::string>(a) && std::holds_alternative<std::string>(b)) {
83return std::string{std::get<std::string>(a) + std::get<std::string>(b)};
84} else {
85throw std::runtime_error(u8"Неподдерживаемые типы данных");
86}
87}
88
89// Out params support option must be enabled for this to work
90void DService::assign(variant_t &out) {
91out = true;
92}
93
94// Despite that you can return property value through method this is not recommended
95// due to unwanted data copying
96variant_t DService::samplePropertyValue() {
97return *sample_property;
98}
99*/
100