8000 Null accessors by lindkvis · Pull Request #93 · lindkvis/caffa · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Null accessors #93

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

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Core/cafChildArrayField.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class ChildArrayField : public ChildArrayFieldHandle

std::string dataType() const override { return PortableDataType<std::vector<DataType>>::name(); }

bool accessible() const override {
return m_fieldDataAccessor != nullptr;
}

void setAccessor( std::unique_ptr<ChildArrayFieldAccessor> accessor ) override
{
m_fieldDataAccessor = std::move( accessor );
Expand Down
72 changes: 68 additions & 4 deletions Core/cafChildArrayField.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ template <typename DataTypePtr>
std::shared_ptr<typename ChildArrayField<DataTypePtr>::DataType> ChildArrayField<DataTypePtr>::operator[]( size_t index ) const
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get object at " + std::to_string( index ) + " for '" + this->keyword() +
"': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

return std::dynamic_pointer_cast<DataType>( m_fieldDataAccessor->at( index ) );
}

Expand All @@ -34,6 +43,14 @@ template <typename DataTypePtr>
void ChildArrayField<DataTypePtr>::push_back( std::shared_ptr<DataType> pointer )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to add object to '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

m_fieldDataAccessor->push_back( pointer );
}

Expand Down Expand Up @@ -63,6 +80,14 @@ void ChildArrayField<DataTypePtr>::insert( size_t index, std::shared_ptr<DataTyp
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to insert object at " + std::to_string( index ) + " in '" + this->keyword() +
"': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

m_fieldDataAccessor->insert( index, pointer );
}

Expand All @@ -73,13 +98,11 @@ template <typename DataTypePtr>
requires is_pointer<DataTypePtr>
void ChildArrayField<DataTypePtr>::insertAt( size_t index, ObjectHandle::Ptr obj )
{
CAFFA_ASSERT( isInitialized() );

auto typedPtr = std::dynamic_pointer_cast<DataType>( obj );
CAFFA_ASSERT( typedPtr );
if ( typedPtr )
{
m_fieldDataAccessor->insert( index, obj );
this->insert( index, typedPtr );
}
}

Expand All @@ -91,7 +114,12 @@ template <typename DataTypePtr>
void ChildArrayField<DataTypePtr>::clear()
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to clear objects from '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}
return m_fieldDataAccessor->clear();
}

Expand All @@ -103,6 +131,14 @@ template <typename DataTypePtr>
void ChildArrayField<DataTypePtr>::erase( size_t index )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to remove object " + std::to_string( index ) + " from '" + this->keyword() +
"': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}
m_fieldDataAccessor->remove( index );
}

Expand Down Expand Up @@ -131,6 +167,13 @@ void ChildArrayField<DataTypePtr>::removeChildObject( ObjectHandle::ConstPtr obj
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to remove object from '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

if ( object )
{
size_t index = m_fieldDataAccessor->index( object );
Expand All @@ -148,6 +191,12 @@ template <typename DataTypePtr>
requires is_pointer<DataTypePtr>
std::vector<ObjectHandle::Ptr> ChildArrayField<DataTypePtr>::childObjects()
{
if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get child objects from '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}
return m_fieldDataAccessor->objects();
}

Expand All @@ -159,6 +208,13 @@ template <typename DataTypePtr>
std::vector<ObjectHandle::ConstPtr> ChildArrayField<DataTypePtr>::childObjects() const
{
const ChildArrayFieldAccessor* accessor = m_fieldDataAccessor.get();

if ( !accessor )
{
std::string errorMessage = "Failed to get child objects from '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}
return accessor->objects();
}
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -207,6 +263,14 @@ ObjectHandle::Ptr ChildArrayField<DataTypePtr>::at( size_t index )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get object at " + std::to_string( index ) + " from '" + this->keyword() +
"': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

return m_fieldDataAccessor->at( index );
}

Expand Down
21 changes: 20 additions & 1 deletion Core/cafChildField.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ class ChildField : public ChildFieldHandle

// Basic access

std::shared_ptr<DataType> object() { return std::dynamic_pointer_cast<DataType>( m_fieldDataAccessor->object() ); }
std::shared_ptr<DataType> object()
{
if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

return std::dynamic_pointer_cast<DataType>( m_fieldDataAccessor->object() );
}
std::shared_ptr<const DataType> object() const
{
if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

return std::dynamic_pointer_cast<const DataType>( m_fieldDataAccessor->object() );
}
void setObject( Ptr ob 6DAF ject );
Expand Down Expand Up @@ -81,6 +98,8 @@ class ChildField : public ChildFieldHandle

std::string dataType() const override { return PortableDataType<DataType>::name(); }

bool accessible() const override { return m_fieldDataAccessor != nullptr; }

void setAccessor( std::unique_ptr<ChildFieldAccessor> accessor ) override
{
m_fieldDataAccessor = std::move( accessor );
Expand Down
53 changes: 53 additions & 0 deletions Core/cafChildField.inl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ template <typename DataTypePtr>
void ChildField<DataTypePtr>::setObject( std::shared_ptr<DataType> object )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to set object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

m_fieldDataAccessor->setObject( object );
}

Expand All @@ -65,6 +73,14 @@ template <typename DataTypePtr>
std::shared_ptr<typename ChildField<DataTypePtr>::DataType> ChildField<DataTypePtr>::deepCloneObject() const
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

auto clonedObject = m_fieldDataAccessor->deepCloneObject();
return std::dynamic_pointer_cast<DataType>( clonedObject );
}
Expand All @@ -77,6 +93,14 @@ template <typename DataTypePtr>
void ChildField<DataTypePtr>::deepCopyObjectFrom( std::shared_ptr<const DataType> copyFrom )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to set object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

m_fieldDataAccessor->deepCopyObjectFrom( copyFrom );
}

