Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Light weight container around const char* & length without copying the data

I have a underlying API that passes a const char* and a length:

foo(const char* data, const uint32_t len);

I'd like to wrap this data/length in a light weight container that can be iterated and has the ability to be randomly accessed but not make a copy (e.g. like a vector). What is the best way to achieve this? The const char* data is not necessarily a 'string'; it may contain NULL's throughout.

I am using STL and Boost. I've seen boost::as_array<> and as_literal<> -- is one of these appropriate here?

like image 994
NuSkooler Avatar asked Oct 20 '25 17:10

NuSkooler


2 Answers

It would be easy to make such a class. Something like this:

template <class T>
class array_ref {
public:
    // makes it work with iterator traits..
    typedef T         value_type;
    typedef size_t    size_type;
    typedef ptrdiff_t difference_type;

    typedef T*        pointer;
    typedef T*        iterator;
    typedef T&        reference;

    typedef const T*  const_pointer;
    typedef const T*  const_iterator;
    typedef const T&  const_reference;

public:
    array_ref(T *p, size_t n) : data_(p), len_(n) {}

    // iteration
    iterator begin()             { return data_; }
    iterator end()               { return data_ + len_; }
    const_iterator begin() const { return data_; }
    const_iterator end() const   { return data_ + len_; }

    // access
    reference operator[](size_t n)             { return data_[n]; }
    reference at(size_t n)                     { return data_[n]; }
    const_reference operator[](size_t n) const { return data_[n]; }
    const_reference at(size_t n) const         { return data_[n]; }

    // capacity
    size_t size() const { return len_; }
    bool empty() const  { return size() == 0; }

    // raw access
    T* data() const { return data_; }

    // etc...
private:
    T* data_;
    size_t len_;
};

This looks like a bunch of code, most of it isn't strictly necessary. However, since it is a template the compiler will only generate code for the methods used. And the actual class itself only uses space for the pointer and the length members.

But in the end, this really isn't much of a gain. Since pointers themselves are nice pointers, I'd probably just use raw pointers here.

like image 161
Evan Teran Avatar answered Oct 22 '25 07:10

Evan Teran


I have used iterator_facade http://www.boost.org/doc/libs/1_43_0/libs/iterator/doc/iterator_facade.html together with iterator_range

you can easily use iterator_range to construct your wrapper, http://www.boost.org/doc/libs/1_42_0/libs/range/doc/utility_class.html#iter_range

boost::iterator_range<char*> range(begin, begin + N);

special cases can be handled with iterator_facade

like image 24
Anycorn Avatar answered Oct 22 '25 06:10

Anycorn