summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/resource-set.h62
-rw-r--r--include/resource.h87
-rw-r--r--src/resource-set.cpp55
-rw-r--r--src/resource.cpp27
-rw-r--r--tests/test-resource-set.cpp59
-rw-r--r--tests/test-resource.cpp116
-rw-r--r--tests/test-resource.h12
7 files changed, 224 insertions, 194 deletions
diff --git a/include/resource-set.h b/include/resource-set.h
index b62e5cd..193dabc 100644
--- a/include/resource-set.h
+++ b/include/resource-set.h
@@ -4,37 +4,83 @@
#include "resource.h"
#include <QString>
#include <QObject>
-#include <QSet>
+#include <QVector>
+#include <QList>
namespace ResourcePolicy
{
+ /**
+ * The resourceSet repesents a set of attributes. Each set can only contain
+ * a single Resource of a given type. That is one AudioPlaybackResource, etc.
+ *
+ * Internally the set is stored as a QVector of \ref Resource objects.
+ *
+ * \b Examples:
+ * This example shows how to create a resourceSet used by a video player
+ * \code
+ * Resource audioResource(AudioPlaybackResource);
+ * Resource videoResource(VideoPlaybackResource);
+ * ResourceSet myResourceSet("player");
+ * myResourceSet.addResource(audioResource);
+ * myResourceSet.addResource(videoResource);
+ * \endcode
+ */
class ResourceSet: public QObject
{
Q_OBJECT
Q_DISABLE_COPY( ResourceSet )
public:
+ /**
+ * The constructor.
+ * \param applicationClass This parameter defines the application class.
+ * The application class is used to determine the priority order of the
+ * application.
+ */
ResourceSet(const QString &applicationClass);
+ /**
+ * The destructor
+ */
~ResourceSet();
+ /**
+ * This method adds a resource to the set. A set contains only a single
+ * instance of a given resource. If the ResourceSet already contains a
+ * resource of the given type it will be overridden.
+ * \param resource The resource to add to the set.
+ */
void addResource(const Resource &resource);
- void addResources(const QSet<Resource> &resources);
- void setResources(const QSet<Resource> &resources);
- QSet<Resource> resources();
+ /**
+ * This method adds (and replaces) the list of resource to the set.
+ * \param resources The list of resources to add to the set.
+ */
+ void addResources(const QList<Resource> &resources);
+ /**
+ * This method clears the old resources and replaces the set with the
+ * resources from the given list.
+ * \param resources The list of resources that the set should contain.
+ */
+ void setResources(const QList<Resource> &resources);
+
+ /**
+ * This method returns a list of all resource in the set.
+ * \return a QList of all resources in the set.
+ */
+ QList<Resource> resources();
bool contains(const Resource &resource) const;
- bool contains(const QSet<Resource> &resources) const;
+ bool contains(const QList<Resource> &resources) const;
quint32 id() const;
signals:
- void resourcesBecameAvailable(QSet<Resource> resources);
- void resourcesAcquired(QSet<Resource> grantedResources);
+ void resourcesBecameAvailable(QList<Resource> availableResources);
+ void resourcesAcquired(QList<Resource> grantedResources);
void resourcesDenied();
void lostResources();
private:
quint32 identifier;
const QString applicationClass;
- QSet<Resource> resourceSet;
+ QVector<Resource> resourceSet;
};
}
diff --git a/include/resource.h b/include/resource.h
index 3d71de6..ef3290c 100644
--- a/include/resource.h
+++ b/include/resource.h
@@ -10,10 +10,11 @@ namespace ResourcePolicy {
*/
enum ResourceType {
InvalidResource = 0,
- AudioPlaybackResource = 0x01,
- VideoPlaybackResource = 0x02,
- AudioRecorderResource = 0x04,
- VideoRecorderResource = 0x08
+ AudioPlaybackResource,
+ VideoPlaybackResource,
+ AudioRecorderResource,
+ VideoRecorderResource,
+ ResourceGuard
};
/**
@@ -21,26 +22,87 @@ namespace ResourcePolicy {
* Resources is to use then in a ResourceSet.
*
* Other Resource types can be added later, these reflect the resources found
- * in the Policy configuration.
+ * in the Policy configuration.
+ *
+ * \b Examples:
+ * This example shows how to make a resource which represents an optional
+ * audio requirement.
+ * \code
+ * Resource audioResource(AudioPlaybackResource);
+ * audioResource->setOptional();
+ * \endcode
+ * This example shows how to create the resources used by a video player
+ * \code
+ * Resource audioResource(AudioPlaybackResource);
+ * Resource videoResource(VideoPlaybackResource);
+ * \endcode
*/
class Resource
{
public:
+ /**
+ * Constructor. Creates a new identifier. The new identifier defaults to
+ * not optional, and not shared. The default type is undefined and
+ * \ref setType() needs to be called to set the type. Optionally a
+ * resource type may be given to the constructor, to avoid having to
+ * call the setType() method.
+ */
+ Resource(enum ResourceType type=InvalidResource);
+ /**
+ * Copy constructor. Copies also the identifier.
+ * \param other The source to copy from.
+ */
Resource(const Resource &other);
- Resource();
+ /**
+ * Assignement operator. Works like the copy constructor.
+ * \param other The source to assign from.
+ */
Resource &operator=(const Resource &other);
-
+ //! Destructor.
~Resource();
+ /**
+ * Returns the type of the resource.
+ * \return the type of the resource.
+ */
ResourceType type() const;
+ /**
+ * Sets the resource type.
+ * \param type The type to set to.
+ * \return true if the type is valid.
+ */
bool setType(enum ResourceType type);
+ /**
+ * Whether or not this resource is optional, in that it doesn't need to
+ * be available for the set to be acquired.
+ * \return true when this resource is optional.
+ */
bool isOptional() const;
+ /**
+ * Set the resource to be optional or mandatory.
+ * \param resourceIsOptional This optional parameter defaults to true.
+ * The default, true, results in the resource becoming optional. When it
+ * is set to false the resource becomes mandatory.
+ */
void setOptional(bool resourceIsOptional=true);
+ /**
+ * Whether or not the resource to be shared. If it is shared then other
+ * programs are allowed to share this resource.
+ * \return true when this resource is shared.
+ */
bool isShared() const;
- void setShared(bool shared=true);
- quint32 id() const;
- void setId(quint32 newId);
-
+ /**
+ * Sets the resource to be shared/private.
+ * \param resourceIsShared This optional parameter defaults to true.
+ * When it is set to true (de default) the resource is shared with
+ * other programs. Setting it to false makes this resource exclusive.
+ */
+ void setShared(bool resourceIsShared=true);
+ /**
+ * Compares two resources and returns true if both are of the same \b type.
+ * \param other The resrouce to compare to.
+ * \return true if the types of both resources are of the same type.
+ */
bool operator==(const Resource &other) const;
private:
ResourceType resourceType;
@@ -48,8 +110,7 @@ namespace ResourcePolicy {
bool shared;
quint32 identifier;
};
-
- uint qHash(const ResourcePolicy::Resource &key);
+
}
#endif
diff --git a/src/resource-set.cpp b/src/resource-set.cpp
index 999baec..af691b1 100644
--- a/src/resource-set.cpp
+++ b/src/resource-set.cpp
@@ -1,48 +1,71 @@
#include "resource-set.h"
+using namespace ResourcePolicy;
-ResourcePolicy::ResourceSet::ResourceSet(const QString &applicationClass)
- : applicationClass(applicationClass), resourceSet()
+ResourceSet::ResourceSet(const QString &applicationClass)
+ : applicationClass(applicationClass), resourceSet(ResourceGuard)
{
identifier = (quint32)this;
}
-ResourcePolicy::ResourceSet::~ResourceSet()
+ResourceSet::~ResourceSet()
{
}
-void ResourcePolicy::ResourceSet::addResource(const ResourcePolicy::Resource &resource)
+void ResourceSet::addResource(const Resource &resource)
{
- resourceSet.insert(resource);
+ resourceSet.insert(resource.type(), resource);
}
-void ResourcePolicy::ResourceSet::addResources(const QSet<ResourcePolicy::Resource> &resources)
+void ResourceSet::addResources(const QList<Resource> &resources)
{
- resourceSet.unite(resources);
+ for(int i=0; i < resources.size(); i++) {
+ addResource(resources.at(i));
+ }
}
-void ResourcePolicy::ResourceSet::setResources(const QSet<ResourcePolicy::Resource> &resources)
+void ResourceSet::setResources(const QList<Resource> &resources)
{
+ Resource invalidResource;
resourceSet.clear();
- resourceSet = resources;
+ resourceSet.resize(ResourceGuard);
+ for(int i=0; i < resources.size(); i++) {
+ addResource(resources.at(i));
+ }
}
-bool ResourcePolicy::ResourceSet::contains(const ResourcePolicy::Resource &resource) const
+bool ResourceSet::contains(const Resource &resource) const
{
- return resourceSet.contains(resource);
+ if(resourceSet.at(resource.type()) == resource)
+ return true;
+ else
+ return false;
}
-bool ResourcePolicy::ResourceSet::contains(const QSet<ResourcePolicy::Resource> &resources) const
+bool ResourceSet::contains(const QList<Resource> &resources) const
{
- return resourceSet.contains(resources);
+ bool containsAll=false;
+ for(int i=0; i<resources.size(); i++) {
+ containsAll = contains(resources.at(i));
+ if(!containsAll) {
+ break;
+ }
+ }
+ return containsAll;
}
-quint32 ResourcePolicy::ResourceSet::id() const
+quint32 ResourceSet::id() const
{
return identifier;
}
-QSet<ResourcePolicy::Resource> ResourcePolicy::ResourceSet::resources()
+QList<Resource> ResourceSet::resources()
{
- return resourceSet;
+ QList<Resource> listOfResources;
+ for(int i=0; i < resourceSet.size(); i++) {
+ if(resourceSet.at(i).type() != InvalidResource) {
+ listOfResources.append(resourceSet.at(i));
+ }
+ }
+ return listOfResources;
}
diff --git a/src/resource.cpp b/src/resource.cpp
index a54c8cf..72ac24e 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -2,9 +2,9 @@
using namespace ResourcePolicy;
-Resource::Resource() : resourceType(InvalidResource), optional(false), shared(false)
+Resource::Resource(enum ResourceType type)
+ : resourceType(type), optional(false), shared(false)
{
- identifier = (quint32)this;
}
Resource::~Resource()
@@ -13,7 +13,7 @@ Resource::~Resource()
Resource::Resource(const Resource &other)
: resourceType(other.resourceType), optional(other.optional),
- shared(other.shared), identifier(other.identifier)
+ shared(other.shared)
{
}
@@ -22,7 +22,6 @@ Resource & Resource::operator=(const Resource &other)
resourceType = other.resourceType;
optional = other.optional;
shared = other.shared;
- identifier = other.identifier;
return *this;
}
@@ -63,30 +62,10 @@ void Resource::setShared(bool shared)
this->shared = shared;
}
-quint32 Resource::id() const
-{
- return identifier;
-}
-
-void Resource::setId(quint32 newId)
-{
- identifier = newId;
-}
-
bool Resource::operator==(const Resource &other) const
{
if(resourceType != other.resourceType) {
return false;
}
- if((shared != other.shared) or
- (optional != other.optional))
- {
- return false;
- }
return true;
}
-
-uint ResourcePolicy::qHash(const Resource & key)
-{
- return (((key.type()<<1) + key.isShared()) << 1) + key.isOptional();
-}
diff --git a/tests/test-resource-set.cpp b/tests/test-resource-set.cpp
index b05a16b..a2834c7 100644
--- a/tests/test-resource-set.cpp
+++ b/tests/test-resource-set.cpp
@@ -41,44 +41,75 @@ void TestResourceSet::testAddResource()
resourceSet->addResource(resource);
- QSet<Resource> resourcesInSet = resourceSet->resources();
+ QList<Resource> resourcesInSet = resourceSet->resources();
QVERIFY(resourcesInSet.contains(resource));
}
void TestResourceSet::testAddResources()
{
- QSet<Resource> resources;
+ QList<Resource> resources;
resources << audioPlayback << videoPlayback;
resourceSet->addResources(resources);
- QSet<Resource> resourcesInSet = resourceSet->resources();
- QVERIFY(resourcesInSet == resources);
+ QList<Resource> resourcesInSet = resourceSet->resources();
+ bool resultContainsAllItems = false;
+ for(int i=0; i<resourcesInSet.size(); i++) {
+ resultContainsAllItems = resources.contains(resourcesInSet.at(i));
+ if(!resultContainsAllItems) {
+ qDebug("resources doesn't contain Resource 0x%02x", resourcesInSet.at(i).type());
+ break;
+ }
+ }
+ QVERIFY(resultContainsAllItems);
+ for(int i=0; i<resources.size(); i++) {
+ resultContainsAllItems = resourcesInSet.contains(resources.at(i));
+ if(!resultContainsAllItems) {
+ break;
+ }
+ }
+ QVERIFY(resultContainsAllItems);
}
void TestResourceSet::testSetResources()
{
- QSet<Resource> resources, newResources;
+ QList<Resource> resources, newResources;
newResources << audioPlayback << videoPlayback;
resources << audioPlayback << videoPlayback << audioRecorder << videoRecorder;
resourceSet->setResources(resources);
- QSet<Resource> resourcesInSet = resourceSet->resources();
+ QList<Resource> resourcesInSet = resourceSet->resources();
resourceSet->setResources(newResources);
- QSet<Resource> resourcesInNewSet = resourceSet->resources();
- QVERIFY(resourcesInNewSet == newResources);
+ QList<Resource> resourcesInNewSet = resourceSet->resources();
+ bool resultContainsAllItems = false;
+ for(int i=0; i<resourcesInNewSet.size(); i++) {
+ resultContainsAllItems = newResources.contains(resourcesInNewSet.at(i));
+ if(!resultContainsAllItems) {
+ qDebug("newResources doesn't contain Resource 0x%02x", resourcesInNewSet.at(i).type());
+ break;
+ }
+ }
+ QVERIFY(resultContainsAllItems);
+ for(int i=0; i<newResources.size(); i++) {
+ resultContainsAllItems = resourcesInNewSet.contains(newResources.at(i));
+ if(!resultContainsAllItems) {
+ qDebug("newResources doesn't contain Resource 0x%02x", resourcesInNewSet.at(i).type());
+ break;
+ }
+ }
+ QVERIFY(resultContainsAllItems);
}
void TestResourceSet::testContainsSingle()
{
- QSet<Resource> resources;
+ QList<Resource> resources;
resources << audioPlayback << videoPlayback << audioRecorder;
resourceSet->setResources(resources);
- QSet<Resource> resourcesInSet = resourceSet->resources();
+ QList<Resource> resourcesInSet = resourceSet->resources();
resourceSet->setResources(resources);
bool containsVideoPlayback = resourceSet->contains(videoPlayback);
@@ -87,11 +118,11 @@ void TestResourceSet::testContainsSingle()
void TestResourceSet::testDoesNotContainSingle()
{
- QSet<Resource> resources;
+ QList<Resource> resources;
resources << audioPlayback << videoPlayback;
resourceSet->setResources(resources);
- QSet<Resource> resourcesInSet = resourceSet->resources();
+ QList<Resource> resourcesInSet = resourceSet->resources();
resourceSet->setResources(resources);
bool containsVideoRecorder = resourceSet->contains(videoRecorder);
@@ -102,13 +133,13 @@ void TestResourceSet::testDoesNotContainSingle()
void TestResourceSet::testContainsSet()
{
- QSet<Resource> resources, subset;
+ QList<Resource> resources, subset;
resources << audioPlayback << videoPlayback << audioRecorder << videoRecorder;
subset << audioPlayback << videoPlayback;
resourceSet->setResources(resources);
- QSet<Resource> resourcesInSet = resourceSet->resources();
+ QList<Resource> resourcesInSet = resourceSet->resources();
resourceSet->setResources(resources);
bool containsSubset = resourceSet->contains(subset);
diff --git a/tests/test-resource.cpp b/tests/test-resource.cpp
index afccc16..e5c60bd 100644
--- a/tests/test-resource.cpp
+++ b/tests/test-resource.cpp
@@ -74,7 +74,7 @@ void TestResource::testShared_data()
QTest::addColumn<bool>("expected");
QTest::newRow("Resource is shared") << true << true;
- QTest::newRow("Resource is not hhared") << false << false;
+ QTest::newRow("Resource is not shared") << false << false;
}
void TestResource::testShared()
@@ -89,27 +89,6 @@ void TestResource::testShared()
QCOMPARE(result, expected);
}
-void TestResource::testIdentifier_data()
-{
- QTest::addColumn<quint32>("identifier");
- QTest::addColumn<quint32>("expected");
-
- QTest::newRow("Set identifier") << (quint32)this << (quint32)this;
- QTest::newRow("Set to 0") << 0U << 0U;
-}
-
-void TestResource::testIdentifier()
-{
- QFETCH(quint32, identifier);
- QFETCH(quint32, expected);
-
- resource->setType(AudioPlaybackResource);
- resource->setId(identifier);
- quint32 result = resource->id();
-
- QCOMPARE(result, expected);
-}
-
void TestResource::testCopy()
{
Resource copy;
@@ -118,7 +97,6 @@ void TestResource::testCopy()
copy = *resource;
- QCOMPARE(copy.id(), resource->id());
QCOMPARE(copy.isOptional(), resource->isOptional());
QCOMPARE(copy.isShared(), resource->isShared());
QCOMPARE(copy.type(), resource->type());
@@ -130,7 +108,6 @@ void TestResource::testCopyConstructor()
resource->setOptional();
Resource copy(*resource);
- QCOMPARE(copy.id(), resource->id());
QCOMPARE(copy.isOptional(), resource->isOptional());
QCOMPARE(copy.isShared(), resource->isShared());
QCOMPARE(copy.type(), resource->type());
@@ -140,102 +117,25 @@ void TestResource::testEqualsOperator()
{
Resource copy;
resource->setType(AudioPlaybackResource);
- resource->setOptional();
-
copy = *resource;
- QVERIFY(copy == *resource);
-}
-
-void TestResource::testHashFunctionWithCopies()
-{
- resource->setType(AudioPlaybackResource);
resource->setOptional();
resource->setShared();
- Resource copy(*resource);
- uint originalHash = qHash(*resource);
- uint copyHash = qHash(copy);
-
- QCOMPARE(originalHash, copyHash);
-}
-
-void TestResource::testHashFunctionWithDifferentResources()
-{
- resource->setType(AudioPlaybackResource);
- resource->setOptional();
- resource->setShared();
- Resource copy(*resource);
- copy.setType(VideoPlaybackResource);
-
- uint originalHash = qHash(*resource);
- uint copyHash = qHash(copy);
-
- QEXPECT_FAIL("", "Expecting to fail since they are different resources", Continue);
- QCOMPARE(originalHash, copyHash);
-}
-
-void TestResource::testHashFunctionWithDifferentOptionality()
-{
- resource->setType(AudioPlaybackResource);
- resource->setOptional();
- resource->setShared();
- Resource copy(*resource);
- copy.setOptional(false);
-
- uint originalHash = qHash(*resource);
- uint copyHash = qHash(copy);
-
- QEXPECT_FAIL("", "Expecting to fail since one isOptional, the other isn't", Continue);
- QCOMPARE(originalHash, copyHash);
+ QVERIFY(copy == *resource);
}
-void TestResource::testHashFunctionWithDifferentSharedness()
+void TestResource::testEqualsOperatorWithDifferentTypes()
{
+ Resource copy;
resource->setType(AudioPlaybackResource);
- resource->setOptional();
- resource->setShared();
- Resource copy(*resource);
- copy.setShared(false);
-
- uint originalHash = qHash(*resource);
- uint copyHash = qHash(copy);
-
- QEXPECT_FAIL("", "Expecting to fail since one isShared , the other isn't", Continue);
- QCOMPARE(originalHash, copyHash);
-}
+ copy = *resource;
-void TestResource::testHashFunctionWithIdentical()
-{
- resource->setType(AudioPlaybackResource);
resource->setOptional();
- resource->setShared();
- Resource copy;
- copy.setOptional();
- copy.setShared();
- copy.setType(AudioPlaybackResource);
-
- uint originalHash = qHash(*resource);
- uint copyHash = qHash(copy);
-
- QCOMPARE(originalHash, copyHash);
-}
+ copy.setType(VideoPlaybackResource);
-void TestResource::testQSetOfResource()
-{
- Resource a,b,c;
- a.setType(AudioPlaybackResource);
- b.setType(VideoPlaybackResource);
- c.setType(AudioRecorderResource);
-
- QSet<Resource> set;
- set.insert(a);
- set.insert(b);
- set.insert(c);
-
- QVERIFY(set.contains(a));
- QVERIFY(set.contains(b));
- QVERIFY(set.contains(c));
+ QEXPECT_FAIL("", "Expecting to fail since different types.", Continue);
+ QVERIFY(copy == *resource);
}
QTEST_MAIN(TestResource)
diff --git a/tests/test-resource.h b/tests/test-resource.h
index 76ba5b4..b0e6964 100644
--- a/tests/test-resource.h
+++ b/tests/test-resource.h
@@ -33,21 +33,11 @@ private slots:
void testShared_data();
void testShared();
- void testIdentifier_data();
- void testIdentifier();
-
void testCopy();
void testCopyConstructor();
void testEqualsOperator();
-
- void testHashFunctionWithCopies();
- void testHashFunctionWithDifferentResources();
- void testHashFunctionWithDifferentOptionality();
- void testHashFunctionWithDifferentSharedness();
- void testHashFunctionWithIdentical();
-
- void testQSetOfResource();
+ void testEqualsOperatorWithDifferentTypes();
};
#endif