Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

PhxNamedInterface.h

Go to the documentation of this file.
00001 #ifndef PHX_NAMED_INTERFACE_H
00002 #define PHX_NAMED_INTERFACE_H
00003 
00004 #include <Phx/PhxConfig.h>
00005 #include <Phx/PhxTypes.h>
00006 
00007 #include <Phx/Core/PhxDescription.h>
00008 #include <Phx/Util/PhxLocks.h>
00009 
00017 namespace Phx {
00018 
00030   class NamedInterface : public LockedPtrInterface<NamedInterface> {
00031     class TypeClass;
00032     class IdentifierClass;
00033 
00034   public:
00035     class Manager;
00036 
00044     typedef ValueType<TypeClass, uint32_t>  Type;
00045 
00054     typedef ValueType<IdentifierClass, uint32_t>  Identifier;
00055 
00059     static const Type INVALID_TYPE;
00060 
00064     static const Identifier INVALID_IDENTIFIER;
00065 
00086     const Type& type(void) const { return mType; }
00087 
00097     const Identifier& identifier(void) const { return mIdentifier; }
00098 
00107     const String& name(void) const { return mName; }
00108 
00109   protected:
00110     // Default constructor - protected to prevent direct
00111     // instantiation.
00112     NamedInterface(void);
00113 
00114     // Virtual destructor -- necessary for subclasses.
00115     virtual ~NamedInterface() {}
00116 
00117   private:
00118     friend class Manager;
00119 
00120     // Allows manager to set values after creation
00121     void setType(Type type) { mType = type; }
00122     void setIdentifier(Identifier identifier) { mIdentifier = identifier; }
00123     void setName(const String& name) { mName = name; }
00124 
00125     Type       mType;
00126     Identifier mIdentifier;
00127     String     mName;
00128   };
00129 
00140   class NamedInterface::Manager : public LockedPtrInterface<NamedInterface::Manager> {
00141   public:
00142     // Type constant and name
00143     const static NamedInterface::Type INTERFACE_TYPE;
00144     const static String               INTERFACE_TYPE_NAME;
00145 
00146     // --------- NAMED INTERFACE INSTANCES ---------
00147 
00184     virtual Ptr<NamedInterface> newNamedInterface(NamedInterface::Type interfaceType,
00185                                                   const Ptr<Description>& description,
00186                                                   const String& instanceName,
00187                                                   NamedInterface::Identifier identifier) = 0;
00188 
00189     // same as above but assigns identifier automatically from a locally unique pool
00190     virtual Ptr<NamedInterface> newNamedInterface(NamedInterface::Type interfaceType,
00191                                                   const Ptr<Description>& description,
00192                                                   const String& instanceName) = 0;
00193 
00208     virtual Ptr<NamedInterface> namedInterface(NamedInterface::Identifier identifier) const = 0;
00209 
00225     virtual Ptr<NamedInterface> namedInterface(const String& name) const = 0;
00226 
00263     virtual void deleteNamedInterface(const Ptr<NamedInterface>& interface) = 0;
00264 
00265     // ------- NAMED INTERFACE TYPES -------
00266 
00271     class TypeListener :
00272       public BaseMultiListener<NamedInterface::Manager, TypeListener, NamedInterface::Type> {
00273     public:
00294       virtual Ptr<NamedInterface> onNewNamedInterface(const Ptr<Description>& constructorData,
00295                                                       const String& instanceName,
00296                                                       NamedInterface::Identifier identifier) = 0;
00297 
00309       virtual void onDeleteNamedInterface(const Ptr<NamedInterface>& interface) = 0;
00310     };
00311 
00332     virtual void typeListener(NamedInterface::Type type, TypeListener* listener) = 0;
00333 
00372     template <class T>
00373     Ptr<TypeListener> newNamedInterfaceType(void) {
00374       
00375       // create default listener for client
00376       Ptr<DefaultTypeListener<T> > typeListenerPtr(new DefaultTypeListener<T>());
00377       typeListener(T::INTERFACE_TYPE, typeListenerPtr.ptr());
00378 
00379       try {
00380                 // Set type name if name is non-null string
00381                 if (T::INTERFACE_TYPE_NAME != "") {
00382                   typeNameType(T::INTERFACE_TYPE_NAME, T::INTERFACE_TYPE);
00383                 }
00384       } catch (...) {
00385                 // Remove listener if setting type name failed.
00386                 typeListener(T::INTERFACE_TYPE, 0);
00387                 throw ;
00388       }
00389 
00390       return typeListenerPtr;
00391     }
00392 
00410     virtual void typeNameType(const String& typeName, NamedInterface::Type type) = 0;
00411 
00426     virtual NamedInterface::Type typeNameType(const String& typeName) const = 0;
00427 
00428   protected:
00429     // Calls interface->setType(type);
00430     void setNamedInterfaceType(const Ptr<NamedInterface>& interface, NamedInterface::Type type);
00431 
00432     // Calls interface->setName(name);
00433     void setNamedInterfaceName(const Ptr<NamedInterface>& interface, const String& name);
00434 
00435     // Calls interface->setIdentifier(identifier);
00436     void setNamedInterfaceIdentifier(const Ptr<NamedInterface>& interface,
00437                                      NamedInterface::Identifier identifier);
00438  
00439   private:
00440     // A default listener implementation that returns a default
00441     // constructed instance of the template parameter type T.
00442     template <class T>
00443     class DefaultTypeListener : public TypeListener {
00444       virtual Ptr<NamedInterface> onNewNamedInterface(const Ptr<Description>& description,
00445                                                       const String& instanceName,
00446                                                       NamedInterface::Identifier identifier) {
00447         const NamedInterface::Manager* mgr = notifier();
00448         return Ptr<NamedInterface>(new T(description, instanceName, identifier, mgr));
00449       }
00450       virtual void onDeleteNamedInterface(const Ptr<NamedInterface>& interface) { }
00451     };
00452   };
00453 
00454   
00455 }; // namespace Phx
00456 
00457 #endif /* PHX_NAMED_INTERFACE_H */

Generated on Wed Dec 21 22:05:37 2005 for Phoenix OSFS by  doxygen 1.4.2