22 #ifndef DCCLPROTOBUFCPPTYPEHELPERS20110323H
23 #define DCCLPROTOBUFCPPTYPEHELPERS20110323H
25 #include <boost/any.hpp>
26 #include <google/protobuf/descriptor.h>
27 #include <google/protobuf/descriptor.pb.h>
28 #include <google/protobuf/message.h>
41 virtual std::string
as_str() {
return "TYPE_UNKNOWN"; }
44 template<google::protobuf::FieldDescriptor::Type t>
49 {
return google::protobuf::FieldDescriptorProto::Type_Name(
static_cast<google::protobuf::FieldDescriptorProto::Type
>(t)); }
60 virtual std::string
as_str() {
return "CPPTYPE_UNKNOWN"; }
67 boost::any
get_value(
const google::protobuf::FieldDescriptor* field,
70 const google::protobuf::Reflection* refl = msg.GetReflection();
71 if(!refl->HasField(msg, field))
74 return _get_value(field, msg);
80 return _get_value(0, msg);
90 const google::protobuf::FieldDescriptor* field,
93 {
return _get_repeated_value(field, msg, index); }
101 void set_value(
const google::protobuf::FieldDescriptor* field,
108 _set_value(field, msg, value);
118 _set_value(0, msg, value);
126 void add_value(
const google::protobuf::FieldDescriptor* field,
133 _add_value(field, msg, value);
136 virtual void _set_value(
const google::protobuf::FieldDescriptor* field,
141 virtual void _add_value(
const google::protobuf::FieldDescriptor* field,
146 virtual boost::any _get_repeated_value(
147 const google::protobuf::FieldDescriptor* field,
150 {
return boost::any(); }
152 virtual boost::any _get_value(
153 const google::protobuf::FieldDescriptor* field,
155 {
return boost::any(); }
164 std::string
as_str() {
return "CPPTYPE_DOUBLE"; }
166 boost::any _get_value(
167 const google::protobuf::FieldDescriptor* field,
169 {
return msg.GetReflection()->GetDouble(msg, field); }
170 boost::any _get_repeated_value(
171 const google::protobuf::FieldDescriptor* field,
174 {
return msg.GetReflection()->GetRepeatedDouble(msg, field, index); }
175 void _set_value(
const google::protobuf::FieldDescriptor* field,
178 { msg->GetReflection()->SetDouble(msg, field, boost::any_cast<type>(value)); }
179 void _add_value(
const google::protobuf::FieldDescriptor* field,
182 { msg->GetReflection()->AddDouble(msg, field, boost::any_cast<type>(value)); }
190 std::string
as_str() {
return "CPPTYPE_FLOAT"; }
192 boost::any _get_value(
193 const google::protobuf::FieldDescriptor* field,
195 {
return msg.GetReflection()->GetFloat(msg, field); }
196 boost::any _get_repeated_value(
197 const google::protobuf::FieldDescriptor* field,
200 {
return msg.GetReflection()->GetRepeatedFloat(msg, field, index); }
201 void _set_value(
const google::protobuf::FieldDescriptor* field,
204 { msg->GetReflection()->SetFloat(msg, field, boost::any_cast<type>(value)); }
205 void _add_value(
const google::protobuf::FieldDescriptor* field,
208 { msg->GetReflection()->AddFloat(msg, field, boost::any_cast<type>(value)); }
214 typedef google::protobuf::int32 type;
215 std::string
as_str() {
return "CPPTYPE_INT32"; }
217 boost::any _get_value(
218 const google::protobuf::FieldDescriptor* field,
220 {
return msg.GetReflection()->GetInt32(msg, field); }
221 boost::any _get_repeated_value(
222 const google::protobuf::FieldDescriptor* field,
225 {
return msg.GetReflection()->GetRepeatedInt32(msg, field, index); }
226 void _set_value(
const google::protobuf::FieldDescriptor* field,
229 { msg->GetReflection()->SetInt32(msg, field, boost::any_cast<type>(value)); }
230 void _add_value(
const google::protobuf::FieldDescriptor* field,
233 { msg->GetReflection()->AddInt32(msg, field, boost::any_cast<type>(value)); }
239 typedef google::protobuf::int64 type;
240 std::string
as_str() {
return "CPPTYPE_INT64"; }
242 boost::any _get_value(
243 const google::protobuf::FieldDescriptor* field,
245 {
return msg.GetReflection()->GetInt64(msg, field); }
246 boost::any _get_repeated_value(
247 const google::protobuf::FieldDescriptor* field,
250 {
return msg.GetReflection()->GetRepeatedInt64(msg, field, index); }
251 void _set_value(
const google::protobuf::FieldDescriptor* field,
254 { msg->GetReflection()->SetInt64(msg, field, boost::any_cast<type>(value)); }
255 void _add_value(
const google::protobuf::FieldDescriptor* field,
258 { msg->GetReflection()->AddInt64(msg, field, boost::any_cast<type>(value)); }
264 typedef google::protobuf::uint32 type;
265 std::string
as_str() {
return "CPPTYPE_UINT32"; }
267 boost::any _get_value(
268 const google::protobuf::FieldDescriptor* field,
270 {
return msg.GetReflection()->GetUInt32(msg, field); }
271 boost::any _get_repeated_value(
272 const google::protobuf::FieldDescriptor* field,
275 {
return msg.GetReflection()->GetRepeatedUInt32(msg, field, index); }
276 void _set_value(
const google::protobuf::FieldDescriptor* field,
279 { msg->GetReflection()->SetUInt32(msg, field, boost::any_cast<type>(value)); }
280 void _add_value(
const google::protobuf::FieldDescriptor* field,
283 { msg->GetReflection()->AddUInt32(msg, field, boost::any_cast<type>(value)); }
289 typedef google::protobuf::uint64 type;
291 std::string
as_str() {
return "CPPTYPE_UINT64"; }
293 boost::any _get_value(
294 const google::protobuf::FieldDescriptor* field,
296 {
return msg.GetReflection()->GetUInt64(msg, field); }
297 boost::any _get_repeated_value(
298 const google::protobuf::FieldDescriptor* field,
301 {
return msg.GetReflection()->GetRepeatedUInt64(msg, field, index); }
302 void _set_value(
const google::protobuf::FieldDescriptor* field,
305 { msg->GetReflection()->SetUInt64(msg, field, boost::any_cast<type>(value)); }
306 void _add_value(
const google::protobuf::FieldDescriptor* field,
309 { msg->GetReflection()->AddUInt64(msg, field, boost::any_cast<type>(value)); }
316 std::string
as_str() {
return "CPPTYPE_BOOL"; }
318 boost::any _get_value(
319 const google::protobuf::FieldDescriptor* field,
321 {
return msg.GetReflection()->GetBool(msg, field); }
322 boost::any _get_repeated_value(
323 const google::protobuf::FieldDescriptor* field,
326 {
return msg.GetReflection()->GetRepeatedBool(msg, field, index); }
327 void _set_value(
const google::protobuf::FieldDescriptor* field,
330 { msg->GetReflection()->SetBool(msg, field, boost::any_cast<type>(value)); }
331 void _add_value(
const google::protobuf::FieldDescriptor* field,
334 { msg->GetReflection()->AddBool(msg, field, boost::any_cast<type>(value)); }
340 typedef std::string type;
341 std::string
as_str() {
return "CPPTYPE_STRING"; }
343 boost::any _get_value(
344 const google::protobuf::FieldDescriptor* field,
346 {
return msg.GetReflection()->GetString(msg, field); }
347 boost::any _get_repeated_value(
348 const google::protobuf::FieldDescriptor* field,
351 {
return msg.GetReflection()->GetRepeatedString(msg, field, index); }
352 void _set_value(
const google::protobuf::FieldDescriptor* field,
355 { msg->GetReflection()->SetString(msg, field, boost::any_cast<type>(value)); }
356 void _add_value(
const google::protobuf::FieldDescriptor* field,
359 { msg->GetReflection()->AddString(msg, field, boost::any_cast<type>(value)); }
366 typedef const google::protobuf::EnumValueDescriptor* const_type;
367 typedef google::protobuf::EnumValueDescriptor* mutable_type;
369 std::string
as_str() {
return "CPPTYPE_ENUM"; }
371 boost::any _get_value(
372 const google::protobuf::FieldDescriptor* field,
374 {
return msg.GetReflection()->GetEnum(msg, field); }
375 boost::any _get_repeated_value(
376 const google::protobuf::FieldDescriptor* field,
379 {
return msg.GetReflection()->GetRepeatedEnum(msg, field, index); }
380 void _set_value(
const google::protobuf::FieldDescriptor* field,
383 { msg->GetReflection()->SetEnum(msg, field, boost::any_cast<const_type>(value)); }
384 void _add_value(
const google::protobuf::FieldDescriptor* field,
387 { msg->GetReflection()->AddEnum(msg, field, boost::any_cast<const_type>(value)); }
399 std::string
as_str() {
return "CPPTYPE_MESSAGE"; }
402 virtual boost::any _get_value(
403 const google::protobuf::FieldDescriptor* field,
407 return &(msg.GetReflection()->GetMessage(msg, field));
412 virtual boost::any _get_repeated_value(
413 const google::protobuf::FieldDescriptor* field,
416 {
return &(msg.GetReflection()->GetRepeatedMessage(msg, field, index)); }
419 virtual void _set_value(
const google::protobuf::FieldDescriptor* field,
425 const_type p = boost::any_cast<const_type>(value);
428 msg->GetReflection()->MutableMessage(msg, field)->MergeFrom(*p);
433 catch(boost::bad_any_cast& e)
435 mutable_type p = boost::any_cast<mutable_type>(value);
437 msg->GetReflection()->MutableMessage(msg, field)->MergeFrom(*p);
443 virtual void _add_value(
const google::protobuf::FieldDescriptor* field,
449 const_type p = boost::any_cast<const_type>(value);
450 msg->GetReflection()->AddMessage(msg, field)->MergeFrom(*p);
452 catch(boost::bad_any_cast& e)
454 mutable_type p = boost::any_cast<mutable_type>(value);
455 msg->GetReflection()->AddMessage(msg, field)->MergeFrom(*p);
462 template<
typename CustomMessage>
466 typedef CustomMessage type;
471 virtual boost::any _get_value(
472 const google::protobuf::FieldDescriptor* field,
475 Parent::const_type p = boost::any_cast<Parent::const_type>(Parent::_get_value(field, msg));
481 virtual boost::any _get_repeated_value(
482 const google::protobuf::FieldDescriptor* field,
486 Parent::const_type p = boost::any_cast<Parent::const_type>(Parent::_get_repeated_value(field, msg, index));
492 virtual void _set_value(
const google::protobuf::FieldDescriptor* field,
496 type v = boost::any_cast<type>(value);
497 Parent::const_type p = &v;
498 Parent::_set_value(field, msg, p);
500 virtual void _add_value(
const google::protobuf::FieldDescriptor* field,
504 type v = boost::any_cast<type>(value);
505 Parent::const_type p = &v;
506 Parent::_add_value(field, msg, p);
518 static google::protobuf::FieldDescriptor::CppType as_enum()
519 {
return google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE; }
525 static google::protobuf::FieldDescriptor::CppType as_enum()
526 {
return google::protobuf::FieldDescriptor::CPPTYPE_FLOAT; }
532 static google::protobuf::FieldDescriptor::CppType as_enum()
533 {
return google::protobuf::FieldDescriptor::CPPTYPE_INT32; }
539 static google::protobuf::FieldDescriptor::CppType as_enum()
540 {
return google::protobuf::FieldDescriptor::CPPTYPE_UINT32; }
546 static google::protobuf::FieldDescriptor::CppType as_enum()
547 {
return google::protobuf::FieldDescriptor::CPPTYPE_INT64; }
553 static google::protobuf::FieldDescriptor::CppType as_enum()
554 {
return google::protobuf::FieldDescriptor::CPPTYPE_UINT64; }
560 static google::protobuf::FieldDescriptor::CppType as_enum()
561 {
return google::protobuf::FieldDescriptor::CPPTYPE_STRING; }
567 static google::protobuf::FieldDescriptor::CppType as_enum()
568 {
return google::protobuf::FieldDescriptor::CPPTYPE_ENUM; }
575 static google::protobuf::FieldDescriptor::CppType as_enum()
576 {
return google::protobuf::FieldDescriptor::CPPTYPE_BOOL; }
583 static google::protobuf::FieldDescriptor::CppType as_enum()
584 {
return google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE; }