xorp

safe_callback_obj.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/safe_callback_obj.hh,v 1.11 2008/10/02 21:57:33 bms Exp $
00023 
00024 #ifndef __LIBXORP_SAFE_CALLBACK_OBJ_HH__
00025 #define __LIBXORP_SAFE_CALLBACK_OBJ_HH__
00026 
00027 
00028 
00029 #include "xorp.h"
00030 
00031 class SafeCallbackBase;
00032 
00047 class CallbackSafeObject :
00048     public NONCOPYABLE
00049 {
00050 public:
00051     CallbackSafeObject() {}
00052     virtual ~CallbackSafeObject();
00053 
00054     void ref_cb(SafeCallbackBase* scb);
00055     void unref_cb(SafeCallbackBase* scb);
00056 
00057 protected:
00058     vector<SafeCallbackBase*> _cbs;
00059 };
00060 
00067 class SafeCallbackBase :
00068     public NONCOPYABLE
00069 {
00070 public:
00076     SafeCallbackBase(CallbackSafeObject* o);
00077 
00084     ~SafeCallbackBase();
00085 
00086     void invalidate();
00087 
00088     bool valid() const;
00089 
00090 protected:
00091     SafeCallbackBase();           // Not directly constructible
00092 
00093 protected:
00094     CallbackSafeObject* _cso;
00095 };
00096 
00097 
00098 // ----------------------------------------------------------------------------
00099 // Inline CallbackSafeObject methods
00100 
00101 inline void
00102 CallbackSafeObject::ref_cb(SafeCallbackBase* scb)
00103 {
00104     _cbs.push_back(scb);
00105 }
00106 
00107 inline void
00108 CallbackSafeObject::unref_cb(SafeCallbackBase* scb)
00109 {
00110     vector<SafeCallbackBase*>::iterator i =
00111     find(_cbs.begin(), _cbs.end(), scb);
00112     if (i != _cbs.end())
00113     _cbs.erase(i);
00114 }
00115 
00116 #endif // __LIBXORP_SAFE_CALLBACK_OBJ_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations