Structures

new topic     » topic index » view thread      » older message » newer message

Hi all,

  Are there any libs for structure handling? I found only record.e in 
the archive and it seems like a hack to me.

  Anyways, I developed a system, that I think works very well.
It's basically just one routine, and a few constants.

You define your structure using a sequence, and validate your object in 
comparison to the structure definition.

You can do stuff like this:

-- SAMPLE --
function char(object c)
  if not integer(c) then return 0 end if
  return c >= 0 and c <= 255
end function

-- THE STRUCTURE DEFINITION
constant MY_STRUCT_NULL ={
   VS_INT, -- integer
   VS_OBJ, -- object (anything can go in this element)
   {      -- specifically defined sequence
      VS_SEQ, -- undefined sequence
      VS_INT -- integer
   },
   VS_ATOM, -- atom
   {routine_id("char"),-255}  -- custom type defininition (STRING, 
MaxLen = 255)
}

type my_struct(object x)
  if not valid_structure(x,MY_STRUCT_NULL) then structure_failed() end 
if
  return 1
end type

my_struct test
test = MY_STRUCT_NULL

-- END SAMPLE --

When a structure fails, and you call structure_failed(), you will get an 
error message, something like this:

"Structure Validation Failed in element [1][2][3]"
"Atom Expected"
"Structure Definition is {0.1,0,{}}"
"Structure is            {"WRONG",5,{'R','I','G','H','T'}}"

Your structure can be as complex as you like, and there are only 3 
routines:
 valid_structure() -- compare object to structure definition
 structure_failed() -- force fatal error, with automatic error message
 get_structure_error() -- handle the error yourself

I'd like to hear your opinions, and get some test drives before I give 
it to Rob for the archives.

Thx
Chris

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu