File indexing completed on 2024-05-19 04:49:28

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef METACAPABILITY_H
0018 #define METACAPABILITY_H
0019 
0020 #include "core/capabilities/Capability.h"
0021 #include "core/amarokcore_export.h"
0022 
0023 class AMAROKCORE_EXPORT MetaCapability
0024 {
0025     public:
0026         virtual ~MetaCapability() {}
0027 
0028         /**
0029          * Return true if this entity has capability @c CapIface, false otherwise.
0030          */
0031         template <class CapIface> bool has() const
0032         {
0033             return hasCapabilityInterface( CapIface::capabilityInterfaceType() );
0034         }
0035 
0036         /**
0037          * Creates a specialized interface which represents a capability of this
0038          * Meta::Base object. The caller of this method is responsible for deleting
0039          * created capability!
0040          *
0041          * @returns a pointer to the capability interface if it exists, 0 otherwise
0042          */
0043         template <class CapIface> CapIface *create()
0044         {
0045             Capabilities::Capability::Type type = CapIface::capabilityInterfaceType();
0046             Capabilities::Capability *iface = createCapabilityInterface( type );
0047             return qobject_cast<CapIface *>( iface );
0048         }
0049 
0050         /**
0051          * Subclasses should override this method to denote they provide particular
0052          * capability type. Must match @see createCapabilityInterface()
0053          *
0054          * This method should be considered protected (but is not because of practical
0055          * reasons), you should normally call @see has()
0056          */
0057         virtual bool hasCapabilityInterface( Capabilities::Capability::Type type ) const;
0058 
0059         /**
0060          * Subclasses should override this method to create particular capability.
0061          * Memory-management of the returned pointer is the responsibility of the
0062          * caller of this method. Must match @see hasCapabilityInterface()
0063          *
0064          * This method should be considered protected (but is not because of practical
0065          * reasons), you should normally call @see create()
0066          */
0067         virtual Capabilities::Capability *createCapabilityInterface( Capabilities::Capability::Type type );
0068 };
0069 
0070 #endif // METACAPABILITY_H