-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89ef7b1
commit a5156e6
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,27 @@ | ||
#include "demo.pb.h" | ||
#include <iostream> | ||
#include "message_serializer.h" | ||
#include "message_deserializer.h" | ||
|
||
int main() | ||
{ | ||
demo::demo_message_int dmi; | ||
dmi.set_data(100); | ||
std::cout << dmi.data() << std::endl; | ||
std::string dmi_serialized = proto::message_serializer::serialize(dmi); | ||
|
||
|
||
demo::demo_message_string dms; | ||
dms.set_data("hello world"); | ||
|
||
std::cout << dmi.data() << std::endl; | ||
std::cout << dms.data() << std::endl; | ||
|
||
std::string dmi_serialized = proto::message_serializer::serialize(dmi); | ||
std::string dms_serialized = proto::message_serializer::serialize(dms); | ||
|
||
auto dmi_deserialized = proto::message_deserializer::deserialize(dmi_serialized.c_str(), dmi_serialized.length()); | ||
auto dms_deserialized = proto::message_deserializer::deserialize(dms_serialized.c_str(), dms_serialized.length()); | ||
|
||
std::cout << std::dynamic_pointer_cast<demo::demo_message_int>(dmi_deserialized)->data() << std::endl; | ||
std::cout << std::dynamic_pointer_cast<demo::demo_message_string>(dms_deserialized)->data() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "message_deserializer.h" | ||
|
||
namespace proto { | ||
std::shared_ptr<google::protobuf::Message> message_deserializer::deserialize(const char* buf, size_t length) { | ||
proto::proto_message proto_message; | ||
proto_message.ParseFromArray(buf, length); | ||
|
||
auto message = create_message(proto_message.type()); | ||
|
||
if(message){ | ||
message->ParseFromString(proto_message.payload()); | ||
} | ||
|
||
return message; | ||
} | ||
|
||
std::shared_ptr<google::protobuf::Message> message_deserializer::create_message(const std::string& type_name) { | ||
std::shared_ptr<google::protobuf::Message> message = nullptr; | ||
auto descriptor = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(type_name); | ||
|
||
if(descriptor) { | ||
auto prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor); | ||
|
||
if(prototype) { | ||
message = std::shared_ptr<google::protobuf::Message>(prototype->New()); | ||
} | ||
} | ||
|
||
return message; | ||
} | ||
|
||
} // proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef MESSAGE_DESERIALIZER_H_ | ||
#define MESSAGE_DESERIALIZER_H_ | ||
|
||
#include "proto_message.pb.h" | ||
#include <string> | ||
#include <memory> | ||
|
||
// forward declaration | ||
namespace google { | ||
namespace protobuf { | ||
class Message; | ||
} | ||
} | ||
|
||
namespace proto { | ||
class message_deserializer { | ||
public: | ||
static std::shared_ptr<google::protobuf::Message> deserialize(const char* buf, size_t length); | ||
|
||
private: | ||
static std::shared_ptr<google::protobuf::Message> create_message(const std::string& type_name); | ||
}; | ||
} // proto | ||
|
||
#endif // MESSAGE_DESERIALIZER_H_ |