| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "LibLsp/JsonRpc/serializer.h" |
| 4 |
#include <string> |
| 5 |
#include "LibLsp/JsonRpc/message.h" |
| 6 |
namespace lsp |
| 7 |
{ |
| 8 |
struct Any |
| 9 |
{ |
| 10 |
//! Type of JSON value |
| 11 |
enum Type |
| 12 |
{ |
| 13 |
kUnKnown = -1, |
| 14 |
kNullType = 0, //!< null |
| 15 |
kFalseType = 1, //!< false |
| 16 |
kTrueType = 2, //!< true |
| 17 |
kObjectType = 3, //!< object |
| 18 |
kArrayType = 4, //!< array |
| 19 |
kStringType = 5, //!< string |
| 20 |
kNumberType = 6 //!< number |
| 21 |
}; |
| 22 |
|
| 23 |
template<typename T> |
| 24 |
bool Get(T& value); |
| 25 |
|
| 26 |
template<typename T> |
| 27 |
void Set(T& value); |
| 28 |
|
| 29 |
int GuessType(); |
| 30 |
int GetType(); |
| 31 |
|
| 32 |
void Set(std::unique_ptr<LspMessage> value); |
| 33 |
|
| 34 |
void SetJsonString(std::string&& _data, Type _type); |
| 35 |
|
| 36 |
void SetJsonString(std::string const& _data, Type _type); |
| 37 |
|
| 38 |
std::string const& Data() const |
| 39 |
{ |
| 40 |
return data; |
| 41 |
} |
| 42 |
|
| 43 |
void swap(Any& arg) noexcept; |
| 44 |
|
| 45 |
/* |
| 46 |
*Example for GetFromMap |
| 47 |
struct A{ |
| 48 |
std::string visitor; |
| 49 |
bool verbose; |
| 50 |
} |
| 51 |
REFLECT_MAP_TO_STRUCT(A,visitor,verbose) |
| 52 |
|
| 53 |
std::string data = "{\"visitor\":\"default\",\"verbose\":\"true\"}; |
| 54 |
lsp:Any any; |
| 55 |
any.SetJsonString(data, static_cast<lsp::Any::Type>(-1)); |
| 56 |
A a_object; |
| 57 |
any.GetFromMap(a_object); |
| 58 |
*/ |
| 59 |
template<typename T> |
| 60 |
bool GetFromMap(T& value); |
| 61 |
|
| 62 |
template<typename T> |
| 63 |
bool GetForMapHelper(T& value); |
| 64 |
bool GetForMapHelper(std::string& value); |
| 65 |
bool GetForMapHelper(optional<std::string>& value); |
| 66 |
|
| 67 |
private: |
| 68 |
std::unique_ptr<Reader> GetReader(); |
| 69 |
std::unique_ptr<Writer> GetWriter() const; |
| 70 |
void SetData(std::unique_ptr<Writer>&); |
| 71 |
|
| 72 |
std::string data; |
| 73 |
int jsonType = kUnKnown; |
| 74 |
}; |
| 75 |
|
| 76 |
}; // namespace lsp |
| 77 |
|
| 78 |
extern void Reflect(Reader& visitor, lsp::Any& value); |
| 79 |
extern void Reflect(Writer& visitor, lsp::Any& value); |
| 80 |
|
| 81 |
template<typename T> |
| 82 |
void ReflectMember(std::map<std::string, lsp::Any>& visitor, char const* name, T& value) |
| 83 |
{ |
| 84 |
|
| 85 |
auto it = visitor.find(name); |
| 86 |
if (it != visitor.end()) |
| 87 |
{ |
| 88 |
it->second.GetForMapHelper(value); |
| 89 |
} |
| 90 |
} |
| 91 |
template<typename T> |
| 92 |
void ReflectMember(std::map<std::string, std::string>& visitor, char const* name, T& value) |
| 93 |
{ |
| 94 |
|
| 95 |
auto it = visitor.find(name); |
| 96 |
if (it != visitor.end()) |
| 97 |
{ |
| 98 |
lsp::Any any; |
| 99 |
any.SetJsonString(it->second, static_cast<lsp::Any::Type>(-1)); |
| 100 |
any.Get(value); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#define REFLECT_MAP_TO_STRUCT(type, ...) \ |
| 105 |
template<typename TVisitor> \ |
| 106 |
void ReflectMap(TVisitor& visitor, type& value) \ |
| 107 |
{ \ |
| 108 |
MACRO_MAP(_MAPPABLE_REFLECT_MEMBER, __VA_ARGS__) \ |
| 109 |
} |
| 110 |
|
| 111 |
namespace lsp |
| 112 |
{ |
| 113 |
template<typename T> |
| 114 |
bool Any::Get(T& value) |
| 115 |
{ |
| 116 |
auto const visitor = GetReader(); |
| 117 |
Reflect(*visitor, value); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
template<typename T> |
| 122 |
void Any::Set(T& value) |
| 123 |
{ |
| 124 |
auto visitor = GetWriter(); |
| 125 |
Reflect(*visitor, value); |
| 126 |
SetData(visitor); |
| 127 |
} |
| 128 |
|
| 129 |
template<typename T> |
| 130 |
bool Any::GetFromMap(T& value) |
| 131 |
{ |
| 132 |
auto const visitor = GetReader(); |
| 133 |
std::map<std::string, lsp::Any> _temp; |
| 134 |
Reflect(*visitor, _temp); |
| 135 |
ReflectMap(_temp, value); |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
template<typename T> |
| 140 |
bool Any::GetForMapHelper(T& value) |
| 141 |
{ |
| 142 |
jsonType = GetType(); |
| 143 |
if (jsonType == kStringType) |
| 144 |
{ |
| 145 |
auto copy = data; |
| 146 |
copy.erase(copy.find_last_not_of('"') + 1); |
| 147 |
copy.erase(0, copy.find_first_not_of('"')); |
| 148 |
lsp::Any any; |
| 149 |
any.SetJsonString(copy, kUnKnown); |
| 150 |
any.Get(value); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
Get(value); |
| 155 |
} |
| 156 |
return true; |
| 157 |
} |
| 158 |
} // namespace lsp |