LCOV - code coverage report
Current view: top level - DCPS/XTypes - TypeAssignability.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 18 83.3 %
Date: 2023-04-30 01:32:43 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :  * Distributed under the OpenDDS License.
       3             :  * See: http://www.opendds.org/license.html
       4             :  */
       5             : 
       6             : #ifndef OPENDDS_DCPS_XTYPES_TYPE_ASSIGNABILITY_H
       7             : #define OPENDDS_DCPS_XTYPES_TYPE_ASSIGNABILITY_H
       8             : 
       9             : #include "TypeObject.h"
      10             : #include "TypeLookupService.h"
      11             : 
      12             : #include <utility>
      13             : #include <map>
      14             : #include <cmath>
      15             : 
      16             : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
      17             : 
      18             : namespace OpenDDS {
      19             : namespace XTypes {
      20             : 
      21             : // Set of pairs of members with each pair contains members from
      22             : // two structure types that have the same member ID and name
      23             : // (or have the same member ID if ignore_member_names is true)
      24             : typedef std::pair<const MinimalStructMember*, const MinimalStructMember*> MemberPair;
      25             : typedef OPENDDS_VECTOR(MemberPair) MatchedSet;
      26             : 
      27             : // From the reader's TypeConsistencyEnforcementQosPolicy
      28             : struct TypeConsistencyAttributes {
      29             :   bool prevent_type_widening;
      30             :   bool ignore_sequence_bounds;
      31             :   bool ignore_string_bounds;
      32             :   bool ignore_member_names; // Only this affects assignability currently
      33             : };
      34             : 
      35             : class OpenDDS_Dcps_Export TypeAssignability {
      36             : public:
      37          39 :   explicit TypeAssignability(TypeLookupService_rch tls)
      38          39 :     : tl_service_(tls)
      39             :   {
      40             :     // Use default type consistency values
      41          39 :     type_consistency_.prevent_type_widening = false;
      42          39 :     type_consistency_.ignore_sequence_bounds = true;
      43          39 :     type_consistency_.ignore_string_bounds = true;
      44          39 :     type_consistency_.ignore_member_names = false;
      45          39 :   }
      46             : 
      47           0 :   TypeAssignability(TypeLookupService_rch tls,
      48             :                     TypeConsistencyAttributes type_consistency)
      49           0 :     : tl_service_(tls)
      50           0 :     , type_consistency_(type_consistency) {}
      51             : 
      52             :   // Check whether ta is-assignable-from tb
      53             :   bool assignable(const TypeObject& ta, const TypeObject& tb) const;
      54             :   bool assignable(const TypeObject& ta, const TypeIdentifier& tb) const;
      55             :   bool assignable(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
      56             :   bool assignable(const TypeIdentifier& ta, const TypeObject& tb) const;
      57             : 
      58             :   // The following set_* functions, if called prior to the assignable
      59             :   // functions, can change the behavior of type assignability
      60             : 
      61             :   // No affect on assignability currently
      62             :   void set_prevent_type_widening(bool value)
      63             :   {
      64             :     type_consistency_.prevent_type_widening = value;
      65             :   }
      66             : 
      67             :   // No affect on assignability currently
      68             :   void set_ignore_sequence_bounds(bool value)
      69             :   {
      70             :     type_consistency_.ignore_sequence_bounds = value;
      71             :   }
      72             : 
      73             :   // No affect on assignability currently
      74             :   void set_ignore_string_bounds(bool value)
      75             :   {
      76             :     type_consistency_.ignore_string_bounds = value;
      77             :   }
      78             : 
      79             :   // Currently, only this affects assignability's behavior
      80           2 :   void set_ignore_member_names(bool value)
      81             :   {
      82           2 :     type_consistency_.ignore_member_names = value;
      83           2 :   }
      84             : 
      85         104 :   void insert_entry(const TypeIdentifier& ti, const TypeObject& tobj)
      86             :   {
      87         104 :     tl_service_->add(ti, tobj);
      88         104 :   }
      89             : 
      90             : private:
      91             :   bool assignable_alias(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
      92             :   bool assignable_annotation(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
      93             :   bool assignable_annotation(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
      94             :   bool assignable_struct(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
      95             :   bool assignable_struct(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
      96             :   bool assignable_union(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
      97             :   bool assignable_union(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
      98             :   bool assignable_bitset(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
      99             :   bool assignable_bitset(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     100             :   bool assignable_sequence(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     101             :   bool assignable_sequence(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     102             :   bool assignable_array(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     103             :   bool assignable_array(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     104             :   bool assignable_map(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     105             :   bool assignable_map(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     106             :   bool assignable_enum(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     107             :   bool assignable_enum(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     108             :   bool assignable_bitmask(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     109             :   bool assignable_bitmask(const MinimalTypeObject& ta, const TypeIdentifier& tb) const;
     110             :   bool assignable_extended(const MinimalTypeObject& ta, const MinimalTypeObject& tb) const;
     111             : 
     112             :   bool assignable_primitive(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     113             :   bool assignable_primitive(const TypeIdentifier& ta, const MinimalTypeObject& tb) const;
     114             :   bool assignable_string(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     115             :   bool assignable_string(const TypeIdentifier& ta, const MinimalTypeObject& tb) const;
     116             :   bool assignable_plain_sequence(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     117             :   bool assignable_plain_sequence(const TypeIdentifier& ta, const MinimalTypeObject& tb) const;
     118             :   bool assignable_plain_array(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     119             :   bool assignable_plain_array(const TypeIdentifier& ta, const MinimalTypeObject& tb) const;
     120             :   bool assignable_plain_map(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     121             :   bool assignable_plain_map(const TypeIdentifier& ta, const MinimalTypeObject& tb) const;
     122             : 
     123             :   // General helpers
     124             :   bool strongly_assignable(const TypeIdentifier& ta, const TypeIdentifier& tb) const;
     125             :   bool is_delimited(const TypeIdentifier& ti) const;
     126             :   bool is_delimited(const MinimalTypeObject& tobj) const;
     127             :   bool is_delimited_with_flags(TypeFlag flags) const;
     128             :   bool equal_type_id(const TypeIdentifier& tia, const TypeIdentifier& tib) const;
     129             :   const TypeIdentifier& get_base_type(const MinimalTypeObject& type) const;
     130             : 
     131             :   // Helpers for assignability of struct
     132             :   void erase_key(MinimalTypeObject& type) const;
     133             :   void hold_key(MinimalTypeObject& type) const;
     134             :   bool hold_key(const TypeIdentifier& ti, MinimalTypeObject& to) const;
     135             :   bool struct_rule_enum_key(const MinimalTypeObject& tb, const CommonStructMember& ma) const;
     136             :   bool get_sequence_bound(LBound& b, const CommonStructMember& m) const;
     137             :   bool get_map_bound(LBound& b, const CommonStructMember& m) const;
     138             :   bool get_string_bound(LBound& b, const CommonStructMember& m) const;
     139             :   bool get_struct_member(const MinimalTypeObject*& ret, const CommonStructMember& m) const;
     140             :   bool get_union_member(const MinimalTypeObject*& ret, const CommonStructMember& m) const;
     141             : 
     142         620 :   const MinimalTypeObject& lookup_minimal(const TypeIdentifier& ti) const
     143             :   {
     144         620 :     return tl_service_->get_type_object(ti).minimal;
     145             :   }
     146             : 
     147             :   XTypes::TypeLookupService_rch tl_service_;
     148             : 
     149             :   // For now, type assignability applies only ignore_member_names.
     150             :   // In the future, it needs to consider other attibutes as well:
     151             :   // prevent_type_widening, ignore_sequence_bounds, ignore_string_bounds.
     152             :   TypeConsistencyAttributes type_consistency_;
     153             : };
     154             : 
     155             : } // namepace XTypes
     156             : } // namespace OpenDDS
     157             : 
     158             : OPENDDS_END_VERSIONED_NAMESPACE_DECL
     159             : 
     160             : #endif /* OPENDDS_DCPS_XTYPES_TYPE_ASSIGNABILITY_H */

Generated by: LCOV version 1.16