containers - Record containing access to instantiation of generic package based on that record -


this annoying problem. have record type wraps various base types , need able store vector (from ada.containers.vectors) in itself! guess that's not possible can give me advice on how solve problem in way perhaps? give better idea of i'm to, here not work:

with base_types; use base_types; ada.strings.wide_unbounded; use ada.strings.wide_unbounded; ada.containers.vectors; green_tasks; use green_tasks; ada.unchecked_deallocation;  package boxed_types     type string_ptr access unbounded_wide_string;    procedure free_unbounded_wide_string new ada.unchecked_deallocation      (object => unbounded_wide_string, name => string_ptr);    type vector_ptr; -- won't work     type type_t (t_null, t_uint64, t_text, t_bool, t_gtask, t_vector);    type item (itype : type_t := t_null) record       case itype          when t_null   => null;          when t_uint64 => uint64      : uinteger_64;          when t_text   => string      : string_ptr;          when t_bool   => bool        : boolean;          when t_gtask  => green_task  : green_task_ptr;          when t_vector => item_vector : vector_ptr;     -- error here        end case;    end record;     package item_vectors new ada.containers.vectors      (index_type   => natural,       element_type => item);    use item_vectors;    type vector_ptr access vector; end boxed_types; 

this gives me not unexpected error "invalid use of type before it's full declaration" vector_ptr. however, cannot instantiate vector package before have declared item either, , need vector , base types wrapped 1 record type. (it's interpreter i'm writing in spare time; vm must store various different types on stack, in heterogenous arrays, manipulate them, etc..)

will have break type safety entirely , mess around address access conversions or there cleaner solution?

here's different version stores item (not access) in vector. works using inheritance, creating vector of base type. implies indefinite_vector because size of each individual component not known in advance.

again, compiled untested.

with ada.containers.indefinite_vectors;  package boxed_base     type base_item tagged record       null;    end record;     package item_vectors new ada.containers.indefinite_vectors      (index_type   => natural,       element_type => base_item'class);    use item_vectors;     type vector_ptr access vector;  end boxed_base; 

this base type has property can stored in vector, , have storage management handled indefinite_vectors. can inherit it, characteristics need.

with ada.strings.wide_unbounded; use ada.strings.wide_unbounded; ada.unchecked_deallocation; boxed_base;  package boxed_types     type uinteger_64 new integer;    type green_task_ptr access uinteger_64;    -- these 2 because original testcase incomplete     type string_ptr access unbounded_wide_string;    type type_t (t_null, t_uint64, t_text, t_bool, t_gtask, t_vector);     type item (itype : type_t ) new boxed_base.base_item record       case itype          when t_null   => null;          when t_uint64 => uint64      : uinteger_64;          when t_text   => string      : string_ptr;          when t_bool   => bool        : boolean;          when t_gtask  => green_task  : green_task_ptr;          when t_vector => item_vector : boxed_base.vector_ptr;           end case;    end record;  end boxed_types; 

one feature of original design has gone : default discriminant not allowed tagged types : means create instance definite discriminant (and definite size!) , cannot modify later (just replace object new one).

another feature may worth mentioning : indefinite_vectors may have performance penalty on definite cousins : if so, necessary cost incurred heterogeneous object sizes , pop in form slice problem.

it possible eliminate discriminant type_t creating different subclasses each type of item; maybe cleaner design @ stage more refactoring want!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -