/usr/include/dataquay/objectmapper/ContainerBuilder.h is in libdataquay-dev 0.9-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Dataquay
A C++/Qt library for simple RDF datastore management.
Copyright 2009-2012 Chris Cannam.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Chris Cannam
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization.
*/
#ifndef _DATAQUAY_CONTAINER_BUILDER_H_
#define _DATAQUAY_CONTAINER_BUILDER_H_
#include <QHash>
#include <QString>
#include <QStringList>
#include <QVariant>
namespace Dataquay {
/**
* \class ContainerBuilder ContainerBuilder.h <dataquay/objectmapper/ContainerBuilder.h>
*
* ContainerBuilder is a utility class which assists with storage of
* arbitrary container objects into variant objects and subsequently
* retrieving them into lists.
*
* Given types T and Container (where Container is typically something
* like QList<T>), ContainerBuilder can convert a QVariantList holding
* type T into a QVariant holding Container, and back again.
*/
class ContainerBuilder
{
public:
/**
* Retrieve the single global instance of ContainerBuilder.
*/
static ContainerBuilder *getInstance();
/**
* ContainerKind describes the sort of behaviour a container
* displays with regard to ordering and structure. A container of
* kind SequenceKind is externally ordered (order must be
* preserved when serialising, for example); kind SetKind is
* unordered or internally ordered (its ordering does not need to
* be described).
*/
enum ContainerKind {
UnknownKind = 0,
SequenceKind, // e.g. list
SetKind // e.g. set
// perhaps also something for maps
};
/**
* Register Container as a container of kind ContainerKind holding
* type T. T must be something capable of being stored in a
* QVariant -- so typically a primitive type, a Qt utility type
* such as QString, or a pointer type. Both T and Container must
* be registered as Qt metatypes using qRegisterMetaType.
*
* The arguments typeName and containerName give the textual names
* for the types T and Container; these must be the same as were
* used when calling qRegisterMetaType.
*
* For example, to register QStringList:
*
* registerContainer<QString, QStringList>
* ("QString", "QStringList", SequenceKind);
*/
template <typename T, typename Container>
void registerContainer(QString typeName, QString containerName,
ContainerKind kind) {
registerContainerExtractor<T, Container>
(typeName, containerName, kind);
}
/**
* Return true if the container named containerName can be
* extracted from a variant. This is the case if containerName
* was the name of a container provided to an earlier call to
* registerContainer.
*/
bool canExtractContainer(QString containerName) {
return m_containerExtractors.contains(containerName);
}
/**
* Return true if the container named containerName can be
* injected into a variant. This is the case if containerName was
* the name of a container provided to an earlier call to
* registerContainer.
*/
bool canInjectContainer(QString containerName) {
return m_containerExtractors.contains(containerName);
}
/**
* Return the typeName that is associated with the given
* containerName. That is, when registerContainer was called for
* a container with name containerName, typeName is the name that
* was given as the first argument of that call.
*/
QString getTypeNameForContainer(QString containerName) {
if (!canExtractContainer(containerName)) return QString();
return m_containerExtractors[containerName]->getTypeName();
}
/**
* Return the kind of the container with the given containerName.
* That is, when registerContainer was called for a container with
* name containerName, return the kind that was given as the final
* argument of that call.
*/
ContainerKind getContainerKind(QString containerName) {
if (!canExtractContainer(containerName)) return UnknownKind;
return m_containerExtractors[containerName]->getKind();
}
/**
* Extract the named container type from the given variant object
* (which must hold that container type) and return a list of
* variants containing the individual elements in the container.
*/
QVariantList extractContainer(QString containerName, const QVariant &v) {
if (!canExtractContainer(containerName)) return QVariantList();
return m_containerExtractors[containerName]->extract(v);
}
/**
* Inject the named container type into a new variant object.
* Taking a list of variants holding the individual elements in
* the container, return a single variant holding the container
* itself.
*/
QVariant injectContainer(QString containerName, const QVariantList &vl) {
if (!canInjectContainer(containerName)) return QVariant();
return m_containerExtractors[containerName]->inject(vl);
}
private:
ContainerBuilder() {
registerContainer<QString, QStringList>
("QString", "QStringList", SequenceKind);
}
~ContainerBuilder() {
for (ContainerExtractorMap::iterator i = m_containerExtractors.begin();
i != m_containerExtractors.end(); ++i) {
delete *i;
}
}
template <typename T, typename Container>
void
registerContainerExtractor(QString typeName, QString containerName,
ContainerKind kind) {
m_containerExtractors[containerName] =
new ContainerExtractor<T, Container>(typeName, kind);
}
struct ContainerExtractorBase {
virtual ~ContainerExtractorBase() { }
virtual QVariantList extract(const QVariant &v) = 0;
virtual QVariant inject(const QVariantList &) = 0;
virtual QString getTypeName() const = 0;
virtual ContainerKind getKind() const = 0;
};
template <typename T, typename Container>
struct ContainerExtractor : public ContainerExtractorBase
{
ContainerExtractor(QString typeName, ContainerKind kind) :
m_typeName(typeName), m_kind(kind) { }
virtual QVariantList extract(const QVariant &v) {
Container tl = v.value<Container>();
QVariantList vl;
foreach (const T &t, tl) vl << QVariant::fromValue<T>(t);
return vl;
}
virtual QVariant inject(const QVariantList &vl) {
Container tl;
foreach (const QVariant &v, vl) tl << v.value<T>();
return QVariant::fromValue<Container>(tl);
}
virtual QString getTypeName() const {
return m_typeName;
}
virtual ContainerKind getKind() const {
return m_kind;
}
QString m_typeName;
ContainerKind m_kind;
};
typedef QHash<QString, ContainerExtractorBase *> ContainerExtractorMap;
ContainerExtractorMap m_containerExtractors;
};
}
#endif
|