Expand All @@ -89,6 +113,13 @@ std::vector<ObjectHandle::Ptr> ChildField<DataTypePtr>::childObjects()
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get child objects for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

auto object = m_fieldDataAccessor->object();
if ( !object ) return {};

Expand All @@ -104,6 +135,13 @@ std::vector<ObjectHandle::ConstPtr> ChildField<DataTypePtr>::childObjects() cons
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get child objects for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

auto object = m_fieldDataAccessor->object();
if ( !object ) return {};

Expand All @@ -118,6 +156,13 @@ template <typename DataTypePtr>
void ChildField<DataTypePtr>::clear()
{
CAFFA_ASSERT( isInitialized() );
if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to clear object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

m_fieldDataAccessor->clear();
}
//--------------------------------------------------------------------------------------------------
Expand All @@ -128,6 +173,14 @@ template <typename DataTypePtr>
void ChildField<DataTypePtr>::removeChildObject( ObjectHandle::ConstPtr object )
{
CAFFA_ASSERT( isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to remove object for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

if ( this->object() == object )
{
this->clear();
Expand Down
51 changes: 31 additions & 20 deletions Core/cafField.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ class Field : public TypedField<DataType>
{
CAFFA_ASSERT( this->isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to get value for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

try
{
return m_fieldDataAccessor->value();
Expand All @@ -98,6 +105,13 @@ class Field : public TypedField<DataType>
{
CAFFA_ASSERT( this->isInitialized() );

if ( !m_fieldDataAccessor )
{
std::string errorMessage = "Failed to set value for '" + this->keyword() + "': Field is not accessible";
CAFFA_ERROR( errorMessage );
throw std::runtime_error( errorMessage );
}

try
{
for ( const auto& validator : m_valueValidators )
Expand Down Expand Up @@ -129,34 +143,31 @@ class Field : public TypedField<DataType>
// Access operators

/*Conversion */
operator DataType() const
{
CAFFA_ASSERT( m_fieldDataAccessor );
return this->value();
}
DataType operator()() const
{
CAFFA_ASSERT( m_fieldDataAccessor );
return this->value();
}
DataType operator*() const
{
CAFFA_ASSERT( m_fieldDataAccessor );
return this->value();
}
operator DataType() const { return this->value(); }
DataType operator()() const { return this->value(); }
DataType operator*() const { return this->value(); }

auto operator<=>( const DataType& fieldValue ) const { return this->value() <=> fieldValue; }

bool accessible() const override {
return m_fieldDataAccessor != nullptr;
}

// Replace accessor
void setAccessor( std::unique_ptr<DataAccessor> accessor ) { m_fieldDataAccessor = std::move( accessor ); }

void setUntypedAccessor( std::unique_ptr<DataFieldAccessorInterface> accessor ) override
{
CAFFA_ASSERT( dynamic_cast<DataAccessor*>( accessor.get() ) != nullptr );

std::unique_ptr<DataAccessor> typedAccessor( dynamic_cast<DataAccessor*>( accessor.release() ) );
CAFFA_ASSERT( typedAccessor );
setAccessor( std::move( typedAccessor ) );
if ( accessor )
{
std::unique_ptr<DataAccessor> typedAccessor( dynamic_cast<DataAccessor*>( accessor.release() ) );
CAFFA_ASSERT( typedAccessor );
setAccessor( std::move( typedAccessor ) );
}
else
{
setAccessor( nullptr );
}
}

template <typename ValidatorType>
Expand Down
5 changes: 5 additions & 0 deletions Core/cafFieldHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class FieldHandle : public ObjectAttribute, public SignalEmitter
*/
virtual void accept( Editor* visitor );

/**
* Can the field be accessed. A non-scriptable field cannot be accessed within the client.
*/
virtual bool accessible() const = 0;

protected:
bool isInitialized() const { return m_ownerObject != nullptr; }

Expand Down
Loading
0