|
Boost Users : |
From: nicola (vitacolo_at_[hidden])
Date: 2008-01-11 15:46:36
Hi,
I have a class modeling a record-based read-only binary file and I have
written a random access iterator based on iterator_facade to iterate
over the records. The class looks like this:
class my_binary_file {
public:
typedef std::string key_type;
typedef unsigned int value_type;
typedef std::pair<key_type, value_type> record_type;
private:
class my_binary_file_iterator : public boost::iterator_facade<
my_binary_file_iterator,
my_binary_file::record_type const,
boost::random_access_traversal_tag,
my_binary_file::record_type&,
int>
{
public:
my_binary_file_iterator() : rec_num_(0), ff_ptr_(0) { }
explicit my_binary_file_iterator(my_binary_file* ff, unsigned int
n) : ff_ptr_(ff), rec_num_(n) { }
private:
friend class boost::iterator_core_access;
unsigned int rec_num_;
my_binary_file* ff_ptr_;
mutable my_binary_file::record_type record;
void increment() { ++rec_num_; }
void decrement() { --rec_num_; }
void advance(int n) { rec_num_ += n; }
int distance_to(const my_binary_file_iterator z) const {
return z.rec_num_ - this->rec_num_;
}
bool equal(my_binary_file_iterator const& other) const {
return this->ff_ptr_ == other.ff_ptr_ && this->rec_num_ ==
other.rec_num_;
}
const my_binary_file::record_type
dereference() const {
ff_ptr_->seekg(rec_num_);
my_binary_file::key_type key;
my_binary_file::value_type val;
ff_ptr_->get_record(key, val);
record = std::make_pair(key, val);
return record;
}
};
public:
typedef my_binary_file_iterator iterator;
iterator begin() const { return iterator(this, 0); }
iterator end() const { return iterator(this, num_records_); }
// Opens a binary file for reading
explicit my_binary_file(const std::string& path);
~my_binary_file();
operator void*() const;
bool operator!() const;
// Reads the record at the current position
bool get_record(key_type& k, value_type& v);
std::ios::pos_type tellg() const { return fs_.tellg(); }
void seekg(unsigned int n) {
fs_.seekg(start_record_ + std::ios::off_type(n * record_size_),
std::ios::beg);
}
private:
boost::filesystem::path pathname_;
mutable boost::filesystem::fstream fs_;
std::streampos start_record_;
key_type::size_type key_size_;
unsigned int record_size_, num_records_;
};
Unfortunately, when I try to compile my project, I get the following
error:
error: invalid conversion from 'const frequency_file* const' to
'frequency_file*'
I have tried several variants of my code, but without success.
I think I'm missing something pretty obvious in my definition of
my_binary_file_iterator, but I can't get out of it. Anyone can help?
Regards
Nicola
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