![]() |
Examples 8.11.2.1: |
The features of the required library class BOOL 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.
OCTET), a binary string and from a text string. The latter two have 'build' variants taking data from a cursor buffer.
The principal conversion to a truth value is the one taking an octet as argument for conversion, thus
If the least significant bit of the octet is set then the result will be true, otherwise false.
This second conversion operation takes a single element binary string as argument, thus
If the implementation-dependent bits of the octet are set then the result will be true, otherwise false. This routine is provided generally for commonality of usage with those routines which need more than one octet for binary representation. In practice the above could have been written as
which would, of course, use the variant with a single octet argument.
This last variant of create takes as argument a text string - such as "T", "tr" or even the full word "True". This will typically have been obtained from some user or text file input to the program. Note that the three suggestions given would only be sensible if the cultural definition of these was for English speakers using 'True' and 'False' as representation. The call
with any of the above suggestions would return the value true. Note, in particular that the matching is not case sensitive.
The required library provides cursor objects for scanning string buffers and the convention has been adopted in the required library that routines which produce values by conversion from some buffer are called 'build'. This first variant takes a binary string cursor as argument, so that it is possible to write
Since a truth value is storable in a single octet, it would be possible to write the equivalent using the first create variant as -
See the BIN_CURSOR class for details of the binary cursor buffer machanism.
Corresponding to the binary build operation there is a text one, taking a STR_CURSOR argument.
Note that in this very special case if the text in the buffer at the current position is not a valid truth value representation then false will be returned and the current position will not have changed. This special case arises because it is more confusing to return an 'OK' at the same time as a Boolean value. See, however, the is_bool predicate below.
The final 'creation' operation is provided for commonality with other classes reading from binary strings. The argument is again a binary cursor, but the operation is different to building. If the first octet in the remainder of the buffer is the truth value true then the second octet is used to give the desired truth value. It is thus a mechanism catering for optional components of some file, for example.
On return from this routine there are two possibilities -
This routine is provided so that a prior check on whether some string contains a text representation of a truth value can be tested in advance. Typical code might look like the following routine -
There are sixteen logical operations defined in this class, one unary and the remainder binary having the truth tables which follow. Note that some of the tables are identical to others. The reason for this is that the way in which using code may wish to handle some problem may view the operation in one way, not in some other.
Feature | self | |
---|---|---|
T | F | |
not | F | T |
Binary operation truth tables are given below rather than giving an individual example code for each. Examples of the use of binary logical operations permeate example text for most other classes!
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
The small group of conversion operations which follows are responsible for conversion from a truth value to either a numeric 'code', a bit-pattern or a text string.
This routine converts the Boolean value to a cardinal number, one corresponding to true and zero corresponding to false, thus -
and
are equivalent - although the former is much more efficient!
Where it is necessary to export a truth value then it needs conversion into a bit-pattern. This name of this operation is the one used throughout the required library for doing such a conversion. Thus the routine in the next example could have been written as
NOTE | While the above source text could provide the implementation of the next example routine, the example is not intended to indicate that it is necessarily the implementation used by a provider of this required library! |
As suggested by the above example, a simple call of this routine with a given BIN_FILE argument will append a binary representation of self to that file, thus -
For a program which operates in several cultures at once (eg a conversion program from one to another) it is often necessary to provide a textual representation of a value suitably composed/encoded for such a culture - not being the local culture in the execution environment. This first form of the 'str' routine is provided with an argument which provides the conversion routine with all of the culture-dependent data it requires, thus -
would provide the necessary string representation to append to, say, the converted output.
This routine provides an output text representation of self in the current local culture, thus -
is identical in meaning to the use of the default culture lib with the first version of str, thus -
In addition to the conversion of a value to some internationalised form, simple formatting layout to incorporate the resulting text string into some message is a most important function of the required library. Full details of this are given in the Representation section.
In order to permit formatting to operate it is necessary for each class inheriting from $FMT to provide the two routines fmt with a library argument and fmt without a library argument. Under normal circumstances these routines will not be invoked directly by a library user, rather by the library formatting engine.
![]() |
Specification Index | ![]() |
Language Index | ![]() |
Section 8 Index |
Comments
or enquiries should be made to Keith Hopper. Page last modified: Wednesday, 24 May 2000. |
![]() |