PROTOBUFF INT64 check aganist previously entered data c++

纵然是瞬间 提交于 2020-01-07 06:57:36

问题


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);

回答1:


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; 


来源:https://stackoverflow.com/questions/32184021/protobuff-int64-check-aganist-previously-entered-data-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!