From 32bed890b6a595813929af6c7d3ca6f0b1268b07 Mon Sep 17 00:00:00 2001 From: Gasper Jansa Date: Fri, 21 Jan 2011 16:09:04 +0100 Subject: [PATCH 1/2] configuration , changed key in maps from char* to std::string --- pvAccessApp/utils/configuration.cpp | 56 +++++------------------------ pvAccessApp/utils/configuration.h | 17 +++------ testApp/utils/configurationTest.cpp | 1 + 3 files changed, 15 insertions(+), 59 deletions(-) diff --git a/pvAccessApp/utils/configuration.cpp b/pvAccessApp/utils/configuration.cpp index 97d3ec9..f68aaf6 100644 --- a/pvAccessApp/utils/configuration.cpp +++ b/pvAccessApp/utils/configuration.cpp @@ -33,38 +33,24 @@ Properties::~Properties() delete _infile; delete _outfile; //clear map - for(_propertiesIterator = _properties.begin() ; - _propertiesIterator != _properties.end(); - _propertiesIterator++ ) - { - delete [] _propertiesIterator->first; - delete [] _propertiesIterator->second; - } _properties.clear(); } void Properties::setProperty(const string key,const string value) { string oldValue; - _propertiesIterator = _properties.find(key.c_str()); + _propertiesIterator = _properties.find(key); if(_propertiesIterator != _properties.end()) //found in map { - delete[] _propertiesIterator->first; - delete[] _propertiesIterator->second; _properties.erase(_propertiesIterator); } - - char* chKey = new char[key.length() + 1]; - strncpy(chKey,key.c_str(),key.length()+ 1); - char* chValue = new char[value.length() + 1]; - strncpy(chValue,value.c_str(),value.length() + 1); - _properties[chKey] = chValue; + _properties[key] = value; } string Properties::getProperty(const string key) { - _propertiesIterator = _properties.find(key.c_str()); + _propertiesIterator = _properties.find(key); if(_propertiesIterator != _properties.end()) //found in map { return string(_propertiesIterator->second); @@ -78,29 +64,18 @@ string Properties::getProperty(const string key) string Properties::getProperty(const string key, const string defaultValue) { - _propertiesIterator = _properties.find(key.c_str()); + _propertiesIterator = _properties.find(key); if(_propertiesIterator != _properties.end()) //found in map { return string(_propertiesIterator->second); } - char* chKey = new char[key.length() + 1]; - strncpy(chKey,key.c_str(),key.length()+ 1); - char* chValue = new char[defaultValue.length() + 1]; - strncpy(chValue,defaultValue.c_str(),defaultValue.length() + 1); - _properties[chKey] = chValue; + _properties[key] = defaultValue; return defaultValue; } void Properties::load() { - for (_propertiesIterator = _properties.begin() ; - _propertiesIterator != _properties.end(); - _propertiesIterator++ ) - { - delete [] _propertiesIterator->first; - delete [] _propertiesIterator->second; - } _properties.clear(); try @@ -148,12 +123,7 @@ void Properties::load() truncate(key); property = line.substr(pos + 1,line.length()); truncate(property); - - char* chKey = new char[key.length() + 1]; - strncpy(chKey,key.c_str(),key.length()+ 1); - char* chProperty = new char[property.length() +1]; - strncpy(chProperty,property.c_str(),property.length() + 1); - _properties[chKey] = chProperty; + _properties[key] = property; } } catch (ifstream::failure& e) @@ -302,32 +272,24 @@ ConfigurationProviderImpl::ConfigurationProviderImpl() ConfigurationProviderImpl::~ConfigurationProviderImpl() { - for(_configsIter = _configs.begin() ; - _configsIter != _configs.end(); - _configsIter++ ) - { - delete [] _configsIter->first; - } _configs.clear(); } void ConfigurationProviderImpl::registerConfiguration(const string name, const Configuration* configuration) { Lock guard(&_mutex); - _configsIter = _configs.find(name.c_str()); + _configsIter = _configs.find(name); if(_configsIter != _configs.end()) { string msg = "configuration with name " + name + " already registered"; throw BaseException(msg.c_str(), __FILE__, __LINE__); } - char* chKey = new char[name.length() + 1]; - strncpy(chKey,name.c_str(),name.length()+ 1); - _configs[chKey] = configuration; + _configs[name] = configuration; } Configuration* ConfigurationProviderImpl::getConfiguration(const string name) { - _configsIter = _configs.find(name.c_str()); + _configsIter = _configs.find(name); if(_configsIter != _configs.end()) { return const_cast(_configsIter->second); diff --git a/pvAccessApp/utils/configuration.h b/pvAccessApp/utils/configuration.h index d0ba249..02754e4 100644 --- a/pvAccessApp/utils/configuration.h +++ b/pvAccessApp/utils/configuration.h @@ -12,10 +12,10 @@ #include +#include #include #include #include -#include #include @@ -23,13 +23,6 @@ namespace epics { namespace pvAccess { #define MAX_NAME_LENGHT 300 -struct conf_cmp_str -{ - bool operator()(char const *a, char const *b) - { - return strcmp(a, b) < 0; - } -}; /** * Properties @@ -52,8 +45,8 @@ public: void list(); private: - std::map _properties; - std::map::iterator _propertiesIterator; + std::map _properties; + std::map::iterator _propertiesIterator; std::ifstream* _infile; std::ofstream* _outfile; std::string _fileName; @@ -181,8 +174,8 @@ public: void registerConfiguration(const std::string name, const Configuration* configuration); private: epics::pvData::Mutex _mutex; - std::map _configs; - std::map::iterator _configsIter; + std::map _configs; + std::map::iterator _configsIter; }; /** diff --git a/testApp/utils/configurationTest.cpp b/testApp/utils/configurationTest.cpp index ceecd1c..ff69124 100644 --- a/testApp/utils/configurationTest.cpp +++ b/testApp/utils/configurationTest.cpp @@ -68,6 +68,7 @@ int main(int argc, char *argv[]) assert(doubleProperty == 42); if(configuration) delete configuration; + if(configProvider) delete configProvider; getShowConstructDestruct()->constuctDestructTotals(stdout); return 0; } From 7d16a119df7e0fa1eac9b5e8b9f1f97c99807a66 Mon Sep 17 00:00:00 2001 From: Gasper Jansa Date: Mon, 24 Jan 2011 21:44:27 +0100 Subject: [PATCH 2/2] some fixes in introspection registry --- pvAccessApp/utils/introspectionRegistry.cpp | 18 ++++++++++-------- testApp/utils/introspectionRegistryTest.cpp | 19 ++++++------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pvAccessApp/utils/introspectionRegistry.cpp b/pvAccessApp/utils/introspectionRegistry.cpp index a50ee41..0a4f0db 100644 --- a/pvAccessApp/utils/introspectionRegistry.cpp +++ b/pvAccessApp/utils/introspectionRegistry.cpp @@ -45,7 +45,9 @@ FieldConstPtr IntrospectionRegistry::getIntrospectionInterface(const short id) Lock guard(&_mutex); _registryIter = _registry.find(id); if(_registryIter == _registry.end()) + { return NULL; + } return _registryIter->second; } @@ -103,6 +105,9 @@ void IntrospectionRegistry::printKeysAndValues(string name) buffer.clear(); cout << "\t" << "Key: "<< _registryIter->first << endl; cout << "\t" << "Value: " << _registryIter->second << endl; + _registryIter->second->dumpReferenceCount(&buffer,0); + cout << "\t" << "References: " << buffer.c_str() << endl; + buffer.clear(); _registryIter->second->toString(&buffer); cout << "\t" << "Value toString: " << buffer.c_str() << endl; } @@ -157,11 +162,6 @@ void IntrospectionRegistry::serialize(FieldConstPtr field, StructureConstPtr par { bool existing; const short key = registry->registerIntrospectionInterface(field, existing); - /*cout << "@@@@@@@@@" << endl; - cout << field->getFieldName() << endl; - cout << "address: " << field << endl; - cout << "existing: " << existing << endl; - cout << "key: " << key << endl;*/ if(existing) { control->ensureBuffer(1+sizeof(int16)/sizeof(int8)); @@ -242,7 +242,9 @@ FieldConstPtr IntrospectionRegistry::deserialize(ByteBuffer* buffer, Deserializa else if(typeCode == IntrospectionRegistry::ONLY_ID_TYPE_CODE) { control->ensureData(sizeof(int16)/sizeof(int8)); - return registry->getIntrospectionInterface(buffer->getShort()); + FieldConstPtr field = registry->getIntrospectionInterface(buffer->getShort()); + field->incReferenceCount(); // we inc, so that deserialize always returns a field with +1 ref. count (as when created) + return field; } // could also be a mask @@ -305,7 +307,6 @@ StructureConstPtr IntrospectionRegistry::deserializeStructureField(ByteBuffer* b } StructureConstPtr structure = _fieldCreate->createStructure(structureFieldName, size, fields); - //???????delete [] fields; return structure; } @@ -353,7 +354,8 @@ PVStructurePtr IntrospectionRegistry::deserializeStructureAndCreatePVStructure(B { return NULL; } - return _pvDataCreate->createPVStructure(NULL,static_cast(field)); + PVStructurePtr retVal = _pvDataCreate->createPVStructure(NULL,static_cast(field)); + return retVal; } void IntrospectionRegistry::serializeStatus(ByteBuffer* buffer, SerializableControl* control, Status* status) diff --git a/testApp/utils/introspectionRegistryTest.cpp b/testApp/utils/introspectionRegistryTest.cpp index 1770875..2c98fd4 100644 --- a/testApp/utils/introspectionRegistryTest.cpp +++ b/testApp/utils/introspectionRegistryTest.cpp @@ -89,7 +89,7 @@ ScalarArrayConstPtr getScalarArray(string name) StructureConstPtr getStructure(string name) { String properties("alarm"); - FieldConstPtr powerSupply[3]; + FieldConstPtrArray powerSupply = new FieldConstPtr[3]; powerSupply[0] = standardField->scalar( String("voltage"),pvDouble,properties); powerSupply[1] = standardField->scalar( @@ -97,7 +97,7 @@ StructureConstPtr getStructure(string name) powerSupply[2] = standardField->scalar( String("current"),pvDouble,properties); StructureConstPtr structure = standardField->structure(name,3,powerSupply); - PVField * pvField = pvDataCreate->createPVField(0,structure); + PVField *pvField = pvDataCreate->createPVField(0,structure); pvFieldArray.push_back(pvField); return structure; } @@ -105,7 +105,7 @@ StructureConstPtr getStructure(string name) StructureArrayConstPtr getStructureArray(string name1, string name2) { String properties("alarm"); - FieldConstPtr powerSupply[3]; + FieldConstPtrArray powerSupply = new FieldConstPtr[3]; powerSupply[0] = standardField->scalar( String("voltage"),pvDouble,properties); powerSupply[1] = standardField->scalar( @@ -407,17 +407,11 @@ void testSerializeStatus() int main(int argc, char *argv[]) { - cout << "DONE0" << endl; - cout << "DONE0" << endl; - cout << "DONE0" << endl; - cout << "DONE0" << endl; - cout << "DONE0" << endl; -/* pvDataCreate = getPVDataCreate(); statusCreate = getStatusCreate(); fieldCreate = getFieldCreate(); standardField = getStandardField(); - cout << "DONE1" << endl; + flusher = new SerializableControlImpl(); control = new DeserializableControlImpl(); @@ -426,7 +420,6 @@ int main(int argc, char *argv[]) clientRegistry = new IntrospectionRegistry(false); serverRegistry = new IntrospectionRegistry(true); - testRegistryPutGet(); testRegistryReset(); testSerialize(); @@ -450,7 +443,7 @@ int main(int argc, char *argv[]) if(clientRegistry) delete clientRegistry; if(serverRegistry) delete serverRegistry; - getShowConstructDestruct()->showDeleteStaticExit(stdout); - cout << "DONE" << endl;*/ + getShowConstructDestruct()->showDeleteStaticExit(stdout); + cout << "DONE" << endl; return 0; }