Skip to content

Commit 8729de4

Browse files
committed
fixes #488 - allow StructMeta copyFrom / deepcopyFrom / updateFrom to work on base types as well
Signed-off-by: Rob Ambalu <[email protected]>
1 parent c11cf7c commit 8729de4

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

cpp/csp/engine/Struct.cpp

+32-7
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,41 @@ void StructMeta::copyFrom( const Struct * src, Struct * dest )
231231
if( unlikely( src == dest ) )
232232
return;
233233

234+
const StructMeta * meta = dest -> meta();
234235
if( dest -> meta() != src -> meta() &&
235236
!StructMeta::isDerivedType( src -> meta(), dest -> meta() ) )
237+
{
238+
if( StructMeta::isDerivedType( dest -> meta(), src -> meta() ) )
239+
meta = src -> meta();
240+
else
241+
{
236242
CSP_THROW( TypeError, "Attempting to copy from struct type '" << src -> meta() -> name() << "' to struct type '" << dest -> meta() -> name()
237-
<< "'. copy_from may only be used to copy from same type or derived types" );
243+
<< "'. copy_from may only be used to copy from struct with a common base type" );
244+
}
245+
}
238246

239-
dest -> meta() -> copyFromImpl( src, dest, false );
247+
meta -> copyFromImpl( src, dest, false );
240248
}
241249

242250
void StructMeta::deepcopyFrom( const Struct * src, Struct * dest )
243251
{
244252
if( unlikely( src == dest ) )
245253
return;
246254

255+
const StructMeta * meta = dest -> meta();
247256
if( dest -> meta() != src -> meta() &&
248257
!StructMeta::isDerivedType( src -> meta(), dest -> meta() ) )
258+
{
259+
if( StructMeta::isDerivedType( dest -> meta(), src -> meta() ) )
260+
meta = src -> meta();
261+
else
262+
{
249263
CSP_THROW( TypeError, "Attempting to deepcopy from struct type '" << src -> meta() -> name() << "' to struct type '" << dest -> meta() -> name()
250-
<< "'. deepcopy_from may only be used to copy from same type or derived types" );
251-
252-
dest -> meta() -> copyFromImpl( src, dest, true );
264+
<< "'. deepcopy_from may only be used to copy from struct with a common base type" );
265+
}
266+
}
267+
268+
meta -> copyFromImpl( src, dest, true );
253269
}
254270

255271
void StructMeta::copyFromImpl( const Struct * src, Struct * dest, bool deepcopy ) const
@@ -291,12 +307,21 @@ void StructMeta::updateFrom( const Struct * src, Struct * dest )
291307
if( unlikely( src == dest ) )
292308
return;
293309

310+
const StructMeta * meta = dest -> meta();
311+
294312
if( dest -> meta() != src -> meta() &&
295313
!StructMeta::isDerivedType( src -> meta(), dest -> meta() ) )
314+
{
315+
if( StructMeta::isDerivedType( dest -> meta(), src -> meta() ) )
316+
meta = src -> meta();
317+
else
318+
{
296319
CSP_THROW( TypeError, "Attempting to update from struct type '" << src -> meta() -> name() << "' to struct type '" << dest -> meta() -> name()
297-
<< "'. update_from may only be used to update from same type or derived types" );
320+
<< "'. update_from may only be used to update from struct with a common base type" );
321+
}
322+
}
298323

299-
dest -> meta() -> updateFromImpl( src, dest );
324+
meta -> updateFromImpl( src, dest );
300325
}
301326

302327
void StructMeta::updateFromImpl( const Struct * src, Struct * dest ) const

0 commit comments

Comments
 (0)