summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/dumb_ptr.hpp32
-rw-r--r--src/common/utils.cpp2
-rw-r--r--src/common/utils.hpp16
3 files changed, 35 insertions, 15 deletions
diff --git a/src/common/dumb_ptr.hpp b/src/common/dumb_ptr.hpp
index d0b35f2..91293c6 100644
--- a/src/common/dumb_ptr.hpp
+++ b/src/common/dumb_ptr.hpp
@@ -21,6 +21,8 @@
#include "sanity.hpp"
+#include <algorithm>
+
// unmanaged new/delete-able pointer
// should be replaced by std::unique_ptr<T>
template<class T>
@@ -64,11 +66,11 @@ public:
}
explicit
- operator bool()
+ operator bool() const
{
return impl;
}
- bool operator !()
+ bool operator !() const
{
return !impl;
}
@@ -95,12 +97,12 @@ public:
}
void new_(size_t z)
{
- impl = new T[z];
+ impl = new T[z]();
}
static
dumb_ptr<T[]> make(size_t z)
{
- return dumb_ptr<T[]>(new T[z], z);
+ return dumb_ptr<T[]>(new T[z](), z);
}
void forget()
{
@@ -108,17 +110,35 @@ public:
sz = 0;
}
+ size_t size() const
+ {
+ return sz;
+ }
+ void resize(size_t z)
+ {
+ if (z == sz)
+ return;
+ T *np = new T[z]();
+ // not exception-safe, but we don't have a dtor anyway
+ size_t i = std::min(z, sz);
+ while (i-->0)
+ np[i] = std::move(impl[i]);
+ delete[] impl;
+ impl = np;
+ sz = z;
+ }
+
T& operator[](size_t i) const
{
return impl[i];
}
explicit
- operator bool()
+ operator bool() const
{
return impl;
}
- bool operator !()
+ bool operator !() const
{
return !impl;
}
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index 49dce34..e3867eb 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -125,7 +125,7 @@ bool split_key_value(const std::string& line, std::string *w1, std::string *w2)
static_assert(sizeof(timestamp_seconds_buffer) == 20, "seconds buffer");
static_assert(sizeof(timestamp_milliseconds_buffer) == 24, "millis buffer");
-void stamp_time(timestamp_seconds_buffer& out, TimeT *t)
+void stamp_time(timestamp_seconds_buffer& out, const TimeT *t)
{
struct tm when = t ? *t : TimeT::now();
strftime(out, 20, "%Y-%m-%d %H:%M:%S", &when);
diff --git a/src/common/utils.hpp b/src/common/utils.hpp
index 33117ac..f2a2117 100644
--- a/src/common/utils.hpp
+++ b/src/common/utils.hpp
@@ -73,17 +73,17 @@ struct TimeT : Comparable
// conversion
TimeT(time_t t=0) : value(t) {}
TimeT(struct tm t) : value(timegm(&t)) {}
- operator time_t() { return value; }
- operator struct tm() { time_t v = value; return *gmtime(&v); }
+ operator time_t() const { return value; }
+ operator struct tm() const { time_t v = value; return *gmtime(&v); }
- explicit operator bool() { return value; }
- bool operator !() { return !value; }
+ explicit operator bool() const { return value; }
+ bool operator !() const { return !value; }
// prevent surprises
template<class T>
TimeT(T) = delete;
template<class T>
- operator T() = delete;
+ operator T() const = delete;
static
TimeT now()
@@ -92,11 +92,11 @@ struct TimeT : Comparable
return time(NULL);
}
- bool error()
+ bool error() const
{
return value == -1;
}
- bool okay()
+ bool okay() const
{
return !error();
}
@@ -116,7 +116,7 @@ long long& convert_for_scanf(TimeT& t)
typedef char timestamp_seconds_buffer[20];
typedef char timestamp_milliseconds_buffer[24];
-void stamp_time(timestamp_seconds_buffer&, TimeT *t=nullptr);
+void stamp_time(timestamp_seconds_buffer&, const TimeT *t=nullptr);
void stamp_time(timestamp_milliseconds_buffer&);
void log_with_timestamp(FILE *out, const_string line);