Boost logo

Boost :

From: Christoph Ludwig (cludwig_at_[hidden])
Date: 2004-05-26 04:03:11


On Tue, May 25, 2004 at 11:24:38PM -0400, christopher diggins wrote:
> From: "Rob Stewart" <stewart_at_[hidden]>
> > What if the type is not a built-in? That is, what if copying is
> > expensive?
>
> I did not anticipate this secnario. I imagined the type only used with small
> value types. This is somewhat implied by the name constrained_value. This is
> perhaps an oversight on my part. Would you be able to give a concrete
> example of a type with more expensive copy semantics which would be an
> appropriate contender? I am not being argumentitive, but simply cautious.

If you write data onto smartcards, the card OS sometimes imposes very
specific restrictions. I once wrote an application that personalized
smartcards with an RSA key-pair. The OS could use only exactly 1024 bit
RSA moduli - an 1023 bit modulus was rejected.

I stated this restriction in the documentation of my wrapper around
the low level card terminal interface and threw an exception if the
precondition was not met. (An assert was not acceptable in this
particular case.) But it would have been nice if already the interface
had made it obvious that only bigints with 1024 bit constitute valid
input. Something along the lines of

  constrained_value<bigint_length_policy<1024, 1024> >

where the constraints_policy is

  // please ignore any syntax errors
  template<unsigned min_length, unsigned max_length>
  class bigint_length_policy {
  public:
    typedef my_bigint_type value;

    void assign(value& lhs, value const& rhs) {
      if(min_length <= rhs.bitlength() &&
         rhs.bitlength() <= max_length) {
         lhs = rhs;
      }
      else {
        throw appropriate_exception();
      }
    }
  };

Of course, the efficiency of the call by value would have been no
issue in my application because the time needed for transferring the
data onto the smartcard probably dominates the unnecessary copy of a
1024 bit bigint on any PC or workstation not taken from the
museum. But one can imagine similar situations where the overhead may
be significant. What, e.g., if constrained_value was also used in the
computationally expensive key generation?

> > Use TypeTraits to determine whether to pass by value
> > or by reference.
>
> How specifically would be the best way to accomplish that? I am not
> sufficiently familiar with the technique of using TypeTraits in this manner.

How about call_traits<T>::param_type from boost/call_traits.hpp? I had
no need for this trait myself yet, whence I don't know about potential
drawbacks, though.

Regards

Christoph

-- 
http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/cludwig.html
LiDIA: http://www.informatik.tu-darmstadt.de/TI/LiDIA/Welcome.html

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk