Search code examples
c++protocol-buffersprotobuf-netprotobuf-c

PROTOBUFF INT64 check aganist previously entered data c++


message HealthOccurrenceCount
{
    required int64 HealthID=1; 
    required int32 OccCount=2; 
    optional bytes wci=3;
}

I would like to add data based on HealthID; If HealthID is already entered then instead of adding a new entry, the program should instead just increment the existing entry's OccCount.

HealthOccurrenceCount objHelthOccCount;
if(objHelthOccCount.healthid() == healthID) // Is this right or do I need to iterate all the nodes?
{
    occCount++;  
    objHelthOccCount.set_occcount(occCount);  
}
else       
    occCount = 1;   

Is this code correct or I should convert HealthID into string?

Generated Code:

// required int64 HealthID = 1;
inline bool has_healthid() const;
inline void clear_healthid();
static const int kHealthIDFieldNumber = 1;
inline ::google::protobuf::int64 healthid() const;
inline void set_healthid(::google::protobuf::int64 value);

Solution

  • According to the doc there is a has_ methods for each singular (required or optional) field which return true if that field has been set.

    Your code would then be something like:

            HealthOccurrenceCount objHelthOccCount;
            if(objHelthOccCount.has_healthid())
            {
                occCount++;  
                objHelthOccCount.set_occcount(occCount);  
            }
            else       
                occCount = 1;