13 #ifndef MMTF_MAP_DECODER_H
14 #define MMTF_MAP_DECODER_H
20 #include <msgpack.hpp>
52 void decode(
const std::string& key,
bool required, T& target);
63 std::map<std::string, msgpack::object*> data_map_;
65 std::set<std::string> decoded_keys_;
70 void checkType_(
const std::string& key, msgpack::type::object_type type,
72 void checkType_(
const std::string& key, msgpack::type::object_type type,
73 const int32_t& target);
74 void checkType_(
const std::string& key, msgpack::type::object_type type,
76 void checkType_(
const std::string& key, msgpack::type::object_type type,
77 const std::string& target);
79 void checkType_(
const std::string& key, msgpack::type::object_type type,
80 const std::vector<T>& target);
82 void checkType_(
const std::string& key, msgpack::type::object_type type,
92 if (obj.type != msgpack::type::MAP) {
93 throw DecodeError(
"Expected msgpack type to be MAP");
96 msgpack::object_kv* current_key_value = obj.via.map.ptr;
97 msgpack::object_kv* last_key_value = current_key_value + obj.via.map.size;
98 for (; current_key_value != last_key_value; ++current_key_value) {
99 msgpack::object* key = &(current_key_value->key);
100 msgpack::object* value = &(current_key_value->val);
101 if (key->type == msgpack::type::STR) {
102 std::string data_map_key(key->via.str.ptr, key->via.str.size);
103 data_map_[data_map_key] = value;
105 std::cerr <<
"Warning: Found non-string key type " << key->type
106 <<
"! Skipping..." << std::endl;
114 std::map<std::string, msgpack::object*>::iterator it;
115 it = data_map_.find(key);
116 if (it != data_map_.end()) {
117 checkType_(key, it->second->type, target);
118 if (it->second->type == msgpack::type::BIN) {
122 it->second->convert(target);
124 decoded_keys_.insert(key);
127 throw DecodeError(
"MsgPack MAP does not contain required entry "
135 std::map<std::string, msgpack::object*>::iterator map_it;
136 std::set<std::string>::iterator parsed_it;
137 for (map_it = data_map_.begin(); map_it != data_map_.end(); ++map_it) {
138 parsed_it = decoded_keys_.find(map_it->first);
139 if (parsed_it == decoded_keys_.end()) {
140 std::cerr <<
"Warning: Found non-parsed key " << map_it->first
141 <<
" in MsgPack MAP.\n";
146 inline void MapDecoder::checkType_(
const std::string& key,
147 msgpack::type::object_type type,
148 const float& target) {
149 if (type != msgpack::type::FLOAT32 && type != msgpack::type::FLOAT64) {
150 std::cerr <<
"Warning: Non-float type " << type <<
" found for "
151 "entry " << key << std::endl;
154 inline void MapDecoder::checkType_(
const std::string& key,
155 msgpack::type::object_type type,
156 const int32_t& target) {
157 if ( type != msgpack::type::POSITIVE_INTEGER
158 && type != msgpack::type::NEGATIVE_INTEGER) {
159 std::cerr <<
"Warning: Non-int type " << type <<
" found for "
160 "entry " << key << std::endl;
163 inline void MapDecoder::checkType_(
const std::string& key,
164 msgpack::type::object_type type,
165 const char& target) {
166 if (type != msgpack::type::STR) {
167 std::cerr <<
"Warning: Non-string type " << type <<
" found for "
168 "entry " << key << std::endl;
171 inline void MapDecoder::checkType_(
const std::string& key,
172 msgpack::type::object_type type,
173 const std::string& target) {
174 if (type != msgpack::type::STR) {
175 std::cerr <<
"Warning: Non-string type " << type <<
" found for "
176 "entry " << key << std::endl;
179 template <
typename T>
180 void MapDecoder::checkType_(
const std::string& key,
181 msgpack::type::object_type type,
182 const std::vector<T>& target) {
183 if (type != msgpack::type::ARRAY && type != msgpack::type::BIN) {
184 std::cerr <<
"Warning: Non-array type " << type <<
" found for "
185 "entry " << key << std::endl;
188 template <
typename T>
189 void MapDecoder::checkType_(
const std::string& key,
190 msgpack::type::object_type type,
192 if (type != msgpack::type::ARRAY && type != msgpack::type::BIN) {
193 std::cerr <<
"Warning: Non-array type " << type <<
" found for "
194 "entry " << key << std::endl;