Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

improved exception handling for deserialization

  • Loading branch information...
commit e595430107c31eed2e260ca5e834e082a667593b 1 parent 65025d6
Dominik Charousset authored
5 cppa/detail/default_uniform_type_info_impl.hpp
@@ -319,10 +319,7 @@ class default_uniform_type_info_impl : public util::abstract_uniform_type_info<T
319 319 }
320 320
321 321 void deserialize(void* obj, deserializer* d) const {
322   - std::string cname = d->seek_object();
323   - if (cname != this->name()) {
324   - throw std::logic_error("wrong type name found");
325   - }
  322 + this->assert_type_name(d);
326 323 d->begin_object(this->name());
327 324 for (auto& m : m_members) {
328 325 m.deserialize(obj, d);
14 cppa/uniform_type_info.hpp
@@ -234,8 +234,22 @@ class uniform_type_info {
234 234 */
235 235 virtual void deserialize(void* instance, deserializer* source) const = 0;
236 236
  237 + /**
  238 + * @brief Checks wheter <tt>src->seek_object()</tt>
  239 + * returns @p tname and throws an exception if not.
  240 + * @throws std::logic_error
  241 + */
  242 + static void assert_type_name(deserializer* src, const std::string& tname);
  243 +
237 244 protected:
238 245
  246 + /**
  247 + * @brief Checks wheter <tt>src->seek_object()</tt>
  248 + * returns {@link name()} and throws an exception if not.
  249 + * @throws std::logic_error
  250 + */
  251 + void assert_type_name(deserializer* src) const;
  252 +
239 253 uniform_type_info(const std::string& uniform_name);
240 254
241 255 /**
11 examples/announce_example_5.cpp
@@ -100,18 +100,13 @@ class tree_type_info : public util::abstract_uniform_type_info<tree> {
100 100 }
101 101
102 102 void deserialize(void* ptr, deserializer* source) const {
103   - // seek_object() gets the uniform name of the next object in the
104   - // stream without modifying the deserializer
105   - std::string cname = source->seek_object();
106   - // this name has to be our type name
107   - if (cname != name()) {
108   - throw std::logic_error("wrong type name found");
109   - }
  103 + // throws an exception if the next object in source is not a tree
  104 + assert_type_name(source);
110 105 // ptr is guaranteed to be a pointer of type tree
111 106 auto tree_ptr = reinterpret_cast<tree*>(ptr);
112 107 tree_ptr->root.children.clear();
113 108 // workflow is analogous to serialize: begin_object() ... end_object()
114   - source->begin_object(cname);
  109 + source->begin_object(name());
115 110 // recursively deserialize nodes, beginning with root
116 111 deserialize_node(tree_ptr->root, source);
117 112 source->end_object();
57 src/uniform_type_info.cpp
@@ -203,9 +203,7 @@ class actor_ptr_tinfo : public util::abstract_uniform_type_info<actor_ptr> {
203 203 deserialize_nullptr(source);
204 204 ptrref.reset();
205 205 }
206   - else {
207   - throw std::logic_error("wrong type name found");
208   - }
  206 + else assert_type_name(source, name); // throws
209 207 }
210 208 else {
211 209 primitive_variant ptup[3];
@@ -279,9 +277,7 @@ class group_ptr_tinfo : public util::abstract_uniform_type_info<group_ptr> {
279 277 deserialize_nullptr(source);
280 278 ptrref.reset();
281 279 }
282   - else {
283   - throw std::logic_error("wrong type name found");
284   - }
  280 + else assert_type_name(source, name); // throws
285 281 }
286 282 else {
287 283 source->begin_object(name);
@@ -344,11 +340,8 @@ class channel_ptr_tinfo : public util::abstract_uniform_type_info<channel_ptr> {
344 340 const std::string& name,
345 341 const std::string& actor_ptr_type_name,
346 342 const std::string& group_ptr_type_name) {
347   - std::string cname = source->seek_object();
348   - if (cname != name) {
349   - throw std::logic_error("wrong type name found");
350   - }
351   - source->begin_object(cname);
  343 + assert_type_name(source, name);
  344 + source->begin_object(name);
352 345 std::string subobj = source->peek_object();
353 346 if (subobj == actor_ptr_type_name) {
354 347 actor_ptr tmp;
@@ -466,9 +459,8 @@ class addr_msg_tinfo : public util::abstract_uniform_type_info<addressed_message
466 459 }
467 460
468 461 virtual void deserialize(void* instance, deserializer* source) const {
469   - auto tname = source->seek_object();
470   - if (tname != name()) throw 42;
471   - source->begin_object(tname);
  462 + assert_type_name(source);
  463 + source->begin_object(name());
472 464 auto& msg = *reinterpret_cast<addressed_message*>(instance);
473 465 actor_ptr_tinfo::s_deserialize(msg.sender(), source, actor_ptr_name);
474 466 channel_ptr_tinfo::s_deserialize(msg.receiver(),
@@ -518,9 +510,7 @@ class process_info_ptr_tinfo : public util::abstract_uniform_type_info<process_i
518 510 deserialize_nullptr(source);
519 511 ptrref.reset();
520 512 }
521   - else {
522   - throw std::logic_error("wrong type name found");
523   - }
  513 + else assert_type_name(source); // throws
524 514 }
525 515 else {
526 516 primitive_variant ptup[2];
@@ -548,10 +538,9 @@ class atom_value_tinfo : public util::abstract_uniform_type_info<atom_value> {
548 538 }
549 539
550 540 virtual void deserialize(void* instance, deserializer* source) const {
  541 + assert_type_name(source);
551 542 auto val = reinterpret_cast<atom_value*>(instance);
552   - auto tname = source->seek_object();
553   - if (tname != name()) throw 42;
554   - source->begin_object(tname);
  543 + source->begin_object(name());
555 544 auto ptval = source->read_value(pt_uint64);
556 545 source->end_object();
557 546 *val = static_cast<atom_value>(get<std::uint64_t>(ptval));
@@ -570,10 +559,9 @@ class duration_tinfo : public util::abstract_uniform_type_info<util::duration> {
570 559 }
571 560
572 561 virtual void deserialize(void* instance, deserializer* source) const {
  562 + assert_type_name(source);
  563 + source->begin_object(name());
573 564 auto val = reinterpret_cast<util::duration*>(instance);
574   - auto tname = source->seek_object();
575   - if (tname != name()) throw 42;
576   - source->begin_object(tname);
577 565 auto unit_val = source->read_value(pt_uint32);
578 566 auto count_val = source->read_value(pt_uint32);
579 567 source->end_object();
@@ -628,9 +616,8 @@ class bool_tinfo : public util::abstract_uniform_type_info<bool> {
628 616 }
629 617
630 618 virtual void deserialize(void* instance, deserializer* source) const {
631   - auto tname = source->seek_object();
632   - if (tname != name()) throw 42;
633   - source->begin_object(tname);
  619 + assert_type_name(source);
  620 + source->begin_object(name());
634 621 auto ptval = source->read_value(pt_uint8);
635 622 source->end_object();
636 623 *reinterpret_cast<bool*>(instance) = (get<pt_uint8>(ptval) != 0);
@@ -776,7 +763,23 @@ bool announce(const std::type_info& tinfo, uniform_type_info* utype) {
776 763
777 764 uniform_type_info::uniform_type_info(const std::string& str) : m_name(str) { }
778 765
779   -uniform_type_info::~uniform_type_info() {
  766 +uniform_type_info::~uniform_type_info() { }
  767 +
  768 +void uniform_type_info::assert_type_name(deserializer* source,
  769 + const std::string& expected_name) {
  770 + std::string tname = source->seek_object();
  771 + if (tname != expected_name) {
  772 + std::string error_msg = "wrong type name found; expected \"";
  773 + error_msg += expected_name;
  774 + error_msg += "\", found \"";
  775 + error_msg += tname;
  776 + error_msg += "\"";
  777 + throw std::logic_error(std::move(error_msg));
  778 + }
  779 +}
  780 +
  781 +void uniform_type_info::assert_type_name(deserializer* source) const {
  782 + assert_type_name(source, name());
780 783 }
781 784
782 785 object uniform_type_info::create() const {
4 unit_testing/test__serialization.cpp
@@ -124,9 +124,7 @@ struct raw_struct_type_info : util::abstract_uniform_type_info<raw_struct> {
124 124 sink->end_object();
125 125 }
126 126 void deserialize(void* ptr, deserializer* source) const {
127   - if (source->seek_object() != name()) {
128   - throw std::logic_error("wrong type name found");
129   - }
  127 + assert_type_name(source);
130 128 source->begin_object(name());
131 129 auto rs = reinterpret_cast<raw_struct*>(ptr);
132 130 rs->str.clear();

0 comments on commit e595430

Please sign in to comment.
Something went wrong with that request. Please try again.