9 static const char rcsid[] __attribute__((used)) =
"$Id: Interval.cpp 1911 2017-09-02 17:37:35Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Interval::Interval() throw () :
Nullable(std::make_shared<uint64_t>(0)) { }
18 Interval::Interval(
const std::string &value)
throw (TypeCastException) :
Nullable() {
19 boost::smatch matcher;
21 if (boost::regex_match(value, matcher, Interval::Pattern())) {
25 usec = std::stoll(matcher[1].str().c_str());
26 }
catch (std::invalid_argument &e) {
27 THROW(TypeCastException,
"unable to cast '%s' to an interval value (invalid string)", value.c_str());
28 }
catch (std::out_of_range &e) {
29 THROW(TypeCastException,
"unable to cast '%s' to an interval value (overflow/underflow)", value.c_str());
32 switch (matcher[2].str().c_str()[0]) {
34 usec *= 86400000000LL;
53 THROW(TypeCastException,
"unable to cast '%s' to an interval value", value.c_str());
56 this->value = std::make_shared<uint64_t>(usec);
58 THROW(TypeCastException,
"unable to cast '%s' to an interval value", value.c_str());
62 Interval::Interval(int64_t value)
throw () :
Nullable(std::make_shared<int64_t>(value)) { }
66 if (!other.isNull()) {
68 this->value = std::make_shared<int64_t>(other.getUsec());
70 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
78 Interval::~Interval() throw () { }
80 bool Interval::operator==(
const Interval &other)
const throw () {
81 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
84 bool Interval::operator!=(
const Interval &other)
const throw () {
85 return !(*
this == other);
88 bool Interval::operator>(
const Interval &other)
const throw () {
89 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
92 bool Interval::operator<(
const Interval &other)
const throw () {
93 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
98 if (!other.isNull()) {
100 this->value = std::make_shared<int64_t>(other.getUsec());
102 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
112 Interval& Interval::operator+=(int64_t value)
throw () {
113 (*std::static_pointer_cast<int64_t>(this->value)) += value;
118 Interval& Interval::operator-=(int64_t value)
throw () {
119 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
124 Interval& Interval::operator*=(int64_t value)
throw () {
125 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
130 Interval& Interval::operator/=(int64_t value)
throw () {
131 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
136 Interval& Interval::operator^=(int64_t value)
throw () {
137 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
142 Interval& Interval::operator%=(int64_t value)
throw () {
143 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
148 Interval& Interval::operator&=(int64_t value)
throw () {
149 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
154 Interval& Interval::operator|=(int64_t value)
throw () {
155 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
160 int64_t Interval::getUsec()
const throw (UnsupportedOperatorException) {
162 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
165 return *std::static_pointer_cast<int64_t>(this->value);
171 if (UNLIKELY(this->isNull())) {
172 c = mpack.append(offset);
174 c = mpack.append(offset, this->str(JSON));
179 if (UNLIKELY(c == 0)) {
180 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
184 void Interval::msgpackBufferSize(
size_t &size)
const throw () {
185 if (UNLIKELY(isNull())) {
194 std::string Interval::str(
int type)
const throw () {
195 if (UNLIKELY(this->isNull())) {
200 int64_t usec = this->getUsec();
202 if ((usec % 3600000000LL) == 0) {
203 snprintf(buffer,
sizeof(buffer),
"%lldh", usec % 3600000000LL);
204 }
else if ((usec % 60000000LL) == 0) {
205 snprintf(buffer,
sizeof(buffer),
"%lldm", usec % 60000000LL);
206 }
else if ((usec % 1000000LL) == 0) {
207 snprintf(buffer,
sizeof(buffer),
"%llds", usec % 1000000LL);
209 snprintf(buffer,
sizeof(buffer),
"%" PRId64
"u", usec);
215 bool Interval::isType(
const char *value,
size_t size)
throw () {
216 return (size > 1 && (value[size - 1] ==
'd' || value[size - 1] ==
'h' || value[size - 1] ==
'm' || value[size - 1] ==
's' || value[size - 1] ==
'u') && boost::regex_match(std::string(value, size), Pattern()));