00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "ExManagerId.h"
00025
00026 bool ExManagerId::m_flag = false;
00027 ExManagerId* ExManagerId::m_instance = NULL;
00028
00029 ExManagerId* ExManagerId::CreateSingleton(void){
00030 Guard(ExManagerId* ExManagerId::CreateSingleton(void))
00031 if(!m_flag)
00032 {
00033 m_flag = true;
00034 m_instance = new ExManagerId;
00035 }else
00036 {
00037 std::cout<<"Error singleton already created"<<std::endl;
00038 }
00039 return m_instance;
00040 UnGuard
00041 }
00042
00043 ExManagerId::ExManagerId()
00044 {
00045 Guard(ExManagerId::ExManagerId())
00046 m_ExIdCounter=0;
00047 UnGuard
00048 }
00049
00050 ExManagerId::~ExManagerId()
00051 {
00052 Guard(ExManagerId::~ExManagerId())
00053 UnGuard
00054 }
00055
00057
00058 void ExManagerId::Reset(void)
00059 {
00060 Guard(ExManagerId::Reset(void))
00061 m_MapObjectInfo->clear();
00062 delete m_MapObjectInfo;
00063 m_ExIdCounter=0;
00064 Init();
00065 UnGuard
00066 }
00067
00068 void ExManagerId::Init(void)
00069 {
00070 Guard(ExManagerId::Init(void))
00071 m_MapObjectInfo=new std::map< ExNihilo::ExId , ExNihilo::ExObjectInfo , std::less <ExNihilo::ExId > >;
00072 UnGuard
00073 }
00074
00075 std::string ExManagerId::GetAviableName(std::string Name,std::string Type,int occu)
00076 {
00077 Guard(std::string ExManagerId::GetAviableName(std::string Name,std::string Type,int occu))
00078 if(GetObjectId(Name,Type)==-1)
00079 {
00080 return Name;
00081 }
00082
00083
00084 if(occu>0)
00085 {
00086 Name.erase(Name.end()-1,Name.end());
00087 }
00088 if(occu>10)
00089 {
00090 Name.erase(Name.end()-1,Name.end());
00091 }
00092 if(occu>100)
00093 {
00094 Name.erase(Name.end()-1,Name.end());
00095 }
00096 if(occu>1000)
00097 {
00098 Name.erase(Name.end()-1,Name.end());
00099 }
00100 if(occu>10000)
00101 {
00102 Name.erase(Name.end()-1,Name.end());
00103 }
00104 char buffer[255];
00105 memset(buffer,0,255);
00106 sprintf(buffer,"%s%d",Name.data(),occu);
00107
00108 return GetAviableName(buffer,Type,occu+1);
00109
00110 UnGuard
00111 }
00112
00113 ExNihilo::ExId ExManagerId::RecordObject(ExCObject &object)
00114 {
00115 Guard(ExNihilo::ExId ExManagerId::RecordObject(ExCObject &object))
00116 try
00117 {
00118 object.SetId(m_ExIdCounter);m_ExIdCounter++;
00119 std::string name(object.GetType());
00120 if(name.find(" *")<name.length())
00121 {
00122 name.erase(name.find(" *"),name.length()-name.find(" *"));
00123 }
00124 object.SetName(GetAviableName(object.GetName(),object.GetType(),0));
00125 object.SetType(name);
00126
00127 ExNihilo::ExObjectInfo Info;
00128 Info.m_Id = object.GetId();
00129 Info.m_Name = object.GetName();
00130 Info.m_Type = object.GetType();
00131
00132 std::pair<ExNihilo::ExId,ExNihilo::ExObjectInfo> obj;
00133 obj.first=object.GetId();
00134 obj.second=Info;
00135 m_MapObjectInfo->insert(obj);
00136
00137 *Consol<<"New Id :"<<Info.m_Id<<" For name:"<<Info.m_Name<<" Type:"<<Info.m_Type<<std::endl;
00138 return Info.m_Id;
00139 }catch(...)
00140 {
00141 throw ExCExpRecordObject();
00142 }
00143 UnGuard
00144 }
00145
00146 ExNihilo::ExId ExManagerId::RecordObject(ExCObject *object)
00147 {
00148 Guard(ExNihilo::ExId ExManagerId::RecordObject(ExCObject *object))
00149 object->SetId(m_ExIdCounter);m_ExIdCounter++;
00150 std::string name(object->GetType());
00151 if(name.find(" *")<name.length())
00152 {
00153 name.erase(name.find(" *"),name.length()-name.find(" *"));
00154 }
00155 object->SetName(GetAviableName(object->GetName(),object->GetType(),0));
00156 object->SetType(name);
00157
00158 ExNihilo::ExObjectInfo Info;
00159 Info.m_Id = object->GetId();
00160 Info.m_Name = object->GetName();
00161 Info.m_Type = object->GetType();
00162
00163 std::pair<ExNihilo::ExId,ExNihilo::ExObjectInfo> obj;
00164 obj.first=object->GetId();
00165 obj.second=Info;
00166 m_MapObjectInfo->insert(obj);
00167
00168 *Consol<<"New Id :"<<Info.m_Id<<" For name:"<<Info.m_Name<<" Type:"<<Info.m_Type<<std::endl;
00169 return Info.m_Id;
00170 UnGuard
00171 }
00172
00173 ExNihilo::ExId ExManagerId::GetObjectId(std::string Name,std::string Type)
00174 {
00175 Guard(ExNihilo::ExId ExManagerId::GetObjectId(std::string Name,std::string Type))
00176 for(m_ItMapObjectInfo=m_MapObjectInfo->begin();m_ItMapObjectInfo!=m_MapObjectInfo->end();m_ItMapObjectInfo++)
00177 {
00178 if(Name==m_ItMapObjectInfo->second.m_Name)
00179 return m_ItMapObjectInfo->second.m_Id;
00180 }
00181 return -1;
00182 UnGuard
00183 }
00184
00185 std::string ExManagerId::GetObjectName(ExNihilo::ExId id)
00186 {
00187 Guard(std::string ExManagerId::GetObjectName(ExNihilo::ExId id))
00188 try
00189 {
00190 return m_MapObjectInfo->find(id)->second.m_Name;
00191 }
00192 catch(...)
00193 {
00194 return "NoName";
00195 }
00196 UnGuard
00197 }
00198
00199 std::string ExManagerId::GetObjectType(ExNihilo::ExId id)
00200 {
00201 Guard(std::string ExManagerId::GetObjectType(ExNihilo::ExId id))
00202 try
00203 {
00204 return m_MapObjectInfo->find(id)->second.m_Type;
00205 }
00206 catch(...)
00207 {
00208 return "NoName";
00209 }
00210 UnGuard
00211 }
00212
00213 void ExManagerId::List(void)
00214 {
00215 Guard(void ExManagerId::List(void))
00216 for(m_ItMapObjectInfo=m_MapObjectInfo->begin();m_ItMapObjectInfo!=m_MapObjectInfo->end();m_ItMapObjectInfo++)
00217 {
00218 *Consol<<"Id :"<<m_ItMapObjectInfo->second.m_Id<<" For name:"<<m_ItMapObjectInfo->second.m_Name<<" Type:"<<m_ItMapObjectInfo->second.m_Type<<std::endl;
00219 }
00220 UnGuard
00221 }
00222
00223 ExCAction ExManagerId::InputAction(ExCAction Action)
00224 {
00225 Guard(ExCAction ExManagerId::InputAction(ExCAction Action))
00226 switch(Action.m_Action)
00227 {
00228 case ID_LIST:
00229 List();
00230 break;
00231 }
00232 if(Action.m_NextAction){return *Action.m_NextAction;}
00233 else{return NOTHING;}
00234 UnGuard
00235 }