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 <ctime>
7#include <iomanip>
8#include <chrono>
9#include <format>
10#include <stdexcept>
11#include <string>
12#include <fstream>
13#include <filesystem>
14#include <functional>
15#include <mutex>
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 template<class T>
29 inline constexpr bool always_false = false;
30
31 std::string loadBuildTimestamp(const std::filesystem::path & path);
32
33 std::string currentTimestamp();
34
35 template<class DataT, class ChildT, template<typename...> class ChildListT>
36 std::vector<std::string> topologicalSort(
37 const absl::flat_hash_map<std::string, DataT> & data,
38 std::function<ChildListT<ChildT>(const DataT &)> get_childreen,
39 std::function<std::string(const ChildT &)> get_child_name)
40 {
41 std::vector<std::string> sorted;
42 absl::flat_hash_set<std::string> visited;
43 absl::flat_hash_set<std::string> on_stack;
44
45 std::function<void(const std::string&)> visit = [&](const std::string& name) {
46 if (visited.contains(name)) return;
47 if (on_stack.contains(name)) {
48 spdlog::error(std::format("Cycle detected involving : {}", name));
49 throw std::runtime_error("Cyclic dependency");
50 }
51
52 on_stack.insert(name);
53
54 auto it = data.find(name);
55 if (it == data.end()) {
56 spdlog::warn(std::format("Missing dependency: {}", name));
57 on_stack.erase(name);
58 return;
59 }
60
61 std::string child_name;
62 for (const auto& child : get_childreen(it->second))
63 {
64 child_name = get_child_name(child);
65 if(child_name.empty()) continue;
66 visit(child_name);
67 }
68
69 visited.insert(name);
70 on_stack.erase(name);
71 sorted.push_back(name);
72 };
73
74 // perform the sort
75 for (const auto& [name, _] : data) {
76 visit(name);
77 }
78
79 return sorted;
80 }
81
82 bool equalsIgnoreCase(const std::string& a, const std::string& b);
83
84 void logException(const std::exception_ptr & exception_ptr, const std::string_view context);
85
87}
std::string name
Definition ingestion.cpp:29
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:36
void logException(const std::exception_ptr &exception_ptr, const std::string_view context)
Definition utils.cpp:48
bool isImportTraceEnabled()
Definition utils.cpp:70
bool equalsIgnoreCase(const std::string &a, const std::string &b)
Definition utils.cpp:40
constexpr bool always_false
Definition utils.hpp:29
std::string loadBuildTimestamp(const std::filesystem::path &path)
Definition utils.cpp:5