• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • rcgen
safecode.cpp
Go to the documentation of this file.
1 /*
2  *
3  * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
4  *
5  * This file is part of the Nepomuk KDE project.
6  * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * See the file "COPYING.LIB" for the exact licensing terms.
13  */
14 
15 #include "safecode.h"
16 
17 #include "property.h"
18 #include "resourceclass.h"
19 
20 static const QString s_typeComment =
21 " // We always store all Resource types as plain Resource objects.\n"
22 " // It does not introduce any overhead (due to the implicit sharing of\n"
23 " // the data and has the advantage that we can mix setProperty calls\n"
24 " // with the special Resource subclass methods.\n"
25 " // More importantly Resource loads the data as Resource objects anyway.\n";
26 
27 QString SafeCode::propertySetterDeclaration( const Property* property, const ResourceClass* rc, const QString &nameSpace ) const
28 {
29  return QString( "void %1set%2%3%4( const %5& value )" )
30  .arg( !nameSpace.isEmpty() ? QString("%1::%2::").arg(nameSpace).arg(rc->name()) : QString() )
31  .arg( property->name()[0].toUpper() )
32  .arg( property->name().mid(1) )
33  .arg( property->isList() ? (property->name().endsWith('s') ? QLatin1String("es") : QLatin1String("s") ) : QString() )
34  .arg( property->typeString( false, nameSpace ) );
35 }
36 
37 QString SafeCode::propertyGetterDeclaration( const Property* property, const ResourceClass* rc, const QString &nameSpace ) const
38 {
39  return QString( "%1 %2%3%4%5() const" )
40  .arg( property->typeString( false, nameSpace ) )
41  .arg( !nameSpace.isEmpty() ? QString("%1::%2::").arg(nameSpace).arg(rc->name()) : QString() )
42  .arg( property->name()[0].toLower() )
43  .arg( property->name().mid(1) )
44  .arg( property->isList() ? (property->name().endsWith('s') ? QLatin1String("es") : QLatin1String("s") ) : QString() );
45 }
46 
47 QString SafeCode::propertyAdderDeclaration( const Property* property, const ResourceClass* rc, const QString &nameSpace ) const
48 {
49  return QString( "void %1add%2%3( const %4& value )" )
50  .arg( !nameSpace.isEmpty() ? QString("%1::%2::").arg(nameSpace).arg(rc->name()) : QString() )
51  .arg( property->name()[0].toUpper() )
52  .arg( property->name().mid(1) )
53  .arg( property->typeString( true, nameSpace ) );
54 }
55 
56 QString SafeCode::propertyReversePropertyGetterDeclaration( const Property* property, const ResourceClass* rc, const QString &nameSpace ) const
57 {
58  Q_ASSERT( rc );
59  Q_ASSERT( property->domain() );
60  return QString( "%1 %2%3%4Of() const" )
61  .arg( QString("QList<") + property->domain(true)->name( nameSpace ) + QString(">") )
62  .arg( !nameSpace.isEmpty() ? QString("%1::%2::").arg(nameSpace).arg(rc->name()) : QString() )
63  .arg( property->name()[0].toLower() )
64  .arg( property->name().mid(1) );
65 }
66 
67 QString SafeCode::resourceAllResourcesDeclaration( const ResourceClass* rc, const QString &nameSpace ) const
68 {
69  return QString( "QList<%1%2> %3all%2s()" )
70  .arg( !nameSpace.isEmpty() ? nameSpace + "::" : QString() )
71  .arg( rc->name() )
72  .arg( !nameSpace.isEmpty() ? QString("%1::%2::").arg(nameSpace).arg( rc->name() ) : QString() );
73 }
74 
75 QString SafeCode::resourcePseudoInheritanceDeclaration( const ResourceClass* baseRc, const ResourceClass* rc, const QString &nameSpace ) const
76 {
77  return QString( "%1 %2to%3() const" )
78  .arg( rc->name( nameSpace ) )
79  .arg( !nameSpace.isEmpty() ? baseRc->name( "Nepomuk2" ) + "::" : QString() )
80  .arg( rc->name() );
81 }
82 
83 QString SafeCode::propertySetterDefinition( const Property* property, const ResourceClass* rc ) const
84 {
85  QString s = propertySetterDeclaration( property, rc, "Nepomuk2" ) + '\n';
86 
87  if( property->hasSimpleType() || property->typeString( true ) == "Resource" || !property->isList() ) {
88  s += QString("{\n"
89  " setProperty( QUrl::fromEncoded(\"%1\"), Variant( value ) );\n"
90  "}\n" )
91  .arg( property->uri().toString() );
92  }
93  else if( property->isList() ) {
94  s += QString("{\n"
95  "%1"
96  " QList<Resource> l;\n"
97  " for( %2::const_iterator it = value.constBegin();\n"
98  " it != value.constEnd(); ++it ) {\n"
99  " l.append( Resource( (*it) ) );\n"
100  " }\n"
101  " setProperty( QUrl::fromEncoded(\"%3\"), Variant( l ) );\n"
102  "}\n" )
103  .arg( s_typeComment )
104  .arg( property->typeString() )
105  .arg( property->uri().toString() );
106  }
107  else {
108  s += QString("{\n"
109  "%1"
110  " setProperty( QUrl::fromEncoded(\"%2\"), Variant( Resource( value ) ) );\n"
111  "}\n" )
112  .arg( s_typeComment )
113  .arg( property->uri().toString() );
114  }
115 
116  return s;
117 }
118 
119 QString SafeCode::propertyGetterDefinition( const Property* property, const ResourceClass* rc ) const
120 {
121  QString s = propertyGetterDeclaration( property, rc, "Nepomuk2" ) + '\n';
122 
123  if( property->hasSimpleType() ) {
124  s += QString( "{\n"
125  " return ( property( QUrl::fromEncoded(\"%1\") ).%2;\n"
126  "}\n" )
127  .arg( property->uri().toString() )
128  .arg( property->literalTypeConversionMethod() );
129  }
130  else if( property->isList() ) {
131  s += QString("{\n"
132  "%1"
133  " QList<%3> rl;\n"
134  " QList<Resource> l = property( QUrl::fromEncoded(\"%2\") ).toResourceList() ;\n"
135  " for( QList<Resource>::const_iterator it = l.constBegin();\n"
136  " it != l.constEnd(); ++it )\n"
137  " rl.append( %3( *it ) );\n"
138  " return rl;\n"
139  "}\n" )
140  .arg( s_typeComment )
141  .arg( property->uri().toString() )
142  .arg( property->typeString( true ) );
143  }
144  else {
145  s += QString("{\n"
146  "%1"
147  " return %2( property( QUrl::fromEncoded(\"%3\") ).toResource().uri() );\n"
148  "}\n" )
149  .arg( s_typeComment )
150  .arg( property->typeString( true ) )
151  .arg( property->uri().toString() );
152  }
153 
154  return s;
155 }
156 
157 QString SafeCode::propertyAdderDefinition( const Property* property, const ResourceClass* rc ) const
158 {
159  QString s = propertyAdderDeclaration( property, rc, "Nepomuk2" ) + '\n';
160 
161  if( property->hasSimpleType() ) {
162  s += QString( "{\n"
163  " Variant v = property( QUrl::fromEncoded(\"%1\") );\n"
164  " v.append( value );\n"
165  " setProperty( QUrl::fromEncoded(\"%1\"), v );\n"
166  "}\n" )
167  .arg( property->uri().toString() );
168  }
169  else {
170  s += QString( "{\n"
171  "%1"
172  " Variant v = property( QUrl::fromEncoded(\"%2\") );\n"
173  " v.append( Resource( value ) );\n"
174  " setProperty( QUrl::fromEncoded(\"%2\"), v );\n"
175  "}\n" )
176  .arg( s_typeComment )
177  .arg( property->uri().toString() );
178  }
179 
180  return s;
181 }
182 
183 QString SafeCode::propertyReversePropertyGetterDefinition( const Property* property, const ResourceClass* rc ) const
184 {
185  QString s = propertyReversePropertyGetterDeclaration( property, rc, "Nepomuk2" ) + '\n';
186 
187  s += QString( "{\n"
188  //" return convertResourceList<%2>( manager()->allResourcesWithProperty( QUrl::fromEncoded(\"%1\"), *this ) );\n"
189  " QList<%2> rl;\n"
190  " QList<Resource> l = ResourceManager::instance()->allResourcesWithProperty( QUrl::fromEncoded(\"%1\"), *this );\n"
191  " for( QList<Resource>::const_iterator it = l.constBegin();\n"
192  " it != l.constEnd(); ++it )\n"
193  " rl.append( %2( *it ) );\n"
194  " return rl;\n"
195  "}\n" )
196  .arg( property->uri().toString() )
197  .arg( property->domain(true)->name() );
198 
199  return s;
200 }
201 
202 QString SafeCode::resourceAllResourcesDefinition( const ResourceClass* rc ) const
203 {
204  return QString( "%1\n"
205  "{\n"
206  //" return Nepomuk2::convertResourceList<%3>( ResourceManager::instance()->allResourcesOfType( QUrl::fromEncoded(\"%2\") ) );\n"
207  " QList<%3> rl;\n"
208  " QList<Resource> l = ResourceManager::instance()->allResourcesOfType( QUrl::fromEncoded(\"%2\") );\n"
209  " for( QList<Resource>::const_iterator it = l.constBegin();\n"
210  " it != l.constEnd(); ++it )\n"
211  " rl.append( %3( *it ) );\n"
212  " return rl;\n"
213  "}\n" )
214  .arg( resourceAllResourcesDeclaration( rc, "Nepomuk2" ) )
215  .arg( rc->uri().toString() )
216  .arg( rc->name() );
217 }
218 
219 QString SafeCode::resourcePseudoInheritanceDefinition( const ResourceClass* baseRc, const ResourceClass* rc ) const
220 {
221  return QString( "%1\n"
222  "{\n"
223  " return %2( *this );\n"
224  "}\n" )
225  .arg( resourcePseudoInheritanceDeclaration( baseRc, rc, "Nepomuk2" ) )
226  .arg( rc->name( "Nepomuk2" ) );
227 }
resourceclass.h
Property::isList
bool isList() const
Returns whether the property is a list of values.
Definition: rcgen/property.cpp:88
SafeCode::propertyAdderDeclaration
QString propertyAdderDeclaration(const Property *property, const ResourceClass *resourceClass, const QString &nameSpace=QString()) const
Returns the declaration of the property adder method.
Definition: safecode.cpp:47
s_typeComment
static const QString s_typeComment
Definition: safecode.cpp:20
SafeCode::resourceAllResourcesDefinition
QString resourceAllResourcesDefinition(const ResourceClass *resourceClass) const
Returns the definition of the resource method that returns all resources.
Definition: safecode.cpp:202
Property::uri
QUrl uri() const
Returns the uri of the property.
Definition: rcgen/property.cpp:48
ResourceClass::uri
QUrl uri() const
Returns the uri of the resource.
Definition: resourceclass.cpp:43
SafeCode::propertyReversePropertyGetterDeclaration
QString propertyReversePropertyGetterDeclaration(const Property *property, const ResourceClass *resourceClass, const QString &nameSpace=QString()) const
Returns the declaration of the property for the reverse property getter method.
Definition: safecode.cpp:56
SafeCode::propertyAdderDefinition
QString propertyAdderDefinition(const Property *property, const ResourceClass *resourceClass) const
Returns the definition of the property adder method.
Definition: safecode.cpp:157
SafeCode::propertySetterDeclaration
QString propertySetterDeclaration(const Property *property, const ResourceClass *resourceClass, const QString &nameSpace=QString()) const
Returns the declaration of the property setter method.
Definition: safecode.cpp:27
ResourceClass
Represents a resource.
Definition: resourceclass.h:30
Property::hasSimpleType
bool hasSimpleType() const
Returns whether the property is of simple type.
Definition: rcgen/property.cpp:159
SafeCode::resourcePseudoInheritanceDefinition
QString resourcePseudoInheritanceDefinition(const ResourceClass *resourceBaseClass, const ResourceClass *resourceClass) const
Returns the definition of the resource method that provides pseudo inheritance.
Definition: safecode.cpp:219
SafeCode::propertyReversePropertyGetterDefinition
QString propertyReversePropertyGetterDefinition(const Property *property, const ResourceClass *resourceClass) const
Returns the definition of the property for the reverse property getter method.
Definition: safecode.cpp:183
SafeCode::propertyGetterDefinition
QString propertyGetterDefinition(const Property *property, const ResourceClass *resourceClass) const
Returns the definition of the property getter method.
Definition: safecode.cpp:119
SafeCode::propertySetterDefinition
QString propertySetterDefinition(const Property *property, const ResourceClass *resourceClass) const
Returns the definition of the property setter method.
Definition: safecode.cpp:83
Property::literalTypeConversionMethod
QString literalTypeConversionMethod() const
Returns the conversion method of the property.
Definition: rcgen/property.cpp:164
Property::domain
ResourceClass * domain(bool onlyReturnGeneratedClass=false) const
Returns the domain resource the property belongs to.
Definition: rcgen/property.cpp:98
SafeCode::resourceAllResourcesDeclaration
QString resourceAllResourcesDeclaration(const ResourceClass *resourceClass, const QString &nameSpace=QString()) const
Returns the declaration of the resource method that returns all resources.
Definition: safecode.cpp:67
Property
Represents the property of a resource.
Definition: rcgen/property.h:29
ResourceClass::name
QString name(const QString &nameSpace=QString()) const
Returns the name of the resource.
Definition: resourceclass.cpp:122
SafeCode::resourcePseudoInheritanceDeclaration
QString resourcePseudoInheritanceDeclaration(const ResourceClass *resourceBaseClass, const ResourceClass *resourceClass, const QString &nameSpace) const
Returns the declaration of the resource method that provides pseudo inheritance.
Definition: safecode.cpp:75
property.h
SafeCode::propertyGetterDeclaration
QString propertyGetterDeclaration(const Property *property, const ResourceClass *resourceClass, const QString &nameSpace=QString()) const
Returns the declaration of the property getter method.
Definition: safecode.cpp:37
Property::typeString
QString typeString(bool simple=false, const QString &nameSpace=QString()) const
Retrieve a string representation of the range.
Definition: rcgen/property.cpp:132
safecode.h
Property::name
QString name() const
Returns the name of the property.
Definition: rcgen/property.cpp:119
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal