-
Couldn't load subscription status.
- Fork 71
allow opt-in for new unset field semantics on csp.Struct (disc #536) #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
195165f
345f37f
4acbb08
2af2edf
685bf73
a8751cf
6816484
2603e73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
| #include <csp/core/System.h> | ||
| #include <csp/engine/Struct.h> | ||
| #include <algorithm> | ||
| #include <ranges> | ||
| #include <string> | ||
|
|
||
| namespace csp | ||
| { | ||
|
|
||
| StructField::StructField( CspTypePtr type, const std::string & fieldname, | ||
| size_t size, size_t alignment ) : | ||
| size_t size, size_t alignment, bool isOptional ) : | ||
| m_fieldname( fieldname ), | ||
| m_offset( 0 ), | ||
| m_size( size ), | ||
| m_alignment( alignment ), | ||
| m_maskOffset( 0 ), | ||
| m_maskBit( 0 ), | ||
| m_maskBitMask( 0 ), | ||
| m_type( type ) | ||
| m_type( type ), | ||
| m_isOptional( isOptional ) | ||
| { | ||
| } | ||
|
|
||
|
|
@@ -33,8 +36,8 @@ and adjustments required for the hidden fields | |
|
|
||
| */ | ||
|
|
||
| StructMeta::StructMeta( const std::string & name, const Fields & fields, | ||
| std::shared_ptr<StructMeta> base ) : m_name( name ), m_base( base ), m_fields( fields ), | ||
| StructMeta::StructMeta( const std::string & name, const Fields & fields, bool isStrict, | ||
| std::shared_ptr<StructMeta> base ) : m_name( name ), m_base( base ), m_isStrict( isStrict ), m_fields( fields ), | ||
| m_size( 0 ), m_partialSize( 0 ), m_partialStart( 0 ), m_nativeStart( 0 ), m_basePadding( 0 ), | ||
| m_maskLoc( 0 ), m_maskSize( 0 ), m_firstPartialField( 0 ), m_firstNativePartialField( 0 ), | ||
| m_isPartialNative( true ), m_isFullyNative( true ) | ||
|
|
@@ -494,6 +497,43 @@ void StructMeta::destroy( Struct * s ) const | |
| m_base -> destroy( s ); | ||
| } | ||
|
|
||
| void StructMeta::validate( const Struct * s ) const | ||
| { | ||
| bool encountered_non_strict = false; | ||
|
|
||
| for( const StructMeta * cur = this; cur; cur = cur -> m_base.get() ) | ||
| { | ||
| encountered_non_strict |= !cur -> isStrict(); | ||
| if( !cur -> isStrict() ) { | ||
| continue; | ||
| } | ||
|
|
||
| // rule 1: a non-strict struct may not inherit (directly or indirectly) from a strict base | ||
| if (encountered_non_strict) | ||
| CSP_THROW( ValueError, "Struct '" << s -> meta() -> name() << "' has non-strict inheritance of strict base '" << cur -> name() << "'" ); | ||
|
|
||
| // rule 2: all local fields are set | ||
| std::string missing_fields; | ||
|
||
| for(const auto& field : cur->m_fields) { | ||
| if(!field->isSet(s)) { | ||
| if(missing_fields.empty()) { | ||
| missing_fields = field->fieldname(); | ||
| } else { | ||
| missing_fields += ", " + field->fieldname(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // raise error if any fields are missing | ||
| if (!missing_fields.empty()) { | ||
| CSP_THROW(ValueError, | ||
| "Strict struct '" << cur->name() << "' missing required fields: " << missing_fields); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Struct::Struct( const std::shared_ptr<const StructMeta> & meta ) | ||
| { | ||
| //Initialize meta shared_ptr | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to be careful on all of the realtime adapters that are now validating. Keep in mind this is happening on a separate thread and an uncaught exception will crash the process.
Just need to make sure all of these validation calls are already under existing try/catch blocks ( some of them may already be )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some new changes that hopefully deal with this, though a sanity check is definitely needed. Left some corresponding comments as well.