Source: ../../libxorp/safe_callback_obj.hh

 Annotated List  Files  Globals  Hierarchy  Index  Top
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-

// Copyright (c) 2001-2009 XORP, Inc.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License, Version
// 2.1, June 1999 as published by the Free Software Foundation.
// Redistribution and/or modification of this program under the terms of
// any other version of the GNU Lesser General Public License is not
// permitted.
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// see the GNU Lesser General Public License, Version 2.1, a copy of
// which can be found in the XORP LICENSE.lgpl file.
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;

// $XORP: xorp/libxorp/safe_callback_obj.hh,v 1.12 2009/01/05 18:30:58 jtc Exp $


#include <algorithm>
#include <vector>

struct SafeCallbackBase;

 * @short Base class for objects that are callback safe.
 * Objects that wish to be callback safe should be derived from this
 * class.  The class works in conjunction with SafeCallbackBase and
 * between them implement callback and callback-object tracking.  When
 * a CallbackSafeObject is destructed it informs all the callbacks that
 * refer to it that this is the case and invalidates (sets to null)
 * the object they point to.
class CallbackSafeObject {
    CallbackSafeObject() {}
    virtual ~CallbackSafeObject();

    void ref_cb(SafeCallbackBase* scb);
    void unref_cb(SafeCallbackBase* scb);

    // Copy operations are not supported.  It's hard to know what the
    // correct thing to do on assignment or copy, so best bet is not
    // to do anything.
    CallbackSafeObject(const CallbackSafeObject&);		// Not implemented
    CallbackSafeObject& operator=(const CallbackSafeObject&);	// Not implemented

    std::vector<SafeCallbackBase*> _cbs;

 * @short Base class for safe callbacks.
 * These are object callbacks that are only dispatched if target of
 * callback is non-null.
class SafeCallbackBase {
     * Constructor.
     * Informs CallbackSafeObject that this callback operates on it.
    SafeCallbackBase(CallbackSafeObject* o);

     * Destructor.
     * Informs CallbackSafeObject that is tracking callback
     * instances that this callback no longer exists.

    void invalidate();

    bool valid() const;

    SafeCallbackBase();					  // Not implemented
    SafeCallbackBase(const SafeCallbackBase&);		  // Not implemented
    SafeCallbackBase& operator=(const SafeCallbackBase&); // Not implemented

    CallbackSafeObject* _cso;

// ----------------------------------------------------------------------------
// Inline CallbackSafeObject methods

inline void
CallbackSafeObject::ref_cb(SafeCallbackBase* scb)

inline void
CallbackSafeObject::unref_cb(SafeCallbackBase* scb)
    std::vector<SafeCallbackBase*>::iterator i =
	find(_cbs.begin(), _cbs.end(), scb);
    if (i != _cbs.end())


Generated by: pavlin on on Wed Jan 7 19:10:36 2009, using kdoc 2.0a54+XORP.