WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZblSprocket.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2014 Bob Dinitto
4  *
5  * Filename: ZblSprocket.cpp
6  * Created on: 12/10/2014
7  * Author: Bob Dinitto
8  *
9  * Zuble is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "ZblSprocket.h"
26 #include "ZblSprockOb.h"
27 #include "zglobal.h"
28 #include <QObject>
29 #include <QTextStream>
30 #include <QDebug>
31 
32 namespace Zbl
33 {
34 
35 #if 0
36 bool ZblSprocket::operator==(const ZblSprocket& other) const
37 {
38  //return (qobject_cast<const QObject*>(other.m_sprocket))
39  // == (qobject_cast<const QObject*>(m_sprocket));
40  return false;
41 }
42 #endif
43 
44 
46  const char* uri,
47  int versionMajor,
48  int versionMinor,
49  const char* qmlName,
50  sprocketObjectConstructor createFunc)
51 {
52 
53  ZblSprockOb* sprock = findSprock(versionMajor, versionMinor, qmlName);
54 
55  if(sprock)
56  {
57  qWarning("ZblSprocket::registerSprocketObject - object "
58  "registration FAILED: object already exists: "
59  "%s:%s:%d:%d", uri, qmlName, versionMajor, versionMinor );
60  return false;
61  }
62 
63  sprock = new ZblSprockOb(uri, versionMajor, versionMinor,
64  qmlName, createFunc);
65 
66  if(!sprock)
67  {
68  qWarning("ZblSprocket::registerSprocketObject - object "
69  "registration FAILED: can't bind sprocket object: "
70  "%s:%s:%d:%d", uri, qmlName, versionMajor, versionMinor );
71  return false;
72  }
73 
74  if(!insertSprock(sprock))
75  return false;
76 
77  return true;
78 }
79 
81  int versionMajor,
82  int versionMinor,
83  const char* qmlName)
84 {
85  sprocketVersionMap* versions = m_objects.value(qmlName, NULL);
86 
87  if(!versions)
88  return NULL;
89 
90  // Magical QML version semantics implemented here!!! if the key doesn't
91  // exist select the version key that is closest to the requested
92  // import version ie the highest object version less than import version
93 
94  int requestedVersion = versionKey(versionMajor, versionMinor);
95 
96  ZblSprockOb* sproc = versions->value(requestedVersion, NULL);
97 
98  if(sproc)
99  return sproc; // key exists
100 
101  // not lucky so work for it!
102 
103  QList<int> keys(versions->keys());
104 
105  if(keys.isEmpty())
106  return NULL; // this shouldn't ever happen due to lazy map construction
107 
108  int nextKey;
109 
110  for(int i=0,previous=-1; i < keys.size(); i++)
111  {
112  nextKey = keys.at(i);
113 
114  if(nextKey > requestedVersion)
115  {
116  if(previous == -1)
117  {
118  return NULL; // no objects of this type in requested version
119  }
120  else
121  {
122  // previous is highest object version in requested version
123 
124  nextKey = previous;
125  break;
126  }
127  }
128  previous = nextKey;
129  }
130 
131  return versions->value(nextKey, NULL);
132 }
133 
135 {
136  sprocketVersionMap* versions = m_objects.value(sprock->qmlName(), NULL);
137 
138  const int key = versionKey(sprock->versionMajor(), sprock->versionMinor());
139 
140  if(!versions)
141  {
142  versions = new sprocketVersionMap(); // lazy map construction
143 
144  m_objects.insert(sprock->qmlName(), versions);
145  }
146  else
147  {
148  if(versions->contains(key))
149  {
150  qWarning() << "ZblSprocket::insertSprock FAILED: object already exists:"
151  << sprocketTag(
152  cStr(sprock->qmlName()),
153  sprock->versionMajor(),
154  sprock->versionMinor()
155  );
156  return false;
157  }
158  }
159 
160  versions->insert(key, sprock);
161 
162  return true;
163 }
164 
165 
167  const char* uri,
168  int versionMajor,
169  int versionMinor
170  )
171 {
172  QString ret;
173  QTextStream ts(&ret);
174  ts << uri << ":" << versionMajor << ":" << versionMinor;
175  return ret;
176 }
177 
178 const QString ZblSprocket::sprocketTag(const ZblSprocket* sprocket)
179 {
180  return sprocketTag(sprocket->uri().toUtf8().constData(), sprocket->versionMajor(), sprocket->versionMinor());
181 }
182 
183 } // Zbl
int versionMinor() const
Definition: ZblSprockOb.h:74
This class holds the state for managing a Zuble sprocket.
Definition: ZblSprocket.h:38
const QString & uri() const
returns the Sprocket plugin&#39;s uri
Definition: ZblSprocket.h:72
int versionMinor() const
returns the Sprocket plugin&#39;s minor version number
Definition: ZblSprocket.h:84
sprocketFoundryMap m_objects
Maps object QML names to maps of sprocket object foundaries. Each foundary contains a separate object...
Definition: ZblSprocket.h:197
ZblSprockOb * findSprock(int versionMajor, int versionMinor, const char *qmlName)
Locates a version-specific Sprocket object constructor in the Sprocket&#39;s object foundry based on the ...
Definition: ZblSprocket.cpp:80
bool registerSprocketObject(const char *uri, int versionMajor, int versionMinor, const char *qmlName, sprocketObjectConstructor createFunc)
Creates a new ZblSprockOb Sprocket object constructor and adds it to the Sprocket&#39;s object foundary...
Definition: ZblSprocket.cpp:45
int versionMajor() const
returns the Sprocket plugin&#39;s major version number
Definition: ZblSprocket.h:78
Definition: ZAndGate.cpp:6
int versionMajor() const
Definition: ZblSprockOb.h:72
const QString & qmlName() const
Definition: ZblSprockOb.h:76
int versionKey(int versionMajor, int versionMinor)
Computes a version key for the specified version number.
Definition: ZblSprocket.h:146
const QString sprocketTag() const
returns a string in the form of "<uri>:<majorVersion>:<minorVersion>"
Definition: ZblSprocket.h:90
#define cStr(qStr)
Definition: zglobal.h:49
QObject *(* sprocketObjectConstructor)(QObject *parent)
A pointer to a function that creates a Zuble Sprocket object. Sprocket object constructor functions s...
Definition: ZblPlugin.h:45
This class acts as an object construction wrapper for objects defined in Zuble Sprocket plugins...
Definition: ZblSprockOb.h:42
QMap< int, ZblSprockOb * > sprocketVersionMap
Maps sprocket object versions to version-specific object construction wrappers.
Definition: ZblPlugin.h:65
bool insertSprock(ZblSprockOb *sprock)
Adds a Sprocket constructor object to the Sprocket&#39;s object foundry.