![]() |
Examples 8.11.2.1: |
![]() |
The features of the required library class UNIT_KINDS are used in individual examples. None of the examples given is a complete class nor necessarily a 'complete' method.
In all of the examples it will be assumed that the code shown is in a class called, say, EXAMPLE.
This first variant of enumeration creation takes an argument which is a cardinal number and returns a valid value. Note carefully the pre-condition that the value must lie between offset and (offset + cardinality - 1) - pre-condition failure will occur with any other value, thus -
res : UNIT_KINDS := UNIT_KINDS::create(1)
yields the value UNIT_KINDS::ISO_1000, since
res.card
has the value 1! Remember that res.enum may not have that value!!!!
It is important to remember that the creation operation will succeed with any prefix of the values which is unique - thus for English only single letters 'I', 'U' and 'O' will be quite sufficient, thus with a string "ISO"
res : UNIT_KINDS := UNIT_KINDS::create(str)
the ISO 1000 value will be created.
res : UNIT_KINDS ; if UNIT_KINDS::is_enum(str_val) = CONVERSION_RESULTS::All_Right then res := UNIT_KINDS::create(str_val) else res := UNIT_KINDS::ISO_1000 end
in which the 'error handling' is merely suggested and must, in practice, depend on the execution context. Note that the local variable res had to be declared outside of the condition if it is to be used in following code.
res : UNIT_KINDS := UNIT_KINDS::build(str_cursor)
gives a value to res. Note from the specification of the build operation that void could be returned! This is only done if the string at the starting cursor position did not represent a value of the enumeration type.
res : UNIT_KINDS := UNIT_KINDS::build(str_cursor,LIBCHARS::default) ; res : UNIT_KINDS := UNIT_KINDS::build(str_cursor,other_lib)
res : UNIT_KINDS := UNIT_KINDS::build(bin_cursor)
res : UNIT_KINDS := UNIT_KINDS::create(binary_string)
could produce a valid value - or void - which in this case, fortunately is never valid. This was the design reason behind making an all clear bit-pattern an invalid enumeration type, so that the many uses of enumerations when creating binary files would never have a 'zero' on the file!
Disambiguation of the result is possible by testing the state of the cursor. If it is the same as at entry then the next octet wasn't a representation of a truth value, if it has been moved by the size of a truth value representation only, then if void is returned then the truth value was false. If the enumeration value in the buffer was not a valid one then void is also returned and the cursor has been moved by the number of octets forming a valid value. The following code fragment illustrates the various disambiguation tests which could be written.
posn : CARD := buffer_cursor.index ; loc_res : UNIT_KINDS := UNIT_KINDS::read(buffer_cursor) ; if void(loc_res) then loc_posn : CARD := buffer_cursor.index ; if loc_posn = posn then -- There wasn't even a valid truth value! elsif (loc_posn - posn) = true.binstr.size then -- The truth value false else -- The value was a bad one! end else -- do something?? end
answer : BOOL := res = loc_res ;
answer := res.is_eq(loc_res)
answer := UNIT_KINDS::offset = 1
so that answer will be true!
answer := UNIT_KINDS::ISO_1000 = 1
again results in answer being true!
if UNIT_KINDS::cardinality = (UNIT_KINDS::Other_Units - loc_res.offset + 1) then ...
is the same things as stating -
if true then ...
which is a bit of useless coding!
answer : BOOL := res.card = res.enum
is quite as likely to yield false as true.
The two following fragments are intended to illustrate the reasoning. They both have identical functionality. Assuming the use of the standard input channel to get a value (ignoring for the purposes of illustration any error handling), compare -
card_map : FMAP{CARD,TARGET} := FMAP{CARD,TARGET}::create ; val : UNIT_KINDS := KINDS::create(IN::get_str) ; card_map := card_map.insert(val.hash,target_50m) ; ...
with the non-hashed version -
card_map : FMAP{UNIT_KINDS,TARGET} := FMAP{UNIT_KINDS,TARGET}::create ; val : UNIT_KINDS := UNIT_KINDS::create(IN::get_str) ; card_map := card_map.insert(val,target_50m) ; ...
The hashed version will probably be slightly faster than the non-hashed one, but they are functionally equivalent.
val := UNIT_KINDS::create(bin_string) ; answer : BOOL := bin_string = val.binstr
leaves answer identically true. The purpose of this is to simplify storing data on a storage device for export and/or re-import on some future program execution.
write( bfile : BIN_FILE ) is bfile := bfile + binstr end
This is the inverse operation of creation from a string. However, the value of answer may not be true!
str_val : STR := IN::get_str ; val : UNIT_KINDS := UNIT_KINDS::create(str_val) ; answer : BOOL := val.str = str_val
The reason for the value which answer has is given earlier in these examples!
This is the inverse operation of creation from a string, this time using the given culture conversion data. However, the value of answer may still not be true!
str_val : STR := IN::get_str ; val : UNIT_KINDS := UNIT_KINDS::create(str_val, other_lib) ; answer : BOOL := val.str = str_val
The reason for this more difficult equality problem is hinted at earlier in these examples.
Formatting of an enumeration is carried out using the anchored formatting mechanism. Do remember, however, that while this routine is visible for direct use, it will generally be much easier to use the formatter (see the FMT class) to create the individual formats as desired. This first version uses the default repertoire and encoding, thus the following could be used to format a right-aligned string -
fmt_descr : ANCHORED_DESCR := ANCHORED_DESCR::create(LIBCHARS::default.Space,12,0) ; res : STR := val.fmt(fmt_descr)
which will result in res containing
" ISO-1000"in an English environment if the value happened to be UNIT_KINDS::ISO_1000!
This formatting variant is used where the cultural formatting is to be done in accordance with the given data, thus
fmt_descr : ANCHORED_DESCR := ANCHORED_DESCR::create(other_lib.Space,12,0) ; res : STR := val.fmt(fmt_descr,other_lib)
which could result in res containing
" andere Norm"in a German environment if the value happened to be UNIT_KINDS::Other_Units and the encoding was the same!
![]() |
Specification Index | ![]() |
Language Index | ![]() |
Section 8 Index |
Comments
or enquiries should be made to Keith Hopper. Page last modified: Friday, 23 March 2001. |
![]() |