Boost Users :
From: Erik Thiele (erik_at_[hidden])
Date: 2004-06-23 10:18:06
i have an int. but it should also support a "undefined" state like SQL
NULL. most times i can use -1 for this. but there are also data types
without a reasonable undefined value. maybe float or double. or maybe -1
is perfectly legal.
so i used my own smartptr class (which i will replace by
boost::shared_ptr since it is better and compatible to libs from
strangers) to fix the problem as follows:
shared_ptr<int> a(new int(10));
shared_ptr<int> b; // has the value "undefined" like SQL NULL.
now i can do checks like
if ( ! b ) printf("%d", *b);
now my problem was that several places in my program access the same int
instance. they can assign values, and then other parts of the program
can read these values. that's good behaviour.but i cannot set the object
to "undefined". see why:
void foo (shared_ptr<int> a)
*a = 10; // <-- ok! this works! everybody now has the 10!
void errorfoo (shared_ptr<int> a)
a.reset(); // <-- this does of course not work, since only my local
// copy of a is now SQL NULL.
so i restructured my shared_ptr to make it able to hold this special SQL
NULL state also. this way i got really awkward semantics though. i don't
go in details here, but in my shared_ptr the errorfoo function works as
expected. (the function is not called "reset" then).
but i got severe problems in typecast semantics of my modified
shared_ptr, so i decided to dump this beast before anyone else has to
see it :-)))
i thought that maybe it's not a good idea to unite smartpointer with SQL
NULL facility. maybe it is better to write:
shared_ptr<nullabletype<int> > x(new nullabletype<int>(55));
if (x->isnull()) fooooo();
printf("the int is %d\n", x->obj()); // throw exception if x is null
in this example the shared_ptr is just sugar. it is not needed.
the nullable subsystem is now independent from shared_ptr.
is there such a nullable type extender beast in boost somewhere?
or do i have to invent it?
by the way, the following is not a good solution i think:
now programmers can do errors like access "thevalue" even though
"thevalue_is_defined" is false. i want some mechanism to prevent
programmers from doing this kind of errors. if and only if the value is
defined it should be possible to read it.
also this nullable type extender is handy for a database layer to
interact with SQL.
-- Erik Thiele
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net