OILS / vendor / souffle / profile / Cell.h View on Github | oilshell.org

160 lines, 111 significant
1/*
2 * Souffle - A Datalog Compiler
3 * Copyright (c) 2016, The Souffle Developers. All rights reserved
4 * Licensed under the Universal Permissive License v 1.0 as shown at:
5 * - https://opensource.org/licenses/UPL
6 * - <souffle root>/licenses/SOUFFLE-UPL.txt
7 */
8
9#pragma once
10
11#include "souffle/profile/CellInterface.h"
12#include "souffle/profile/StringUtils.h"
13
14#include <chrono>
15#include <iostream>
16#include <string>
17#include <utility>
18
19namespace souffle {
20namespace profile {
21
22template <typename T>
23class Cell : public CellInterface {
24 const T value;
25
26public:
27 Cell(T value) : value(value){};
28 ~Cell() override = default;
29};
30
31template <>
32class Cell<std::chrono::microseconds> : public CellInterface {
33 const std::chrono::microseconds value;
34
35public:
36 Cell(std::chrono::microseconds value) : value(value){};
37 double getDoubleVal() const override {
38 return value.count() / 1000000.0;
39 }
40 int64_t getLongVal() const override {
41 std::cerr << "getting long on time cell\n";
42 throw this;
43 }
44 std::string getStringVal() const override {
45 std::cerr << "getting string on time cell\n";
46 throw this;
47 }
48 std::string toString(int /* precision */) const override {
49 return Tools::formatTime(value);
50 }
51 std::chrono::microseconds getTimeVal() const override {
52 return value;
53 }
54};
55
56template <>
57class Cell<double> : public CellInterface {
58 const double value;
59
60public:
61 Cell(double value) : value(value){};
62 double getDoubleVal() const override {
63 return value;
64 }
65 int64_t getLongVal() const override {
66 std::cerr << "getting long on double cell\n";
67 throw this;
68 }
69 std::string getStringVal() const override {
70 std::cerr << "getting string on double cell\n";
71 throw this;
72 }
73 std::chrono::microseconds getTimeVal() const override {
74 std::cerr << "getting time on double cell\n";
75 throw this;
76 }
77 std::string toString(int /* precision */) const override {
78 return Tools::formatNum(value);
79 }
80};
81
82template <>
83class Cell<std::string> : public CellInterface {
84 const std::string value;
85
86public:
87 Cell(std::string value) : value(std::move(value)){};
88 double getDoubleVal() const override {
89 std::cerr << "getting double on string cell\n";
90 throw this;
91 }
92 int64_t getLongVal() const override {
93 std::cerr << "getting long on string cell\n";
94 throw this;
95 }
96 std::string getStringVal() const override {
97 return value;
98 }
99 std::chrono::microseconds getTimeVal() const override {
100 std::cerr << "getting time on double cell\n";
101 throw this;
102 }
103 std::string toString(int /* precision */) const override {
104 return Tools::cleanString(value);
105 }
106};
107
108template <>
109class Cell<int64_t> : public CellInterface {
110 const int64_t value;
111
112public:
113 Cell(int64_t value) : value(value){};
114 double getDoubleVal() const override {
115 std::cerr << "getting double on long cell\n";
116 throw this;
117 }
118 std::string getStringVal() const override {
119 std::cerr << "getting string on long cell\n";
120 throw this;
121 }
122 int64_t getLongVal() const override {
123 return value;
124 }
125 std::chrono::microseconds getTimeVal() const override {
126 std::cerr << "getting time on long cell\n";
127 throw this;
128 }
129 std::string toString(int precision) const override {
130 return Tools::formatNum(precision, value);
131 };
132};
133
134template <>
135class Cell<void> : public CellInterface, std::false_type {
136public:
137 Cell() = default;
138 double getDoubleVal() const override {
139 std::cerr << "getting double on void cell";
140 throw this;
141 }
142 int64_t getLongVal() const override {
143 std::cerr << "getting long on void cell";
144 throw this;
145 }
146 std::string getStringVal() const override {
147 std::cerr << "getting string on void cell\n";
148 throw this;
149 }
150 std::chrono::microseconds getTimeVal() const override {
151 std::cerr << "getting time on void cell\n";
152 throw this;
153 }
154 std::string toString(int /* precision */) const override {
155 return "-";
156 }
157};
158
159} // namespace profile
160} // namespace souffle