souffle  2.0.2-371-g6315b36
Cell.h
Go to the documentation of this file.
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 
13 
14 #include <chrono>
15 #include <iostream>
16 #include <string>
17 #include <utility>
18 
19 namespace souffle {
20 namespace profile {
21 
22 template <typename T>
23 class Cell : public CellInterface {
24  const T value;
25 
26 public:
27  Cell(T value) : value(value){};
28  ~Cell() override = default;
29 };
30 
31 template <>
32 class Cell<std::chrono::microseconds> : public CellInterface {
33  const std::chrono::microseconds value;
34 
35 public:
36  Cell(std::chrono::microseconds value) : value(value){};
37  double getDoubleVal() const override {
38  return value.count() / 1000000.0;
39  }
40  long 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 
56 template <>
57 class Cell<double> : public CellInterface {
58  const double value;
59 
60 public:
61  Cell(double value) : value(value){};
62  double getDoubleVal() const override {
63  return value;
64  }
65  long 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 
82 template <>
83 class Cell<std::string> : public CellInterface {
84  const std::string value;
85 
86 public:
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  long 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 
108 template <>
109 class Cell<long> : public CellInterface {
110  const long value;
111 
112 public:
113  Cell(long 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  long 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 
134 template <>
135 class Cell<void> : public CellInterface, std::false_type {
136 public:
137  Cell() = default;
138  double getDoubleVal() const override {
139  std::cerr << "getting double on void cell";
140  throw this;
141  }
142  long 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
souffle::profile::CellInterface::getLongVal
virtual long getLongVal() const =0
souffle::profile::Cell
Definition: Cell.h:23
souffle::profile::Cell< double >::getStringVal
std::string getStringVal() const override
Definition: Cell.h:69
souffle::profile::CellInterface::getTimeVal
virtual std::chrono::microseconds getTimeVal() const =0
souffle::profile::Cell< double >::getTimeVal
std::chrono::microseconds getTimeVal() const override
Definition: Cell.h:73
CellInterface.h
souffle::profile::Cell< long >::getStringVal
std::string getStringVal() const override
Definition: Cell.h:118
souffle::profile::Tools::cleanString
std::string cleanString(std::string val)
Remove and \t characters, and \t sequence of two chars, and wrapping quotes.
Definition: StringUtils.h:206
souffle::profile::Cell< double >::getDoubleVal
double getDoubleVal() const override
Definition: Cell.h:62
souffle::profile::CellInterface::getStringVal
virtual std::string getStringVal() const =0
souffle::profile::Cell< double >::Cell
Cell(double value)
Definition: Cell.h:61
souffle::profile::Cell< std::chrono::microseconds >::getLongVal
long getLongVal() const override
Definition: Cell.h:40
souffle::profile::CellInterface::toString
virtual std::string toString(int precision) const =0
souffle::profile::CellInterface
Definition: CellInterface.h:17
souffle::profile::Cell< std::chrono::microseconds >::value
const std::chrono::microseconds value
Definition: Cell.h:33
souffle::profile::Cell< long >::toString
std::string toString(int precision) const override
Definition: Cell.h:129
souffle::profile::Cell::~Cell
~Cell() override=default
souffle::profile::Cell< void >::getTimeVal
std::chrono::microseconds getTimeVal() const override
Definition: Cell.h:150
souffle::profile::Tools::formatNum
std::string formatNum(double amount)
Definition: StringUtils.h:40
souffle::profile::Cell< std::chrono::microseconds >::Cell
Cell(std::chrono::microseconds value)
Definition: Cell.h:36
souffle::profile::Cell< void >::toString
std::string toString(int) const override
Definition: Cell.h:154
souffle::profile::Cell< std::chrono::microseconds >::getTimeVal
std::chrono::microseconds getTimeVal() const override
Definition: Cell.h:51
souffle::profile::Cell< std::chrono::microseconds >::getDoubleVal
double getDoubleVal() const override
Definition: Cell.h:37
souffle::profile::Cell< void >::getLongVal
long getLongVal() const override
Definition: Cell.h:142
souffle::profile::Cell::value
const T value
Definition: Cell.h:24
souffle::profile::Cell< long >::getDoubleVal
double getDoubleVal() const override
Definition: Cell.h:114
souffle::profile::Cell< std::chrono::microseconds >::toString
std::string toString(int) const override
Definition: Cell.h:48
souffle::profile::Cell< long >::getTimeVal
std::chrono::microseconds getTimeVal() const override
Definition: Cell.h:125
souffle::profile::CellInterface::getDoubleVal
virtual double getDoubleVal() const =0
souffle::profile::Cell::Cell
Cell(T value)
Definition: Cell.h:27
StringUtils.h
souffle::profile::Cell< std::chrono::microseconds >::getStringVal
std::string getStringVal() const override
Definition: Cell.h:44
souffle::profile::Cell< void >::getDoubleVal
double getDoubleVal() const override
Definition: Cell.h:138
souffle::profile::Cell< double >::getLongVal
long getLongVal() const override
Definition: Cell.h:65
souffle::profile::Tools::formatTime
std::string formatTime(std::chrono::microseconds number)
Definition: StringUtils.h:108
souffle::profile::Cell< double >::toString
std::string toString(int) const override
Definition: Cell.h:77
std
Definition: Brie.h:3053
souffle
Definition: AggregateOp.h:25
souffle::profile::Cell< long >::getLongVal
long getLongVal() const override
Definition: Cell.h:122
souffle::profile::Cell< long >::value
const long value
Definition: Cell.h:110
souffle::profile::Cell< double >::value
const double value
Definition: Cell.h:58
souffle::profile::Cell< long >::Cell
Cell(long value)
Definition: Cell.h:113
souffle::profile::Cell< void >::getStringVal
std::string getStringVal() const override
Definition: Cell.h:146