On Thursday, August 23, 2012 10:29:00 AM UTC-7, ?? Tiib wrote:
Maybe try to pack up your words with code example?
struct lock {
virtual ~lock() {}
virtual void unlock() = 0;
virtual bool engaged() const = 0;
};
struct null_lock : lock {
void unlock() {}
bool engaged() const { return false; }
};
struct single_lock : lock {
void unlock() { state = UNLOCKED; }
bool engaged() const { return state == LOCKED; }
};
// can be added later...
struct multi_lock : lock {
// probably have this syntax wrong...
void unlock() { for ( lock & current_lock : locks) current_lock.unlock(); }
bool engaged() const {
return find_if(locks.begin(), locks.end(), bind(&lock::engaged, _1)) != locks.end();
}
iterator begin();
iterator end();
private:
?? locks;
};
struct door {
virtual ~door() {}
bool open() {
if (lock()->engaged()) return false;
state = OPEN;
return true;
}
lock* lock();
private:
lock my_lock;
door_state state;
};
Use code:
void go_through_door(door & entry) {
entry.lock()->unlock();
entry.open();
// go through...
}
Looks pretty darn simple to me. Compared to:
// may be a few lines of code smaller...but not much.
struct door { ... };
struct lockable { .... };
struct lockable_door : door, lockable { ... };
// but you pay for it here...
void go_through_door(door & entry) {
if (auto lock = dynamic_cast<lockable*>(&entry))
lock->unlock();
entry.open();
};