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