Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/putil/factoryBase.h

Go to the documentation of this file.
00001 // Filename: factoryBase.h
00002 // Created by:  cary (06Oct99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef FACTORYBASE_H
00020 #define FACTORYBASE_H
00021 
00022 #include <pandabase.h>
00023 
00024 #include "typedObject.h"
00025 #include "typedReferenceCount.h"
00026 #include "factoryParams.h"
00027 
00028 #include "pvector.h"
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : FactoryBase
00032 // Description : A Factory can be used to create an instance of a
00033 //               particular subclass of some general base class.  Each
00034 //               subclass registers itself with the Factory, supplying
00035 //               a function that will construct an instance of that
00036 //               subclass; the Factory can later choose a suitable
00037 //               subclass and return a newly-constructed pointer to an
00038 //               object of that type on the user's demand.  This is
00039 //               used, for instance, to manage the set of
00040 //               GraphicsPipes available to the user.
00041 //
00042 //               FactoryBase is the main definition of the thin
00043 //               template class Factory.
00044 ////////////////////////////////////////////////////////////////////
00045 class EXPCL_PANDA FactoryBase {
00046 public:
00047   typedef TypedObject *BaseCreateFunc(const FactoryParams &params);
00048 
00049   // public interface
00050 public:
00051   FactoryBase();
00052   ~FactoryBase();
00053 
00054   TypedObject *make_instance(TypeHandle handle,
00055                              const FactoryParams &params);
00056 
00057   INLINE TypedObject *make_instance(const string &type_name,
00058                                     const FactoryParams &params);
00059 
00060   TypedObject *make_instance_more_general(TypeHandle handle,
00061                                           const FactoryParams &params);
00062 
00063   INLINE TypedObject *make_instance_more_general(const string &type_name,
00064                                                  const FactoryParams &params);
00065 
00066   TypeHandle find_registered_type(TypeHandle handle);
00067 
00068   void register_factory(TypeHandle handle, BaseCreateFunc *func);
00069 
00070   int get_num_types() const;
00071   TypeHandle get_type(int n) const;
00072 
00073   void clear_preferred();
00074   void add_preferred(TypeHandle handle);
00075   int get_num_preferred() const;
00076   TypeHandle get_preferred(int n) const;
00077 
00078   void write_types(ostream &out, int indent_level = 0) const;
00079 
00080 private:
00081   // These are private; we shouldn't be copy-constructing Factories.
00082   FactoryBase(const FactoryBase &copy);
00083   void operator = (const FactoryBase &copy);
00084 
00085   // internal utility functions
00086   TypedObject *make_instance_exact(TypeHandle handle,
00087                                    const FactoryParams &params);
00088   TypedObject *make_instance_more_specific(TypeHandle handle,
00089                                            const FactoryParams &params);
00090 
00091 private:
00092   // internal mechanics and bookkeeping
00093 
00094 #if defined(WIN32_VC) && !defined(__ICL)    //__ICL is Intel C++
00095   // Visual C++ seems to have a problem with building a map based on
00096   // BaseCreateFunc.  We'll have to typecast it on the way out.
00097   typedef pmap<TypeHandle, void *> Creators;
00098 #else
00099   typedef pmap<TypeHandle, BaseCreateFunc *> Creators;
00100 #endif
00101 
00102   Creators _creators;
00103 
00104   typedef pvector<TypeHandle> Preferred;
00105   Preferred _preferred;
00106 };
00107 
00108 #include "factoryBase.I"
00109 
00110 #endif /* FACTORY_H */

Generated on Fri May 2 00:43:37 2003 for Panda by doxygen1.3