145 lines
3.4 KiB
C++
145 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <fstream>
|
|
#include <google/protobuf/repeated_ptr_field.h>
|
|
#include <google/protobuf/text_format.h>
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include <string>
|
|
|
|
#ifndef _WIN32
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
#include "bts_export.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/datetime.h"
|
|
|
|
namespace google {
|
|
namespace protobuf {
|
|
class Message;
|
|
class Timestamp;
|
|
} // namespace protobuf
|
|
} // namespace google
|
|
|
|
namespace bts {
|
|
namespace utils {
|
|
namespace pb {
|
|
|
|
BTS_EXPORT std::string to_string(google::protobuf::Message const &msg);
|
|
BTS_EXPORT google::protobuf::Timestamp to_timestamp(
|
|
datetime::PTime const &ptime);
|
|
|
|
template <class T>
|
|
bool from_string(const std::string &str, T *out) {
|
|
return google::protobuf::TextFormat::ParseFromString(str, out);
|
|
}
|
|
|
|
template <class T>
|
|
T from_string(const std::string &str) {
|
|
T result;
|
|
bts_assert(google::protobuf::TextFormat::ParseFromString(str, &result));
|
|
return result;
|
|
}
|
|
|
|
template <class T>
|
|
void writebuf(std::ofstream &out, T &pb) {
|
|
auto size = static_cast<std::uint32_t>(pb.ByteSizeLong());
|
|
out.write(reinterpret_cast<char *>(&size), sizeof(size));
|
|
bts_assert(pb.SerializeToOstream(&out));
|
|
}
|
|
|
|
template <class T>
|
|
bool readbuf(std::istream &in, T *result) {
|
|
bts_assert(result != nullptr);
|
|
uint32_t size;
|
|
in.read(reinterpret_cast<char *>(&size), sizeof(size));
|
|
std::vector<char> buffer(size);
|
|
in.read(&buffer[0], size);
|
|
if (!in.fail()) {
|
|
return result->ParsePartialFromArray(&buffer[0], size);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
inline google::protobuf::Timestamp pb_now() {
|
|
::google::protobuf::Timestamp result;
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL);
|
|
result.set_seconds(tv.tv_sec);
|
|
result.set_nanos(tv.tv_usec * 1000);
|
|
return result;
|
|
}
|
|
|
|
inline bool smaller(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
if (t1.seconds() < t2.seconds())
|
|
return true;
|
|
else if (t1.seconds() > t2.seconds())
|
|
return false;
|
|
else
|
|
return t1.nanos() < t2.nanos();
|
|
}
|
|
|
|
inline bool greater(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
if (t1.seconds() > t2.seconds())
|
|
return true;
|
|
else if (t1.seconds() < t2.seconds())
|
|
return false;
|
|
else
|
|
return t1.nanos() > t2.nanos();
|
|
}
|
|
|
|
inline bool equal(
|
|
google::protobuf::Timestamp const &t1,
|
|
google::protobuf::Timestamp const &t2) {
|
|
return t1.seconds() == t2.seconds() && t1.nanos() == t2.nanos();
|
|
}
|
|
|
|
inline double to_double_time(google::protobuf::Timestamp const &t1) {
|
|
return t1.seconds() + t1.nanos() / 1e9;
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace pb
|
|
} // namespace utils
|
|
} // namespace bts
|
|
|
|
namespace google::protobuf {
|
|
|
|
template <
|
|
class T,
|
|
typename std::enable_if_t<
|
|
nlohmann::detail::has_to_json<nlohmann::json, T>::value,
|
|
int> = 0>
|
|
void to_json(
|
|
nlohmann::json &j, const google::protobuf::RepeatedPtrField<T> &vals) {
|
|
j = nlohmann::json::array();
|
|
for (const T &v : vals) {
|
|
to_json(j.emplace_back(), v);
|
|
}
|
|
}
|
|
|
|
template <
|
|
class T,
|
|
typename std::enable_if_t<
|
|
nlohmann::detail::has_to_json<nlohmann::json, T>::value,
|
|
int> = 0>
|
|
void from_json(
|
|
const nlohmann::json &j, google::protobuf::RepeatedPtrField<T> &vals) {
|
|
vals.Clear();
|
|
if (!j.is_array()) {
|
|
return;
|
|
}
|
|
for (auto it = j.begin(); it != j.end(); it++) {
|
|
it->get_to(*vals.Add());
|
|
}
|
|
}
|
|
|
|
} // namespace google::protobuf
|