@@ -35,6 +35,8 @@ class StructField
35
35
36
36
bool isNative () const { return m_type -> type () <= CspType::Type::MAX_NATIVE_TYPE; }
37
37
38
+ bool isOptional () const { return m_isOptional; }
39
+
38
40
void setOffset ( size_t off ) { m_offset = off; }
39
41
void setMaskOffset ( size_t off, uint8_t bit )
40
42
{
@@ -75,7 +77,7 @@ class StructField
75
77
protected:
76
78
77
79
StructField ( CspTypePtr type, const std::string & fieldname,
78
- size_t size, size_t alignment );
80
+ size_t size, size_t alignment, bool isOptional );
79
81
80
82
void setIsSet ( Struct * s ) const
81
83
{
@@ -108,6 +110,7 @@ class StructField
108
110
uint8_t m_maskBit;
109
111
uint8_t m_maskBitMask;
110
112
CspTypePtr m_type;
113
+ const bool m_isOptional;
111
114
};
112
115
113
116
using StructFieldPtr = std::shared_ptr<StructField>;
@@ -120,7 +123,7 @@ class NativeStructField : public StructField
120
123
121
124
public:
122
125
NativeStructField () {}
123
- NativeStructField ( const std::string & fieldname ) : NativeStructField( CspType::fromCType<T>::type(), fieldname )
126
+ NativeStructField ( const std::string & fieldname, bool isOptional ) : NativeStructField( CspType::fromCType<T>::type(), fieldname, isOptional )
124
127
{
125
128
}
126
129
@@ -157,7 +160,7 @@ class NativeStructField : public StructField
157
160
}
158
161
159
162
protected:
160
- NativeStructField ( CspTypePtr type, const std::string & fieldname ) : StructField( type, fieldname, sizeof ( T ), alignof ( T ) )
163
+ NativeStructField ( CspTypePtr type, const std::string & fieldname, bool isOptional ) : StructField( type, fieldname, sizeof ( T ), alignof ( T ), isOptional )
161
164
{}
162
165
};
163
166
@@ -179,7 +182,8 @@ using TimeStructField = NativeStructField<Time>;
179
182
class CspEnumStructField final : public NativeStructField<CspEnum>
180
183
{
181
184
public:
182
- CspEnumStructField ( CspTypePtr type, const std::string & fieldname ) : NativeStructField( type, fieldname )
185
+ CspEnumStructField ( CspTypePtr type, const std::string & fieldname, bool isOptional ) : NativeStructField( type,
186
+ fieldname, isOptional )
183
187
{}
184
188
};
185
189
@@ -218,8 +222,8 @@ class NotImplementedStructField : public StructField
218
222
class NonNativeStructField : public StructField
219
223
{
220
224
public:
221
- NonNativeStructField ( CspTypePtr type, const std::string &fieldname, size_t size, size_t alignment ) :
222
- StructField ( type, fieldname, size, alignment )
225
+ NonNativeStructField ( CspTypePtr type, const std::string &fieldname, size_t size, size_t alignment, bool isOptional ) :
226
+ StructField ( type, fieldname, size, alignment, isOptional )
223
227
{}
224
228
225
229
virtual void initialize ( Struct * s ) const = 0;
@@ -244,8 +248,8 @@ class StringStructField final : public NonNativeStructField
244
248
public:
245
249
using CType = csp::CspType::StringCType;
246
250
247
- StringStructField ( CspTypePtr type, const std::string & fieldname ) :
248
- NonNativeStructField ( type, fieldname, sizeof ( CType ), alignof ( CType ) )
251
+ StringStructField ( CspTypePtr type, const std::string & fieldname, bool isOptional ) :
252
+ NonNativeStructField ( type, fieldname, sizeof ( CType ), alignof ( CType ), isOptional )
249
253
{}
250
254
251
255
void initialize ( Struct * s ) const override
@@ -343,8 +347,8 @@ class ArrayStructField : public NonNativeStructField
343
347
}
344
348
345
349
public:
346
- ArrayStructField ( CspTypePtr arrayType, const std::string & fieldname ) :
347
- NonNativeStructField( arrayType, fieldname, sizeof ( CType ), alignof( CType ) )
350
+ ArrayStructField ( CspTypePtr arrayType, const std::string & fieldname, bool isOptional ) :
351
+ NonNativeStructField( arrayType, fieldname, sizeof ( CType ), alignof( CType ), isOptional )
348
352
{}
349
353
350
354
const CType & value ( const Struct * s ) const
@@ -421,8 +425,8 @@ class ArrayStructField : public NonNativeStructField
421
425
class DialectGenericStructField : public NonNativeStructField
422
426
{
423
427
public:
424
- DialectGenericStructField ( const std::string & fieldname, size_t size, size_t alignment ) :
425
- NonNativeStructField ( CspType::DIALECT_GENERIC(), fieldname, size, alignment )
428
+ DialectGenericStructField ( const std::string & fieldname, size_t size, size_t alignment, bool isOptional ) :
429
+ NonNativeStructField ( CspType::DIALECT_GENERIC(), fieldname, size, alignment, isOptional )
426
430
{}
427
431
428
432
const DialectGenericType & value ( const Struct * s ) const
@@ -587,21 +591,24 @@ class StructMeta : public std::enable_shared_from_this<StructMeta>
587
591
using FieldNames = std::vector<std::string>;
588
592
589
593
// Fields will be re-arranged and assigned their offsets in StructMeta for optimal performance
590
- StructMeta ( const std::string & name, const Fields & fields, std::shared_ptr<StructMeta> base = nullptr );
594
+ StructMeta ( const std::string & name, const Fields & fields, bool isStrict, std::shared_ptr<StructMeta> base = nullptr );
591
595
virtual ~StructMeta ();
592
596
593
597
const std::string & name () const { return m_name; }
594
598
size_t size () const { return m_size; }
595
599
size_t partialSize () const { return m_partialSize; }
596
600
597
601
bool isNative () const { return m_isFullyNative; }
602
+ bool isStrict () const { return m_isStrict; }
598
603
599
604
const Fields & fields () const { return m_fields; }
600
605
const FieldNames & fieldNames () const { return m_fieldnames; }
601
606
602
607
size_t maskLoc () const { return m_maskLoc; }
603
608
size_t maskSize () const { return m_maskSize; }
604
609
610
+ void validate ( const Struct * s ) const ;
611
+
605
612
const StructFieldPtr & field ( const char * name ) const
606
613
{
607
614
static StructFieldPtr s_empty;
@@ -652,7 +659,8 @@ class StructMeta : public std::enable_shared_from_this<StructMeta>
652
659
std::shared_ptr<StructMeta> m_base;
653
660
StructPtr m_default;
654
661
FieldMap m_fieldMap;
655
-
662
+ bool m_isStrict;
663
+
656
664
// fields in order, memory owners of field objects which in turn own the key memory
657
665
// m_fields includes all base fields as well. m_fieldnames maintains the proper iteration order of fields
658
666
Fields m_fields;
@@ -738,6 +746,11 @@ class Struct
738
746
return meta () -> allFieldsSet ( this );
739
747
}
740
748
749
+ void validate () const
750
+ {
751
+ meta () -> validate ( this );
752
+ }
753
+
741
754
742
755
// used to cache dialect representations of this struct, if needed
743
756
void * dialectPtr () const { return hidden () -> dialectPtr; }
@@ -822,8 +835,8 @@ bool TypedStructPtr<T>::operator==( const TypedStructPtr<T> & rhs ) const
822
835
class StructStructField final : public NonNativeStructField
823
836
{
824
837
public:
825
- StructStructField ( CspTypePtr cspType, const std::string &fieldname ) :
826
- NonNativeStructField ( cspType, fieldname, sizeof ( StructPtr ), alignof ( StructPtr ) )
838
+ StructStructField ( CspTypePtr cspType, const std::string &fieldname, bool isOptional ) :
839
+ NonNativeStructField ( cspType, fieldname, sizeof ( StructPtr ), alignof ( StructPtr ), isOptional )
827
840
{
828
841
CSP_ASSERT ( cspType -> type () == CspType::Type::STRUCT );
829
842
m_meta = std::static_pointer_cast<const CspStructType>( cspType ) -> meta ();
0 commit comments