00001 // 00002 // Copyright (c) Benjamin Kaufmann 2010 00003 // 00004 // This is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This file is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this file; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 // 00018 // 00019 // NOTE: ProgramOptions is inspired by Boost.Program_options 00020 // see: www.boost.org/libs/program_options 00021 // 00022 #ifndef PROGRAM_OPTIONS_NOTIFIER_H_INCLUDED 00023 #define PROGRAM_OPTIONS_NOTIFIER_H_INCLUDED 00024 00025 namespace ProgramOptions { namespace detail { 00026 00028 // Notifier 00030 // HACK: 00031 // This should actually be replaced with a proper and typesafe delegate class. 00032 // At least, it should be parametrized on the actual parameter type since 00033 // the invocation of f via the generic func is not strictly conforming. 00034 // Yet, it should work on all major compilers - we do not mess with 00035 // the address and the alignment of void* should be compatible with that of T*. 00036 template <class ParamT> 00037 struct Notifier { 00038 typedef bool (*notify_func_type)(void*, const std::string& name, ParamT); 00039 Notifier() : obj(0), func(0) {} 00040 template <class O> 00041 Notifier(O* o, bool (*f)(O*, const std::string&, ParamT)) { 00042 obj = o; 00043 func= reinterpret_cast<notify_func_type>(f); 00044 } 00045 void* obj; 00046 notify_func_type func; 00047 bool notify(const std::string& name, ParamT val) const { 00048 return func(obj, name, val); 00049 } 00050 bool empty() const { return func == 0; } 00051 }; 00052 00053 template <class ParamT, class ObjT> 00054 struct Notify { 00055 typedef bool (*type)(ObjT*, const std::string& name, ParamT); 00056 }; 00057 00058 } } 00059 #endif