PIIODevice registration dramatically optimization
This commit is contained in:
@@ -84,7 +84,6 @@
|
||||
//!
|
||||
//! \section PIIODevice_sec7 Creating devices by unambiguous string
|
||||
//! There are some virtual functions to describe child class without its declaration.
|
||||
//! \n \a fullPathPrefix() should returns unique prefix of device
|
||||
//! \n \a constructFullPath() should returns full unambiguous string, contains prefix and all device parameters
|
||||
//! \n \a configureFromFullPath() provide configuring device from full unambiguous string without prefix and "://"
|
||||
//! \n Macro PIIODEVICE should be used instead of PIOBJECT
|
||||
@@ -265,15 +264,14 @@ void PIIODevice::write_func() {
|
||||
}
|
||||
|
||||
|
||||
PIIODevice * PIIODevice::newDeviceByPrefix(const PIString & prefix) {
|
||||
if (prefix.isEmpty()) return 0;
|
||||
PIVector<const PIObject * > rd(PICollection::groupElements("__PIIODevices__"));
|
||||
piForeachC (PIObject * d, rd) {
|
||||
if (prefix == ((const PIIODevice * )d)->fullPathPrefix()) {
|
||||
return ((const PIIODevice * )d)->copy();
|
||||
}
|
||||
PIIODevice * PIIODevice::newDeviceByPrefix(const char * prefix) {
|
||||
if (!prefix) return nullptr;
|
||||
PIConstChars p(prefix);
|
||||
for (const auto & i: fabrics()) {
|
||||
if (i.first == p)
|
||||
return i.second();
|
||||
}
|
||||
return 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -427,7 +425,7 @@ bool PIIODevice::configure(const PIString & config_file, const PIString & sectio
|
||||
|
||||
|
||||
PIString PIIODevice::constructFullPath() const {
|
||||
return fullPathPrefix() + "://" + constructFullPathDevice() + fullPathOptions();
|
||||
return PIStringAscii(fullPathPrefix()) + PIStringAscii("://") + constructFullPathDevice() + fullPathOptions();
|
||||
}
|
||||
|
||||
|
||||
@@ -489,14 +487,21 @@ void PIIODevice::splitFullPath(PIString fpwm, PIString * full_path, DeviceMode *
|
||||
|
||||
PIStringList PIIODevice::availablePrefixes() {
|
||||
PIStringList ret;
|
||||
PIVector<const PIObject * > rd(PICollection::groupElements("__PIIODevices__"));
|
||||
piForeachC (PIObject * d, rd) {
|
||||
ret << ((const PIIODevice * )d)->fullPathPrefix();
|
||||
}
|
||||
for (const auto & i: fabrics())
|
||||
ret << i.first.toString();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PIIODevice::registerDevice(const char * prefix, PIIODevice * (*fabric)()) {
|
||||
PIConstChars p(prefix);
|
||||
if (p.isEmpty()) return;
|
||||
//printf("registerDevice %s %d %d\n", prefix, p.isEmpty(), fabrics().size());
|
||||
if (!fabrics().contains(p))
|
||||
fabrics()[p] = fabric;
|
||||
}
|
||||
|
||||
|
||||
PIString PIIODevice::fullPathOptions() const {
|
||||
if (mode_ == ReadWrite && options_ == 0) return PIString();
|
||||
PIString ret(" (");
|
||||
@@ -511,8 +516,8 @@ PIString PIIODevice::fullPathOptions() const {
|
||||
|
||||
PIIODevice * PIIODevice::createFromFullPath(const PIString & full_path) {
|
||||
PIString prefix = full_path.left(full_path.find(":"));
|
||||
PIIODevice * nd = newDeviceByPrefix(prefix);
|
||||
if (!nd) return 0;
|
||||
PIIODevice * nd = newDeviceByPrefix(prefix.dataAscii());
|
||||
if (!nd) return nullptr;
|
||||
nd->configureFromFullPath(full_path.mid(prefix.length() + 3));
|
||||
cacheFullPath(full_path, nd);
|
||||
return nd;
|
||||
@@ -520,8 +525,8 @@ PIIODevice * PIIODevice::createFromFullPath(const PIString & full_path) {
|
||||
|
||||
|
||||
PIIODevice * PIIODevice::createFromVariant(const PIVariantTypes::IODevice & d) {
|
||||
PIIODevice * nd = newDeviceByPrefix(d.prefix);
|
||||
if (!nd) return 0;
|
||||
PIIODevice * nd = newDeviceByPrefix(d.prefix.dataAscii());
|
||||
if (!nd) return nullptr;
|
||||
nd->configureFromVariant(d);
|
||||
return nd;
|
||||
}
|
||||
@@ -550,6 +555,12 @@ void PIIODevice::cacheFullPath(const PIString & full_path, const PIIODevice * d)
|
||||
}
|
||||
|
||||
|
||||
PIMap<PIConstChars, PIIODevice * (*)()> & PIIODevice::fabrics() {
|
||||
static PIMap<PIConstChars, PIIODevice * (*)()> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool PIIODevice::threadedRead(uchar *readed, int size) {
|
||||
// piCout << "iodevice threaded read";
|
||||
if (ret_func_ != 0) return ret_func_(ret_data_, readed, size);
|
||||
|
||||
Reference in New Issue
Block a user