/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */ /* $Id: cmnObjectRegister.cpp,v 1.11 2007/04/26 19:33:56 anton Exp $ Author(s): Anton Deguet Created on: 2004-10-05 (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights Reserved. --- begin cisst license - do not edit --- This software is provided "as is" under an open source license, with no warranty. The complete license can be found in license.txt and http://www.cisst.org/cisst/license.txt. --- end cisst license --- */ #include cmnObjectRegister* cmnObjectRegister::Instance(void) { // create a static variable static cmnObjectRegister instance; return &instance; } bool cmnObjectRegister::RegisterInstance(const std::string & objectName, cmnGenericObject * objectPointer) { // try to find this object first iterator what = ObjectContainer.find(objectName); if (what == ObjectContainer.end()) { // verify that the pointer itself is not registered const iterator end = ObjectContainer.end(); iterator iter = ObjectContainer.begin(); iterator found = end; while ((iter != end) && (found == end)) { if (iter->second == objectPointer) { found = iter; } iter++; } if (found != end) { // pointer already registered CMN_LOG(1) << "class cmnObjectRegister: Registration failed. There is already a registered object with the address: " << objectPointer << " (name: " << found->first << ")" << std::endl; return false; } else { // actually register ObjectContainer[objectName] = objectPointer; return true; } } else { // name already used CMN_LOG(1) << "class cmnObjectRegister: Registration failed. There is already a registered object with the name: " << objectName << std::endl; return false; } return false; } bool cmnObjectRegister::RemoveInstance(const std::string & objectName) { if (ObjectContainer.erase(objectName) == 0) { CMN_LOG(1) << "class cmnObjectRegister: " << objectName << " can not be removed from the register since it is not registered" << std::endl; return false; } return true; } cmnGenericObject* cmnObjectRegister::FindObjectInstance(const std::string& objectName) const { cmnGenericObject * result = NULL; const_iterator what = ObjectContainer.find(objectName); if (what != ObjectContainer.end()) { result = what->second; } return result; } std::string cmnObjectRegister::FindNameInstance(cmnGenericObject * objectPointer) const { const const_iterator end = ObjectContainer.end(); const_iterator iter = ObjectContainer.begin(); const_iterator found = end; while ((iter != end) && (found == end)) { if (iter->second == objectPointer) { return iter->first; } iter++; } return "undefined"; } void cmnObjectRegister::ToStreamInstance(std::ostream & outputStream) const { const_iterator iterator; const const_iterator end = ObjectContainer.end(); for (iterator = ObjectContainer.begin(); iterator != end; iterator++) { outputStream << " " << iterator->first << " (" << ((iterator->second)->Services())->GetName() << ")"; } } // **************************************************************************** // Change History // **************************************************************************** // // $Log: cmnObjectRegister.cpp,v $ // Revision 1.11 2007/04/26 19:33:56 anton // All files in libraries: Applied new license text, separate copyright and // updated dates, added standard header where missing. // // Revision 1.10 2006/11/20 20:33:19 anton // Licensing: Applied new license to cisstCommon, cisstVector, cisstNumerical, // cisstInteractive, cisstImage and cisstOSAbstraction. // // Revision 1.9 2006/03/10 14:24:00 anton // cmnObjectRegister: Added begin() and end() to provide a way to browse the // registered objects. Renamed Iterator and ConstIterator to match STL conventions // // Revision 1.8 2005/09/26 15:41:46 anton // cisst: Added modelines for emacs and vi. // // Revision 1.7 2005/05/19 19:29:01 anton // cisst libs: Added the license to cisstCommon and cisstVector // // Revision 1.6 2005/02/03 19:06:55 anton // cmnObjectRegister.cpp: Removed commented line. // // Revision 1.5 2005/01/25 18:06:27 alamora // ToStream no longer prints "Registered objects:" // The IRE expects ToStream output in this format: // var1 (var1_type) var2 (var2_type) // Update the IRE when changes are made to ToStream // // Revision 1.4 2004/10/14 16:14:55 anton // cmnClassRegister and cmnObjectRegister: Removed useless std::endl in // ToStream() methods. // // Revision 1.3 2004/10/06 19:36:29 anton // cmnObjectRegister: Improved and tested version with icc, gcc, .net (2000/ // 2003). I decided to greatly simplify the code not registering the object // register in the class register (it was twisted, complex and useless). // // Revision 1.2 2004/10/05 22:01:22 anton // cisstCommon: Rewrote cmnObjectRegister. This code is untested. // // // ****************************************************************************