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#include <optional>
17#include <string_view>
18
19#include "logo.hpp"
20
21#include <spdlog/spdlog.h>
22#include <absl/container/flat_hash_map.h>
23#include <absl/container/flat_hash_set.h>
24
25#define STRINGIFY(x) #x
26#define TOSTRING(x) STRINGIFY(x)
27
28namespace dcn::utils
29{
30 template<class T>
31 inline constexpr bool always_false = false;
32
33 std::string loadBuildTimestamp(const std::filesystem::path & path);
34
35 std::string currentTimestamp();
36
37 std::int64_t nowMs();
38
39 template<class DataT, class ChildT, template<typename...> class ChildListT>
40 std::vector<std::string> topologicalSort(
41 const absl::flat_hash_map<std::string, DataT> & data,
42 std::function<ChildListT<ChildT>(const DataT &)> get_childreen,
43 std::function<std::string(const ChildT &)> get_child_name)
44 {
45 std::vector<std::string> sorted;
46 absl::flat_hash_set<std::string> visited;
47 absl::flat_hash_set<std::string> on_stack;
48
49 std::function<void(const std::string&)> visit = [&](const std::string& name) {
50 if (visited.contains(name)) return;
51 if (on_stack.contains(name)) {
52 spdlog::error(std::format("Cycle detected involving : {}", name));
53 throw std::runtime_error("Cyclic dependency");
54 }
55
56 on_stack.insert(name);
57
58 auto it = data.find(name);
59 if (it == data.end()) {
60 spdlog::warn(std::format("Missing dependency: {}", name));
61 on_stack.erase(name);
62 return;
63 }
64
65 std::string child_name;
66 for (const auto& child : get_childreen(it->second))
67 {
68 child_name = get_child_name(child);
69 if(child_name.empty()) continue;
70 visit(child_name);
71 }
72
73 visited.insert(name);
74 on_stack.erase(name);
75 sorted.push_back(name);
76 };
77
78 // perform the sort
79 for (const auto& [name, _] : data) {
80 visit(name);
81 }
82
83 return sorted;
84 }
85
86 bool equalsIgnoreCase(const std::string& a, const std::string& b);
87
88 std::string toLower(std::string value);
89
90 std::optional<std::string> trimAsciiWhitespace(std::string_view value);
91
92 bool likelyNetworkPath(const std::filesystem::path & path);
93
94 void logException(const std::exception_ptr & exception_ptr, const std::string_view context);
95
96}
std::string name
Definition ingestion.cpp:29
Definition logo.hpp:6
std::string currentTimestamp()
Definition utils.cpp:17
std::string toLower(std::string value)
Definition utils.cpp:83
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:40
void logException(const std::exception_ptr &exception_ptr, const std::string_view context)
Definition utils.cpp:96
bool likelyNetworkPath(const std::filesystem::path &path)
Definition utils.cpp:89
std::optional< std::string > trimAsciiWhitespace(std::string_view value)
Definition utils.cpp:57
bool equalsIgnoreCase(const std::string &a, const std::string &b)
Definition utils.cpp:50
std::int64_t nowMs()
Definition utils.cpp:43
constexpr bool always_false
Definition utils.hpp:31
std::string loadBuildTimestamp(const std::filesystem::path &path)
Definition utils.cpp:8