Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sstream>
4#include <cstdlib>
5#include <cstdint>
6#include <iomanip>
7#include <chrono>
8#include <format>
9#include <string>
10#include <fstream>
11#include <filesystem>
12#include <functional>
13
14#include "native.h"
15#include <asio.hpp>
16
17#include "logo.hpp"
18
19#include <spdlog/spdlog.h>
20#include <absl/container/flat_hash_map.h>
21#include <absl/container/flat_hash_set.h>
22
23#define STRINGIFY(x) #x
24#define TOSTRING(x) STRINGIFY(x)
25
26namespace dcn::utils
27{
28 std::string loadBuildTimestamp(const std::filesystem::path & path);
29
30 std::string currentTimestamp();
31
41 asio::awaitable<void> watchdog(std::chrono::steady_clock::time_point& deadline);
42
43 asio::awaitable<void> ensureOnStrand(const asio::strand<asio::io_context::executor_type> & strand);
44
45 std::string escapeSolSrcQuotes(const std::string& json);
46
47 template<class DataT, class ChildT, template<typename...> class ChildListT>
48 std::vector<std::string> topologicalSort(
49 const absl::flat_hash_map<std::string, DataT> & data,
50 std::function<ChildListT<ChildT>(const DataT &)> get_childreen,
51 std::function<std::string(const ChildT &)> get_child_name)
52 {
53 std::vector<std::string> sorted;
54 absl::flat_hash_set<std::string> visited;
55 absl::flat_hash_set<std::string> on_stack;
56
57 std::function<void(const std::string&)> visit = [&](const std::string& name) {
58 if (visited.contains(name)) return;
59 if (on_stack.contains(name)) {
60 spdlog::error(std::format("Cycle detected involving : {}", name));
61 throw std::runtime_error("Cyclic dependency");
62 }
63
64 on_stack.insert(name);
65
66 auto it = data.find(name);
67 if (it == data.end()) {
68 spdlog::warn(std::format("Missing dependency: {}", name));
69 on_stack.erase(name);
70 return;
71 }
72
73 std::string child_name;
74 for (const auto& child : get_childreen(it->second))
75 {
77 if(child_name.empty()) continue;
79 }
80
81 visited.insert(name);
82 on_stack.erase(name);
83 sorted.push_back(name);
84 };
85
86 // perform the sort
87 for (const auto& [name, _] : data) {
88 visit(name);
89 }
90
91 return sorted;
92 }
93
94}
Definition logo.hpp:6
std::string currentTimestamp()
Definition utils.cpp:14
std::vector< std::string > topologicalSort(const absl::flat_hash_map< std::string, DataT > &data, std::function< ChildListT< ChildT >(const DataT &)> get_childreen, std::function< std::string(const ChildT &)> get_child_name)
Definition utils.hpp:48
asio::awaitable< void > watchdog(std::chrono::steady_clock::time_point &deadline)
Suspends the coroutine until the given deadline is reached.
Definition utils.cpp:24
asio::awaitable< void > ensureOnStrand(const asio::strand< asio::io_context::executor_type > &strand)
Definition utils.cpp:37
std::string escapeSolSrcQuotes(const std::string &json)
Definition utils.cpp:46
std::string loadBuildTimestamp(const std::filesystem::path &path)
Definition utils.cpp:5
nlohmann::json json
Definition parser.hpp:4