create smart pointer for parking lot - RAII handling of parking/unparking/drop
Any parking lot needs to either be parked and unparked or dropped.
To achieve this we need a smart pointer (to reuse the reference count), but also the following functionality:
ready() - mark that the processing of the hook point was successful (only if the drop flag has not been set) the destructor should check if the ready flag has been set the constructor should set an internal dirty flag which can only be cleared by calling ready()
this is similar to the MySql/PgSql transactions.
each destructor should check if the dirty flag has been cleared. if the flag has not been cleared for the current instance, set the drop flag.
if any of the instances has not been able to clear the dirty flag resulting in the drop flag to be set, will result in dropping the packet.
ParkingLot {
...
bool drop_; // initialized to false on constructor
};
ParkingLotPtr : public boost::shared_ptr<ParkingLot> {
private:
ParkingLotPtr(const ParkingLotPtr&);
const ParkingLotPtr operator=(const ParkingLotPtr&);
bool dirty_;
ParkingLotPtr() : dirty_(true) {
}
~ParkingLotPtr() {
if (dirty_) {
get()->drop_ = true;
}
if (get()->drop_ && use_count() == 1) {
// drop packet
} else if (use_count() == 1) {
// ready to unpark
}
}
void ready() {
if (!get->drop_) {
dirty_ = false;
}
}
}
hook point:
{
...
ParkingLotPtr& parking_lot = callout_handle.getParkingLotPtr();
...
parking_lot->ready();
}
This way we can guarantee that the handling of the parking lot is always handled, and all exceptions in any hook point will drop the packet. The only thing that needs to be done by every hook point is to mark the ready flag before exiting.
This is similar to reference/dereference, but the reference count is handled by the base smart pointer.
Each hook library that creates/accesses the parking log must call getParkingLotPtr() which will generate a new ParkingLotPtr that needs to call ready before exiting scope (should be the last operation in the hook point).
We might need to protect the dirty and drop flags to be MT ready.