am-freescan/integrations/freescan-service/src/common.hpp

93 lines
2.6 KiB
C++
Raw Normal View History

#pragma once
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#include "../third_party/json.hpp"
namespace freescan {
using Json = nlohmann::json;
namespace fs = std::filesystem;
struct ApiError : std::runtime_error {
int status;
std::string code;
ApiError(int value, std::string name, std::string message)
: std::runtime_error(std::move(message)), status(value), code(std::move(name)) {}
};
std::wstring wide(const std::string &value);
std::string utf8(const std::wstring &value);
std::string path_string(const fs::path &value);
std::string utc_now();
std::string utc_from_ms(int64_t value);
int64_t unix_ms();
std::string new_id();
std::string base64_encode(const std::vector<uint8_t> &value);
std::vector<uint8_t> base64_decode(const std::string &value);
Json read_json(const fs::path &path);
void write_json(const fs::path &path, const Json &value);
void log_line(const fs::path &path, const std::string &message);
Json error_json(const std::exception &exception);
struct FrameSnapshot {
std::vector<uint8_t> jpeg;
int width = 0, height = 0, sourceWidth = 0, sourceHeight = 0;
uint64_t sequence = 0;
int64_t capturedMs = 0, encodedMs = 0;
double encodeMs = 0;
};
Json frame_to_json(const FrameSnapshot &frame);
FrameSnapshot frame_from_json(const Json &value);
class Handle {
HANDLE handle_ = INVALID_HANDLE_VALUE;
public:
Handle() = default;
explicit Handle(HANDLE value) : handle_(value) {}
~Handle() {
reset();
}
Handle(const Handle &) = delete;
Handle &operator=(const Handle &) = delete;
Handle(Handle &&other) noexcept : handle_(other.release()) {}
Handle &operator=(Handle &&other) noexcept {
if (this != &other)
reset(other.release());
return *this;
}
HANDLE get() const {
return handle_;
}
explicit operator bool() const {
return handle_ && handle_ != INVALID_HANDLE_VALUE;
}
HANDLE release() {
HANDLE value = handle_;
handle_ = INVALID_HANDLE_VALUE;
return value;
}
void reset(HANDLE value = INVALID_HANDLE_VALUE) {
if (*this)
CloseHandle(handle_);
handle_ = value;
}
};
} // namespace freescan