C++ and Java CNI: Check Java references

Mark Mitchell mark@codesourcery.com
Tue Apr 24 11:16:00 GMT 2001


>>>>> "Per" == Per Bothner <per@bothner.com> writes:

    Per> from the source.  However, explicit null-pointer checking
    Per> seems to stretch the boundary of appropriate magic re-writes.

FWIW, I agree with Per.  I would prefer not to see this patch
installed.

Instead, I would suggest that CNI programmers use C++ features, i.e.,
smart pointers, to do the job.

For example:

  template <typename T>
  class JavaPointer {

    T& operator* () {  
      if (!p) throw NullException;
      return *p;
    }

  private:

    T* p;
  };

(Obviously, this needs fleshing out.)

It's possible to write conversion operators (see auto_ptr) so that
pretty much all of the C++ pointer semantics come through this way.

Then, the maintenance requirement boils down to simply remembering to
use JavaPointer everywhere.  That's a lot easier than remembering to
add all the checks by hand.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com



More information about the Java mailing list