-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandbox.h
55 lines (46 loc) · 1.36 KB
/
sandbox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include "options.h"
#include <memory>
struct ExecutionResults {
double cpu_time = 0;
double sys_time = 0;
double wall_time = 0;
uint64_t memory_usage = 0;
int32_t status_code = 0;
int32_t signal = 0;
bool killed_by_sandbox = false;
bool error = false;
std::string message;
};
class Sandbox {
public:
virtual ExecutionResults Execute(const options::Options &options) = 0;
virtual bool CanUse() = 0;
virtual int Priority() = 0;
virtual ~Sandbox() {}
};
class SandboxRegistry {
public:
static void Register(std::unique_ptr<Sandbox> sandbox) {
Registry().push_back(std::move(sandbox));
}
static Sandbox *Get();
private:
using reg_t = std::vector<std::unique_ptr<Sandbox>>;
static reg_t &Registry() {
static reg_t registry;
return registry;
}
};
void PrintResults(const ExecutionResults &results, const std::string &file);
void PrintJsonResults(const ExecutionResults &results, const std::string &file);
namespace detail {
template <typename Sandbox> class Register {
public:
Register() { SandboxRegistry::Register(std::make_unique<Sandbox>()); }
};
} // namespace detail
#define REGISTER_SANDBOX(Sandbox) \
namespace { \
detail::Register<Sandbox> register_##Sandbox; \
}