Boost logo

Boost Users :

From: Davi de Castro Reis (davicastro_at_[hidden])
Date: 2005-04-20 07:30:41


Hi all,

I am writing a class that returns some pointer to buffers in a mmaped
area. Since this buffers have constant size, I am now wrapping them in a
std::pair<const uint32, char *buf>. This works fine, but I can bet that
boost have something better, with correct semantics for operator * and,
maybe, ownership semantics.

Another problem that I have with std::pair<const uint32, char *buf> is
that I don't get the correct cv-qualified semantics with a
const std::pair<const uint32, char *buf>. I would like the const
modified version of my type to behave like a const std::pair<const
uint32, const char * const buf>. The reason for this is that I will,
then, be able to receive const char * buffers in my input. Check sample
code below:

class A
{
   typedef std::pair<const uint32, char *> value_type;
   void put(const value_type buf);
   value_type get();
   const value_type get() const;
}

Buffers in the input (put method params) will be copied inside my data
structure and their memory management is up to the user. Buffers in the
output (get method return values) are pointers to a mmap area and should
not be freed. So, if I do:

A a;
a.put(make_pair(strlen("test"), "test"));

Code doesn't compile because cv-qualification of the char * member of
buf does not get correct. The only way to get out is to write this
rather ugly class A.

class A
{
   typedef std::pair<const uint32, char *>
     value_type;
   typedef const std::pair<const uint32, const char * const>
     const_value_type;

   void put(const_value_type buf);
   value_type get();
   const_value_type get() const;
}

Is there a better solution for this? boost::array doesn't fit the job
because the size parameter is compile time. Smart pointers, besides
missing the size member, calls delete() when it is not desired.

So, a class to fit the job needs to have the size member (if there is no
such class, it is ok to stick to std::pair or boost::tuple), and
expected cv-qualification for the char *, meaning that neither the
pointer nor the pointed-by area can be modified (btw, this problem
arises often when I am working with boost::mpl, so a help here will be
greatly appreciated).

Thanks in advance,

[]s
Davi de Castro Reis


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