xorp

config_param.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-2009 XORP, Inc.
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/config_param.hh,v 1.17 2008/10/02 21:57:30 bms Exp $
00023 
00024 #ifndef __LIBXORP_CONFIG_PARAM_HH__
00025 #define __LIBXORP_CONFIG_PARAM_HH__
00026 
00027 #include "libxorp/callback.hh"
00028 
00029 
00036 template<class T>
00037 class ConfigParam {
00038 public:
00039     typedef typename XorpCallback1<void, T>::RefPtr UpdateCallback;
00040     
00050     explicit ConfigParam(const T& value)
00051     : _value(value), _initial_value(value) {}
00052     
00065     ConfigParam(const T& value, const UpdateCallback& update_cb)
00066     : _value(value), _initial_value(value), _update_cb(update_cb) {}
00067     
00071     virtual ~ConfigParam() {}
00072     
00078     const T& get() const { return (_value); }
00079     
00085     void set(const T& value) {
00086     _value = value;
00087     if (! _update_cb.is_empty())
00088         _update_cb->dispatch(_value);
00089     }
00090     
00097     ConfigParam& operator=(const T& value) {
00098     set(value);
00099     return (*this);
00100     }
00101 
00111     const T& operator++() { return (incr()); }
00112 
00122     T operator++(int) {
00123     T old_value = _value;
00124     incr();
00125     return (old_value);
00126     }
00127 
00137     const T& incr() { set(_value + 1); return (_value); }
00138 
00148     const T& operator--() { return (decr()); }
00149 
00159     T operator--(int) {
00160     T old_value = _value;
00161     decr();
00162     return (old_value);
00163     }
00164 
00174     const T& decr() { set(_value - 1); return (_value); }
00175 
00179     const T& get_initial_value() const { return (_initial_value); }
00180     
00184     void reset() { set(_initial_value); }
00185     
00186 private:
00187     T _value;           // The current value
00188     T _initial_value;       // The initial value
00189     UpdateCallback _update_cb;  // Callback invoked when _value changes
00190 };
00191 
00192 #endif // __LIBXORP_CONFIG_PARAM_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations