Update of /cvsroot/objecthandler/ObjectHandler/oh
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22074/oh
Modified Files:
objecthandler.cpp objecthandler.hpp
Log Message:
1) case-preserving ObjectID behaviour implemented with just
one map using the case insensitive comparison functor iless
2) use iterator post-increment to deal with erase invalidation effect
3) clearPermanent() and clearAll() merged into clearResidentObjects(bool deletePermanent)
Index: objecthandler.hpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/oh/objecthandler.hpp,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** objecthandler.hpp 26 Oct 2006 10:14:41 -0000 1.16
--- objecthandler.hpp 2 Jan 2007 16:36:19 -0000 1.17
***************
*** 25,29 ****
#include <oh/object.hpp>
#include <oh/objhandlerdefines.hpp>
- #include <map>
//! ObjHandler
--- 25,28 ----
***************
*** 56,60 ****
template <class T>
! void retrieveObject(boost::shared_ptr<T> &ret, const std::string &id) {
boost::shared_ptr<Object> object = retrieveObjectImpl(id);
ret = boost::dynamic_pointer_cast<T> (object);
--- 55,60 ----
template <class T>
! void retrieveObject(boost::shared_ptr<T> &ret,
! const std::string &id) {
boost::shared_ptr<Object> object = retrieveObjectImpl(id);
ret = boost::dynamic_pointer_cast<T> (object);
***************
*** 65,71 ****
}
! void retrieveObject(
! boost::shared_ptr<Object> &ret,
! const std::string &id) {
ret = retrieveObjectImpl(id);
}
--- 65,70 ----
}
! void retrieveObject(boost::shared_ptr<Object> &ret,
! const std::string &id) {
ret = retrieveObjectImpl(id);
}
***************
*** 106,122 ****
}
! // convert a vector of strings to a vector of objects
! template <class ObjectClass>
! std::vector<boost::shared_ptr<ObjectClass> > getObjectVector(
! const std::vector<std::string> &objectIDs) {
! std::vector<boost::shared_ptr<ObjectClass> > ret;
! for (std::vector<std::string>::const_iterator i = objectIDs.begin();
! i != objectIDs.end(); i++) {
! OH_GET_OBJECT(objectPointer, *i, ObjectClass);
! ret.push_back(objectPointer);
! }
! return ret;
! }
#endif
-
--- 105,123 ----
}
! // TODO move into the ObjHandler namespace
! // convert a vector of strings to a vector of objects
! template <class ObjectClass>
! std::vector<boost::shared_ptr<ObjectClass> > getObjectVector(
! const std::vector<std::string> &objectIDs) {
! std::vector<boost::shared_ptr<ObjectClass> > ret;
! ret.reserve(objectIDs.size());
! for (std::vector<std::string>::const_iterator i = objectIDs.begin();
! i != objectIDs.end();
! ++i) {
! OH_GET_OBJECT(objectPointer, *i, ObjectClass);
! ret.push_back(objectPointer);
! }
! return ret;
! }
#endif
Index: objecthandler.cpp
===================================================================
RCS file: /cvsroot/objecthandler/ObjectHandler/oh/objecthandler.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** objecthandler.cpp 2 Jan 2007 11:15:26 -0000 1.7
--- objecthandler.cpp 2 Jan 2007 16:36:19 -0000 1.8
***************
*** 1,5 ****
/*
! Copyright (C) 2005 Ferdinando Ametrano
Copyright (C) 2004, 2005, 2006 Eric Ehlers
--- 1,5 ----
/*
! Copyright (C) 2005, 2006, 2007 Ferdinando Ametrano
Copyright (C) 2004, 2005, 2006 Eric Ehlers
***************
*** 23,32 ****
#include <oh/objecthandler.hpp>
#include <oh/exception.hpp>
! #include <oh/utilities.hpp>
#include <ostream>
#include <sstream>
- #include <boost/algorithm/string/case_conv.hpp>
- #include <boost/regex.hpp>
#include <algorithm>
namespace ObjHandler {
--- 23,34 ----
#include <oh/objecthandler.hpp>
#include <oh/exception.hpp>
! #include <oh/iless.hpp>
! #include <boost/regex.hpp>
#include <ostream>
#include <sstream>
#include <algorithm>
+ #include <map>
+
+ using std::string;
namespace ObjHandler {
***************
*** 38,50 ****
// Map object ID to object reference
! std::map<std::string, boost::shared_ptr<Object> > repository_;
!
! // Map uppercase object ID to the ID in original case,
! // e.g. convertCase_["MYOBJECT"] = "MyObject"
! // This allows "case-preserving" behavior:
// storeObject("MyObject") // store "MyObject"
// retrieveObject("MyObJeCt") // retrieve "MyObject"
// storeObject("MYOBJECT") // overwrite "MyObject"
! std::map<std::string, std::string> convertCase_;
ObjectHandler::ObjectHandler() {
--- 40,49 ----
// Map object ID to object reference
! // the iless comparison functor class allows "case-preserving" behavior:
// storeObject("MyObject") // store "MyObject"
// retrieveObject("MyObJeCt") // retrieve "MyObject"
// storeObject("MYOBJECT") // overwrite "MyObject"
! typedef std::map<string, boost::shared_ptr<Object>, my_iless> OhRepository;
! OhRepository repository_;
ObjectHandler::ObjectHandler() {
***************
*** 62,70 ****
throw Exception("Attempt to reference uninitialized ObjectHandler object");
}
!
! std::string ObjectHandler::storeObject(
! const std::string &objectID,
! const boost::shared_ptr<Object> &object) {
! convertCase_[boost::algorithm::to_upper_copy(objectID)] = objectID;
repository_[objectID] = object;
return objectID;
--- 61,69 ----
throw Exception("Attempt to reference uninitialized ObjectHandler object");
}
!
! // TODO: implement Scott Meyers' "Effective STL" item 24
! string ObjectHandler::storeObject(
! const string &objectID,
! const boost::shared_ptr<Object> &object) {
repository_[objectID] = object;
return objectID;
***************
*** 72,108 ****
boost::shared_ptr<Object> ObjectHandler::retrieveObjectImpl(
! const std::string &objectID) const {
! std::map<std::string, std::string>::const_iterator result
! = convertCase_.find(boost::algorithm::to_upper_copy(objectID));
! if (result == convertCase_.end()) {
std::ostringstream msg;
! msg << "ObjectHandler error: attempt to retrieve object "
! << "with unknown ID '" << objectID << "'";
throw Exception(msg.str());
! } else
! return repository_[result->second];
}
! void ObjectHandler::deleteObject(const std::string &objectID) {
! repository_.erase(convertCase_[boost::algorithm::to_upper_copy(objectID)]);
! convertCase_.erase(boost::algorithm::to_upper_copy(objectID));
}
void ObjectHandler::deleteAllObjects(const bool &deletePermanent) {
! if (deletePermanent) {
repository_.clear();
! convertCase_.clear();
! } else {
! std::map<std::string, boost::shared_ptr<Object> >::const_iterator iter_current, iter_previous;
! iter_current = repository_.begin();
! while (iter_current != repository_.end()) {
! iter_previous = iter_current;
! iter_current++;
! std::string key = iter_previous->first;
! boost::shared_ptr<Object> object = iter_previous->second;
! if (!object->permanent()) {
! repository_.erase(key);
! convertCase_.erase(boost::algorithm::to_upper_copy(key));
! }
}
}
--- 71,101 ----
boost::shared_ptr<Object> ObjectHandler::retrieveObjectImpl(
! const string &objectID) const {
! OhRepository::const_iterator result = repository_.find(objectID);
! if (result != repository_.end())
! return result->second;
! else {
std::ostringstream msg;
! msg << "ObjectHandler error: attempt to retrieve object with "
! "unknown ID '" << objectID << "'";
throw Exception(msg.str());
! }
}
! void ObjectHandler::deleteObject(const string &objectID) {
! repository_.erase(objectID);
}
void ObjectHandler::deleteAllObjects(const bool &deletePermanent) {
! if (deletePermanent)
repository_.clear();
! else {
! OhRepository::iterator i = repository_.begin();
! while (i != repository_.end()) {
! if (!i->second->permanent())
! // post-increment the iterator!!
! repository_.erase(i++);
! else
! ++i;
}
}
***************
*** 110,118 ****
void ObjectHandler::dump(std::ostream& out) {
! out << "dump of all objects in ObjectHandler:" << std::endl << std::endl;
! for (std::map<std::string, boost::shared_ptr<Object> >::const_iterator i=repository_.begin();
! i!=repository_.end(); i++) {
boost::shared_ptr<Object> object = i->second;
! out << "Object with ID = " << i->first << ":" << std::endl << *object.get();
}
}
--- 103,113 ----
void ObjectHandler::dump(std::ostream& out) {
! out << "dump of all objects in ObjectHandler:" <<
! std::endl << std::endl;
! for (OhRepository::const_iterator i=repository_.begin();
! i!=repository_.end(); ++i) {
boost::shared_ptr<Object> object = i->second;
! out << "Object with ID = " << i->first << ":" <<
! std::endl << *object.get();
}
}
***************
*** 122,150 ****
}
! const std::vector<std::string> ObjectHandler::listObjectIDs(
! const std::string ®ex) {
! std::vector<std::string> objectIDs;
if (regex.empty()) {
! for (std::map<std::string, boost::shared_ptr<Object> >::const_iterator i=repository_.begin(); i!=repository_.end(); i++)
objectIDs.push_back(i->first);
} else {
boost::regex r(regex);
! for (std::map<std::string, boost::shared_ptr<Object> >::const_iterator i=repository_.begin(); i!=repository_.end(); i++) {
! std::string objectID = i->first;
if (regex_match(objectID, r)) objectIDs.push_back(objectID);
}
}
- std::sort(objectIDs.begin(), objectIDs.end());
return objectIDs;
}
! void ObjectHandler::checkName(const std::string &objectID) {
! std::map<std::string, std::string>::const_iterator result =
! convertCase_.find(boost::algorithm::to_upper_copy(objectID));
! if (result != convertCase_.end()) {
std::ostringstream msg;
! msg << "ObjectHandler error: cannot create object "
! "with ID '" << objectID <<
! "' because an object with that name already exists";
throw Exception(msg.str());
}
--- 117,150 ----
}
! const std::vector<string> ObjectHandler::listObjectIDs(
! const string ®ex) {
! std::vector<string> objectIDs;
if (regex.empty()) {
! objectIDs.reserve(repository_.size());
! for (OhRepository::const_iterator i=repository_.begin();
! i!=repository_.end();
! ++i)
objectIDs.push_back(i->first);
} else {
boost::regex r(regex);
! for (OhRepository::const_iterator i=repository_.begin();
! i!=repository_.end();
! ++i) {
! string objectID = i->first;
! // FIXME: it should be case insensitive
if (regex_match(objectID, r)) objectIDs.push_back(objectID);
}
}
return objectIDs;
}
! void ObjectHandler::checkName(const string &objectID) {
! OhRepository::const_iterator result = repository_.find(objectID);
! if (result != repository_.end()) {
std::ostringstream msg;
! msg << "ObjectHandler error: cannot create object with ID '" <<
! objectID << "' because an object with the equivalent name '" <<
! result->first <<
! "' already exists";
throw Exception(msg.str());
}
***************
*** 152,154 ****
}
-
--- 152,153 ----
|