Creating a Data Object as a Shared Object
This example demonstrates how a data object is created in an area instance version.
DATA: handle TYPE REF TO cl_demo_area, root TYPE REF TO cl_demo_root, exc TYPE REF TO cx_shm_attach_error. FIELD-SYMBOLS <fs> TYPE any. DATA(out) = cl_demo_output=>new( ). TRY. handle = cl_demo_area=>attach_for_write( ). CREATE OBJECT root AREA HANDLE handle. handle->set_root( root ). CREATE DATA root->dref AREA HANDLE handle TYPE string. ASSIGN root->dref->* TO <fs>. <fs> = `String in shared memory`. handle->detach_commit( ). CATCH cx_shm_attach_error INTO exc. out->display( exc->get_text( ) ). LEAVE PROGRAM. CATCH cx_shm_external_type. out->display( 'Type cannot be used' ). LEAVE PROGRAM. ENDTRY. TRY. handle = cl_demo_area=>attach_for_read( ). ASSIGN handle->root->dref->* TO <fs>. out->display( <fs> ). handle->detach( ). CATCH cx_shm_attach_error INTO exc. out->display( exc->get_text( ) ). LEAVE PROGRAM. ENDTRY.
AREA HANDLE is used to create an anonymous data object of type
string as a
shared object in an
area instance version of the area CL_DEMO_AREA. The generically typed attribute
dref of the
area root class
CL_DEMO_ROOT is used as a reference variable. A field symbol is used to dereference the data reference and assign a value to the anonymous data object.
Once the method DETACH_COMMIT completes the write, a read is performed to demonstrate how the objects in the shared memory are accessed. An access of this type can also be made in another program, as long as the area instance version exists in the shared memory.