[caching/hint_array] add walk method

This commit is contained in:
Joe Thornber 2013-10-09 09:45:17 +01:00
parent d02b3b6399
commit 19a1596591
2 changed files with 38 additions and 11 deletions

View File

@ -162,9 +162,22 @@ namespace {
//-------------------------------- //--------------------------------
template <typename ValueType> class value_adapter {
struct no_op_visitor { public:
virtual void visit(uint32_t index, ValueType const &v) { value_adapter(hint_visitor &v)
: v_(v) {
}
virtual void visit(uint32_t index, std::vector<unsigned char> const &v) {
v_.visit(static_cast<block_address>(index), v);
}
private:
hint_visitor &v_;
};
struct no_op_visitor : public hint_visitor {
virtual void visit(block_address cblock, std::vector<unsigned char> const &v) {
} }
}; };
@ -183,20 +196,20 @@ namespace {
}; };
template <uint32_t WIDTH> template <uint32_t WIDTH>
void check_hints(shared_ptr<array_base> base, damage_visitor &visitor) { void walk_hints(shared_ptr<array_base> base, hint_visitor &hv, damage_visitor &dv) {
typedef hint_traits<WIDTH> traits; typedef hint_traits<WIDTH> traits;
typedef array<traits> ha; typedef array<traits> ha;
shared_ptr<ha> a = downcast_array<ha>(base); shared_ptr<ha> a = downcast_array<ha>(base);
no_op_visitor<typename traits::value_type> nv; value_adapter vv(hv);
ll_damage_visitor ll(visitor); ll_damage_visitor ll(dv);
a->visit_values(nv, ll); a->visit_values(vv, ll);
} }
void check_hints_(uint32_t width, shared_ptr<array_base> base, void walk_hints_(uint32_t width, shared_ptr<array_base> base,
damage_visitor &visitor) { hint_visitor &hv, damage_visitor &dv) {
switch (width) { switch (width) {
#define xx(n) case n: check_hints<n>(base, visitor); break #define xx(n) case n: walk_hints<n>(base, hv, dv); break
all_widths all_widths
#undef xx #undef xx
} }
@ -256,10 +269,17 @@ hint_array::grow(unsigned new_nr_entries, vector<unsigned char> const &value)
grow_(width_, impl_, new_nr_entries, value); grow_(width_, impl_, new_nr_entries, value);
} }
void
hint_array::walk(hint_visitor &hv, hint_array_damage::damage_visitor &dv)
{
walk_hints_(width_, impl_, hv, dv);
}
void void
hint_array::check(hint_array_damage::damage_visitor &visitor) hint_array::check(hint_array_damage::damage_visitor &visitor)
{ {
check_hints_(width_, impl_, visitor); no_op_visitor vv;
walk(vv, visitor);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -47,6 +47,12 @@ namespace caching {
}; };
} }
class hint_visitor {
public:
virtual ~hint_visitor() {}
virtual void visit(block_address cblock, std::vector<unsigned char> const &data) = 0;
};
class hint_array { class hint_array {
public: public:
typedef boost::shared_ptr<hint_array> ptr; typedef boost::shared_ptr<hint_array> ptr;
@ -65,6 +71,7 @@ namespace caching {
void set_hint(unsigned index, vector<unsigned char> const &data); void set_hint(unsigned index, vector<unsigned char> const &data);
void grow(unsigned new_nr_entries, vector<unsigned char> const &value); void grow(unsigned new_nr_entries, vector<unsigned char> const &value);
void walk(hint_visitor &hv, hint_array_damage::damage_visitor &dv);
void check(hint_array_damage::damage_visitor &visitor); void check(hint_array_damage::damage_visitor &visitor);
private: private: