00001 /* 00002 The lestes compiler suite 00003 Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy 00004 Copyright (C) 2002, 2003, 2004, 2005 Petr Zika 00005 Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala 00006 Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina 00007 Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda 00008 Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar 00009 Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; version 2 of the License. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 See the full text of the GNU General Public License version 2, and 00021 the limitations in the file doc/LICENSE. 00022 00023 By accepting the license the licensee waives any and all claims 00024 against the copyright holder(s) related in whole or in part to the 00025 work, its use, and/or the inability to use it. 00026 00027 */ 00028 #ifndef lestes__lang__cplus__lex___encoder_factory_hh___included 00029 #define lestes__lang__cplus__lex___encoder_factory_hh___included 00030 00031 /*! \file 00032 \brief Factory for encoders. 00033 00034 Declaration of encoder_factory class managing encoder implementations. 00035 \author pt 00036 */ 00037 #include <lestes/common.hh> 00038 #include <lestes/lang/cplus/lex/encoder.hh> 00039 #include <lestes/std/map.hh> 00040 #include <lestes/std/objectize_macros.hh> 00041 00042 package(lestes); 00043 package(lang); 00044 package(cplus); 00045 package(lex); 00046 00047 /*! 00048 \brief Factory for encoders. 00049 00050 Manages all encoder implementations. 00051 */ 00052 class encoder_factory: public ::lestes::std::object { 00053 public: 00054 //! Adapts the return value of create methods. 00055 template <typename Encoder> 00056 static ptr<encoder> encoder_create_adaptor(void); 00057 //! Type of common encoder create methods. 00058 typedef ptr<encoder> (* encoder_create_type)(void); 00059 //! Inserts new encoder into the database. 00060 void insert(const lstring &a_name, encoder_create_type a_create); 00061 //! Removes the encoder from the database. 00062 void remove(const lstring &a_name); 00063 //! Returns encoder for name. 00064 ptr<encoder> summon(const lstring &a_name) const; 00065 //! Returns the only instance. 00066 static ptr<encoder_factory> instance(void); 00067 protected: 00068 //! Creates the object 00069 encoder_factory(void); 00070 //! Marks the object. 00071 virtual void gc_mark(void); 00072 private: 00073 //! Type of storage for create functions. 00074 typedef ::lestes::std::map< lstring, encoder_create_type > storage_type; 00075 /* TODO pt change to hash map 00076 typedef hash_map< ::std::string, encoder_creator, string_hash_function > storage_type; 00077 */ 00078 //! The storage for encoders. 00079 srp<storage_type> storage; 00080 //! The only object of the class. 00081 static ptr<encoder_factory> singleton; 00082 //! Hides copy constructor. 00083 encoder_factory(const encoder_factory &); 00084 //! Hides assignment operator. 00085 encoder_factory &operator=(const encoder_factory &); 00086 }; 00087 00088 /*! 00089 Adapts return value of concrete encoder create() method to return base class. 00090 \param Encoder The concrete encoder to adapt. 00091 \param create The creator of the concrete encoder. 00092 */ 00093 template <typename T> 00094 ptr<encoder> encoder_factory::encoder_create_adaptor(void) 00095 { 00096 return ptr<encoder>(T::create()); 00097 } 00098 00099 end_package(lex); 00100 end_package(cplus); 00101 end_package(lang); 00102 00103 package(std); 00104 /*! 00105 Makes the encoder_create_type nondumpable. 00106 */ 00107 specialize_objectize_nodump(::lestes::lang::cplus::lex::encoder_factory::encoder_create_type); 00108 00109 end_package(std); 00110 end_package(lestes); 00111 00112 #endif 00113 /* vim: set ft=lestes : */
1.5.1-20070107