00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanIdentifierOwner.h" 00028 00029 #include "CeylanIdentifier.h" // for CeylanIdentifier 00030 #include "CeylanUtils.h" // for emergencyShutdown 00031 #include "CeylanLogLight.h" // for CEYLAN_LOG 00032 //#include "CeylanLogPlug.h" // for LogPlug 00033 00034 00035 #ifdef CEYLAN_USES_CONFIG_H 00036 #include "CeylanConfig.h" // for CEYLAN_DEBUG 00037 #endif // CEYLAN_USES_CONFIG_H 00038 00039 00040 00041 using std::string ; 00042 00043 using namespace Ceylan ; 00044 00045 00046 00047 IdentifierNotAvailableException::IdentifierNotAvailableException( 00048 const string & message ) : 00049 Ceylan::Exception( message ) 00050 { 00051 00052 } 00053 00054 00055 00056 IdentifierNotAvailableException::~IdentifierNotAvailableException() throw() 00057 { 00058 00059 } 00060 00061 00062 00063 IdentifierOwner::IdentifierOwner() : 00064 _id( 0 ) 00065 { 00066 00067 CEYLAN_LOG( "IdentifierOwner constructor" ) ; 00068 00069 } 00070 00071 00072 00073 IdentifierOwner::~IdentifierOwner() throw() 00074 { 00075 00076 if ( hasIdentifier() ) 00077 deleteIdentifier() ; 00078 00079 } 00080 00081 00082 00083 Identifier & IdentifierOwner::getIdentifier() const 00084 { 00085 00086 if ( _id != 0 ) 00087 { 00088 return * _id ; 00089 } 00090 else 00091 { 00092 throw IdentifierNotAvailableException( 00093 "No available identifier to return for " 00094 "IdentifierOwner::getIdentifier" ) ; 00095 } 00096 00097 } 00098 00099 00100 00101 void IdentifierOwner::setIdentifier( Identifier & id ) 00102 { 00103 00104 if ( _id != 0 ) 00105 { 00106 throw IdentifierNotAvailableException( 00107 "IdentifierOwner::setIdentifier unable to assign " 00108 "new identifier cause a previous one is still available." ) ; 00109 } 00110 else 00111 { 00112 _id = & id ; 00113 } 00114 00115 } 00116 00117 00118 00119 bool IdentifierOwner::hasIdentifier() const 00120 { 00121 00122 return ( _id != 0 ) ; 00123 00124 } 00125 00126 00127 00128 void IdentifierOwner::deleteIdentifier() 00129 { 00130 00131 #if CEYLAN_DEBUG 00132 00133 if ( _id != 0 ) 00134 { 00135 delete _id ; 00136 _id = 0 ; 00137 } 00138 else 00139 { 00140 Ceylan::emergencyShutdown( 00141 "IdentifierOwner::deleteIdentifier: trying to " 00142 "delete a non-existing identifier." ) ; 00143 } 00144 00145 #else // CEYLAN_DEBUG 00146 00147 delete _id ; 00148 _id = 0 ; 00149 00150 #endif // CEYLAN_DEBUG 00151 00152 } 00153 00154 00155 00156 const string IdentifierOwner::toString( VerbosityLevels level ) const 00157 { 00158 00159 if ( _id != 0 ) 00160 { 00161 return "This identifier owner has a registered identifier: " 00162 + _id->toString( level ) ; 00163 } 00164 else 00165 { 00166 return "This identifier owner has no registered identifier" ; 00167 } 00168 00169 } 00170