lokinet/llarp/util/status.cpp

158 lines
3.7 KiB
C++
Raw Normal View History

2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
namespace llarp
{
namespace util
{
2019-02-11 17:14:43 +00:00
StatusObject::StatusObject(const StatusObject& other)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
Impl.SetObject();
2019-02-11 17:14:43 +00:00
auto& a = Impl.GetAllocator();
Impl.CopyFrom(other.Impl, a);
#else
(void)other;
2019-02-08 19:43:25 +00:00
#endif
}
StatusObject::~StatusObject()
{
2019-02-08 22:44:21 +00:00
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
Impl.RemoveAllMembers();
2019-02-08 22:44:21 +00:00
#endif
2019-02-08 19:43:25 +00:00
}
void
2019-02-11 17:14:43 +00:00
StatusObject::Put(const value_type& val)
{
Put(std::get< 0 >(val), std::get< 1 >(val));
}
void
StatusObject::Put(String_t name, const Variant& val)
{
if(absl::holds_alternative< uint64_t >(val))
PutInt(name, absl::get< uint64_t >(val));
else if(absl::holds_alternative< std::string >(val))
PutString(name, absl::get< std::string >(val));
else if(absl::holds_alternative< bool >(val))
PutBool(name, absl::get< bool >(val));
else if(absl::holds_alternative< StatusObject >(val))
PutObject(name, absl::get< StatusObject >(val));
else if(absl::holds_alternative< std::vector< std::string > >(val))
PutStringArray(name, absl::get< std::vector< std::string > >(val));
else if(absl::holds_alternative< std::vector< StatusObject > >(val))
PutObjectArray(name, absl::get< std::vector< StatusObject > >(val));
}
void
StatusObject::PutBool(String_t name, bool val)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
v.SetBool(val);
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
(void)val;
#endif
}
void
2019-02-11 17:14:43 +00:00
StatusObject::PutInt(String_t name, uint64_t val)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
2019-02-08 21:29:56 +00:00
v.SetUint64(val);
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
(void)val;
#endif
}
void
2019-02-11 17:14:43 +00:00
StatusObject::PutObject(String_t name, const StatusObject& val)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
v.SetObject();
2019-02-08 21:29:56 +00:00
v.CopyFrom(val.Impl, a);
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
(void)val;
#endif
}
void
2019-02-11 17:14:43 +00:00
StatusObject::PutObjectArray(String_t name,
const std::vector< StatusObject >& arr)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
v.SetArray();
2019-02-08 21:29:56 +00:00
Value_t i;
2019-02-08 19:43:25 +00:00
for(const auto& obj : arr)
{
2019-02-08 21:29:56 +00:00
v.PushBack(i.SetObject().CopyFrom(obj.Impl, a), a);
2019-02-08 19:43:25 +00:00
}
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
2019-02-08 21:29:56 +00:00
(void)arr;
2019-02-08 19:43:25 +00:00
#endif
}
void
2019-02-11 17:14:43 +00:00
StatusObject::PutStringArray(String_t name,
const std::vector< std::string >& arr)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
v.SetArray();
2019-02-08 21:29:56 +00:00
Value_t i;
2019-02-11 17:14:43 +00:00
for(const auto& str : arr)
2019-02-08 19:43:25 +00:00
{
2019-02-08 21:29:56 +00:00
v.PushBack(i.SetString(str.c_str(), a), a);
2019-02-08 19:43:25 +00:00
}
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
2019-02-08 21:29:56 +00:00
(void)arr;
2019-02-08 19:43:25 +00:00
#endif
}
void
2019-02-11 17:14:43 +00:00
StatusObject::PutString(String_t name, const std::string& val)
2019-02-08 19:43:25 +00:00
{
#ifdef USE_ABYSS
2019-02-08 21:29:56 +00:00
auto& a = Impl.GetAllocator();
2019-02-08 19:43:25 +00:00
Value_t v;
2019-02-08 21:29:56 +00:00
v.SetString(val.c_str(), a);
2019-02-11 17:14:43 +00:00
auto s = llarp::string_view_string(name);
Value_t k(s.c_str(), a);
2019-02-08 21:29:56 +00:00
Impl.AddMember(k, v, a);
2019-02-08 19:43:25 +00:00
#else
(void)name;
(void)val;
#endif
}
} // namespace util
} // namespace llarp