LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/sandesh/compiler/parse - t_field.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 77 93 82.8 %
Date: 2026-11-09 02:10:49 Functions: 18 24 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Licensed to the Apache Software Foundation (ASF) under one
       3             :  * or more contributor license agreements. See the NOTICE file
       4             :  * distributed with this work for additional information
       5             :  * regarding copyright ownership. The ASF licenses this file
       6             :  * to you under the Apache License, Version 2.0 (the
       7             :  * "License"); you may not use this file except in compliance
       8             :  * with the License. You may obtain a copy of the License at
       9             :  *
      10             :  *   http://www.apache.org/licenses/LICENSE-2.0
      11             :  *
      12             :  * Unless required by applicable law or agreed to in writing,
      13             :  * software distributed under the License is distributed on an
      14             :  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      15             :  * KIND, either express or implied. See the License for the
      16             :  * specific language governing permissions and limitations
      17             :  * under the License.
      18             :  *
      19             :  * Modifications Copyright (c) Juniper Networks, Inc.
      20             :  * Copyright Contributors to the OpenSDN project
      21             :  */
      22             : 
      23             : #ifndef T_FIELD_H
      24             : #define T_FIELD_H
      25             : 
      26             : #include <string>
      27             : #include <sstream>
      28             : 
      29             : #include "t_doc.h"
      30             : 
      31             : // Forward declare for xsd_attrs
      32             : class t_struct;
      33             : 
      34             : /**
      35             :  * Class to represent a field in a thrift structure. A field has a data type,
      36             :  * a symbolic name, and a numeric identifier.
      37             :  *
      38             :  */
      39             : class t_field : public t_doc {
      40             :  public:
      41       13673 :   t_field(t_type* type, std::string name) :
      42       13673 :     type_(type),
      43       13673 :     name_(name),
      44       13673 :     key_(0),
      45             : #ifdef SANDESH
      46       13673 :     req_(T_OPT_IN_REQ_OUT),
      47             : #endif
      48       13673 :     value_(NULL),
      49       13673 :     xsd_optional_(false),
      50       13673 :     xsd_nillable_(false),
      51       13673 :     xsd_attrs_(NULL)
      52             : #ifdef SANDESH
      53       13673 :     , auto_generated_(false)
      54             : #endif
      55       13673 :   {}
      56             : 
      57       88724 :   t_field(t_type* type, std::string name, int32_t key) :
      58       88724 :     type_(type),
      59       88724 :     name_(name),
      60       88724 :     key_(key),
      61       88724 :     req_(T_OPT_IN_REQ_OUT),
      62       88724 :     value_(NULL),
      63       88724 :     xsd_optional_(false),
      64       88724 :     xsd_nillable_(false),
      65       88724 :     xsd_attrs_(NULL)
      66             : #ifdef SANDESH
      67       88724 :     , auto_generated_(false)
      68             : #endif
      69       88724 :   {}
      70             : 
      71             : #ifdef SANDESH
      72       12180 :   t_field(t_type* type, std::string name, int32_t key, bool auto_generated) :
      73       12180 :     type_(type),
      74       12180 :     name_(name),
      75       12180 :     key_(key),
      76       12180 :     req_(T_OPT_IN_REQ_OUT),
      77       12180 :     value_(NULL),
      78       12180 :     xsd_optional_(false),
      79       12180 :     xsd_nillable_(false),
      80       12180 :     xsd_attrs_(NULL),
      81       24360 :     auto_generated_(auto_generated){}
      82             : 
      83        4326 :   static std::string auto_string_name(int32_t key) {
      84        4326 :       std::string str;
      85             :       char buf[20];
      86        4326 :       snprintf(buf, 20, "str%d", key);
      87        4326 :       str = buf;
      88        8652 :       return str;
      89           0 :   }
      90             : #endif
      91             : 
      92       13673 :   virtual ~t_field() {}
      93             : 
      94      619397 :   t_type* get_type() const {
      95      619397 :     return type_;
      96             :   }
      97             : 
      98      780557 :   const std::string& get_name() const {
      99      780557 :     return name_;
     100             :   }
     101             : 
     102      504285 :   int32_t get_key() const {
     103      504285 :     return key_;
     104             :   }
     105             : 
     106             :   enum e_req {
     107             :     T_REQUIRED,
     108             :     T_OPTIONAL,
     109             :     T_OPT_IN_REQ_OUT
     110             :   };
     111             : 
     112       88724 :   void set_req(e_req req) {
     113       88724 :     req_ = req;
     114       88724 :   }
     115             : 
     116      421867 :   e_req get_req() const {
     117      421867 :     return req_;
     118             :   }
     119             : 
     120        4494 :   void set_value(t_const_value* value) {
     121        4494 :     value_ = value;
     122        4494 :   }
     123             : 
     124       29899 :   t_const_value* get_value() {
     125       29899 :     return value_;
     126             :   }
     127             : 
     128       84398 :   void set_xsd_optional(bool xsd_optional) {
     129       84398 :     xsd_optional_ = xsd_optional;
     130       84398 :   }
     131             : 
     132           0 :   bool get_xsd_optional() const {
     133           0 :     return xsd_optional_;
     134             :   }
     135             : 
     136       84398 :   void set_xsd_nillable(bool xsd_nillable) {
     137       84398 :     xsd_nillable_ = xsd_nillable;
     138       84398 :   }
     139             : 
     140           0 :   bool get_xsd_nillable() const {
     141           0 :     return xsd_nillable_;
     142             :   }
     143             : 
     144           0 :   void set_xsd_attrs(t_struct* xsd_attrs) {
     145           0 :     xsd_attrs_ = xsd_attrs;
     146           0 :   }
     147             : 
     148           0 :   t_struct* get_xsd_attrs() {
     149           0 :     return xsd_attrs_;
     150             :   }
     151             : 
     152             : #ifdef SANDESH
     153      104070 :   bool get_auto_generated() const {
     154      104070 :     return auto_generated_;
     155             :   }
     156             : 
     157             :   void set_auto_generated(bool auto_generated) {
     158             :     auto_generated_ = auto_generated;
     159             :   }
     160             : #endif
     161             : 
     162             :   // This is not the same function as t_type::get_fingerprint_material,
     163             :   // but it does the same thing.
     164      182010 :   std::string get_fingerprint_material() const {
     165      182010 :     std::ostringstream keystm;
     166      182010 :     keystm << key_;
     167      364020 :     return keystm.str() + ":" +
     168      182010 :       ((req_ == T_OPTIONAL) ? "opt-" : "") +
     169      728040 :       type_->get_fingerprint_material();
     170      182010 :   }
     171             : 
     172             :   /**
     173             :    * Comparator to sort fields in ascending order by key.
     174             :    * Make this a functor instead of a function to help GCC inline it.
     175             :    * The arguments are (const) references to const pointers to const t_fields.
     176             :    */
     177             :   struct key_compare {
     178      219192 :     bool operator()(t_field const * const & a, t_field const * const & b) const {
     179      219192 :       return a->get_key() < b->get_key();
     180             :     }
     181             :   };
     182             : 
     183             : #ifdef SANDESH
     184       11279 :   virtual bool has_key_annotation() const {
     185       11279 :     std::map<std::string, std::string>::const_iterator it;
     186       11279 :     it = annotations_.find("key");
     187       11279 :     if (it != annotations_.end()) {
     188         765 :       return true;
     189             :     }
     190       10514 :     return false;
     191             :   }
     192           0 :   virtual bool has_hidden_annotation() const {
     193           0 :     std::map<std::string, std::string>::const_iterator it;
     194           0 :     it = annotations_.find("hidden");
     195           0 :     if (it != annotations_.end()) {
     196           0 :       return true;
     197             :     }
     198           0 :     return false;
     199             :   }
     200             : #endif
     201             : 
     202             :   std::map<std::string, std::string> annotations_;
     203             : 
     204             :  private:
     205             :   t_type* type_;
     206             :   std::string name_;
     207             :   int32_t key_;
     208             :   e_req req_;
     209             :   t_const_value* value_;
     210             : 
     211             :   bool xsd_optional_;
     212             :   bool xsd_nillable_;
     213             :   t_struct* xsd_attrs_;
     214             : #ifdef SANDESH
     215             :   bool auto_generated_;
     216             : #endif
     217             : 
     218             : };
     219             : 
     220             : /**
     221             :  * A simple struct for the parser to use to store a field ID, and whether or
     222             :  * not it was specified by the user or automatically chosen.
     223             :  */
     224             : struct t_field_id {
     225             :   int64_t value;
     226             :   bool auto_assigned;
     227             : };
     228             : 
     229             : #endif

Generated by: LCOV version 1.14