| 1 |
// $Id: b_objtype.cpp,v 1.18 2003/03/15 04:35:30 fukasawa Exp $ |
| 2 |
|
| 3 |
//============================================================================= |
| 4 |
/** |
| 5 |
* @file b_objtype.cpp |
| 6 |
* |
| 7 |
* @author Fukasawa Mitsuo |
| 8 |
* |
| 9 |
* |
| 10 |
* Copyright (C) 2001-2004 BEE Co.,Ltd. All rights reserved. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or |
| 13 |
* modify it under the terms of the GNU General Public License |
| 14 |
* as published by the Free Software Foundation; either version 2 |
| 15 |
* of the License, or (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
*/ |
| 26 |
//============================================================================= |
| 27 |
|
| 28 |
#define BEE_BUILD_DLL |
| 29 |
|
| 30 |
#include "b_objtype.h" |
| 31 |
#include "b_object.h" |
| 32 |
#include "b_attribute.h" |
| 33 |
|
| 34 |
//----------------------------------------------------------------------------- |
| 35 |
// Constructor/Destructor |
| 36 |
//----------------------------------------------------------------------------- |
| 37 |
b_objtype::b_objtype(const string& name, b_specification * spec) |
| 38 |
: m_name(name), m_userName(name), m_seed(1) |
| 39 |
{ |
| 40 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::b_objtype"); |
| 41 |
if (spec == NULL) |
| 42 |
{ |
| 43 |
m_spec = ObjSpec::instance(); |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
m_spec = spec; |
| 48 |
} |
| 49 |
m_spec->add(this); |
| 50 |
} |
| 51 |
|
| 52 |
//----------------------------------------------------------------------------- |
| 53 |
b_objtype::~b_objtype() |
| 54 |
{ |
| 55 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::~b_objtype"); |
| 56 |
|
| 57 |
// Release all objects |
| 58 |
b_objectMap::iterator iter = m_objs.begin(); |
| 59 |
for ( ; iter != m_objs.end(); iter++) |
| 60 |
{ |
| 61 |
b_object * obj = (*iter).second; |
| 62 |
delete obj; |
| 63 |
m_spec->remove(obj); |
| 64 |
} |
| 65 |
m_objs.clear(); |
| 66 |
|
| 67 |
// Release all attributes |
| 68 |
for (size_t i = 0; i < m_attrs.size(); i++) |
| 69 |
{ |
| 70 |
b_attribute * attr = m_attrs[i]; |
| 71 |
if (! attr->nodel()) // can't delete static attributes |
| 72 |
{ |
| 73 |
//@ delete attr; |
| 74 |
} |
| 75 |
} |
| 76 |
m_attrs.clear(); |
| 77 |
m_dict.clear(); |
| 78 |
|
| 79 |
m_spec->remove(this); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
//----------------------------------------------------------------------------- |
| 84 |
// Make object idenntifier |
| 85 |
//----------------------------------------------------------------------------- |
| 86 |
int b_objtype::makeObjID(string& retid) |
| 87 |
{ |
| 88 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::makeObjID"); |
| 89 |
|
| 90 |
BCHAR buf[256+2]; |
| 91 |
u_int oid = m_seed++; |
| 92 |
_stprintf(buf, _TX("%s:%u"), m_name.c_str(), oid); |
| 93 |
retid = buf; |
| 94 |
return BEE_SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
//----------------------------------------------------------------------------- |
| 98 |
// Add attribute |
| 99 |
//----------------------------------------------------------------------------- |
| 100 |
bool comp_position(b_attribute * lhs, b_attribute * rhs) |
| 101 |
{ |
| 102 |
return (lhs->position() < rhs->position()); |
| 103 |
} |
| 104 |
|
| 105 |
//----------------------------------------------------------------------------- |
| 106 |
int b_objtype::add(b_attribute * attr) |
| 107 |
{ |
| 108 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::add"); |
| 109 |
|
| 110 |
m_attrs.push_back(attr); |
| 111 |
if (attr->position() == 0) |
| 112 |
{ |
| 113 |
attr->position(m_attrs.size() - 1); // Set position of object |
| 114 |
} |
| 115 |
this->entryAttrName(attr); |
| 116 |
|
| 117 |
sort(m_attrs.begin(), m_attrs.end(), comp_position); |
| 118 |
|
| 119 |
return BEE_SUCCESS; |
| 120 |
} |
| 121 |
|
| 122 |
//----------------------------------------------------------------------------- |
| 123 |
int b_objtype::entryAttrName(b_attribute * attr) |
| 124 |
{ |
| 125 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::entryAttrName"); |
| 126 |
|
| 127 |
m_dict.insert(b_attrPair(attr->name(), attr)); |
| 128 |
b_attribute * mbrAttr; |
| 129 |
if (attr->isStruct() || attr->isVector()) |
| 130 |
{ |
| 131 |
for (size_t i = 0; i < attr->m_attrs.size(); i++) |
| 132 |
{ |
| 133 |
mbrAttr = attr->m_attrs[i]; |
| 134 |
this->entryAttrName(mbrAttr); |
| 135 |
} |
| 136 |
} |
| 137 |
return BEE_SUCCESS; |
| 138 |
} |
| 139 |
|
| 140 |
//----------------------------------------------------------------------------- |
| 141 |
// Find attribute by name |
| 142 |
//----------------------------------------------------------------------------- |
| 143 |
const b_attribute * b_objtype::find(const string& name) const |
| 144 |
{ |
| 145 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::find"); |
| 146 |
|
| 147 |
b_attrMap::const_iterator iter = m_dict.find(name); |
| 148 |
if (iter == m_dict.end()) |
| 149 |
{ |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
const b_attribute * result = (*iter).second; |
| 153 |
return result; |
| 154 |
} |
| 155 |
|
| 156 |
//----------------------------------------------------------------------------- |
| 157 |
const b_attribute * b_objtype::findByUser(const string& name) const |
| 158 |
{ |
| 159 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::findByUser"); |
| 160 |
|
| 161 |
const b_attribute * attr; |
| 162 |
b_attributes::const_iterator at_iter = m_attrs.begin(); |
| 163 |
for ( ; at_iter != m_attrs.end(); at_iter++) |
| 164 |
{ |
| 165 |
attr = *at_iter; |
| 166 |
if (attr->m_userName == name) |
| 167 |
{ // found user's name attribute |
| 168 |
return attr; |
| 169 |
} |
| 170 |
} |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
//----------------------------------------------------------------------------- |
| 175 |
// Create object |
| 176 |
//----------------------------------------------------------------------------- |
| 177 |
b_object * b_objtype::instance(const BCHAR * name) |
| 178 |
{ |
| 179 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::instance"); |
| 180 |
|
| 181 |
BEE_GUARD_RETURN(ACE_Thread_Mutex, ace_mon, this->m_lock, NULL, |
| 182 |
10, 200); |
| 183 |
|
| 184 |
b_object * obj = new b_object(this, name); |
| 185 |
m_objs.insert(b_objectPair(obj->objid(), obj)); |
| 186 |
m_spec->addObject(obj); |
| 187 |
return obj; |
| 188 |
} |
| 189 |
|
| 190 |
//----------------------------------------------------------------------------- |
| 191 |
int b_objtype::entry(b_object * obj) |
| 192 |
{ |
| 193 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::entry"); |
| 194 |
|
| 195 |
BEE_GUARD_RETURN(ACE_Thread_Mutex, ace_mon, this->m_lock, BEE_ERROR, |
| 196 |
10, 200); |
| 197 |
|
| 198 |
m_objs.insert(b_objectPair(obj->objid(), obj)); |
| 199 |
m_spec->addObject(obj); |
| 200 |
return BEE_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
//----------------------------------------------------------------------------- |
| 204 |
b_object * b_objtype::findObject(const string& name) |
| 205 |
{ |
| 206 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::findObject"); |
| 207 |
|
| 208 |
string objid = _TX(""); |
| 209 |
if (name.find(':') == string::npos) |
| 210 |
{ |
| 211 |
objid += this->m_name; |
| 212 |
objid += _TX(":"); |
| 213 |
} |
| 214 |
objid += name; |
| 215 |
|
| 216 |
b_objectMap::const_iterator iter = m_objs.find(objid); |
| 217 |
if (iter == m_objs.end()) |
| 218 |
{ |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
b_object * result = (*iter).second; |
| 222 |
return result; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
//----------------------------------------------------------------------------- |
| 227 |
// Remove object |
| 228 |
//----------------------------------------------------------------------------- |
| 229 |
int b_objtype::remove(b_object * obj) |
| 230 |
{ |
| 231 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::remove"); |
| 232 |
|
| 233 |
BEE_GUARD_RETURN(ACE_Thread_Mutex, ace_mon, this->m_lock, BEE_ERROR, |
| 234 |
10, 300); |
| 235 |
|
| 236 |
m_spec->remove(obj); |
| 237 |
size_t q = m_objs.erase(obj->objid()); |
| 238 |
if (q != 1) |
| 239 |
{ |
| 240 |
return BEE_ERROR; |
| 241 |
} |
| 242 |
return BEE_SUCCESS; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
//----------------------------------------------------------------------------- |
| 247 |
// Get instances |
| 248 |
//----------------------------------------------------------------------------- |
| 249 |
int b_objtype::allObject(b_objects& objects) |
| 250 |
{ |
| 251 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::allObject"); |
| 252 |
|
| 253 |
BEE_GUARD_RETURN(ACE_Thread_Mutex, ace_mon, this->m_lock, BEE_ERROR, |
| 254 |
2, 400); |
| 255 |
|
| 256 |
objects.clear(); |
| 257 |
b_objectMap::iterator iter = m_objs.begin(); |
| 258 |
for ( ; iter != m_objs.end(); iter++) |
| 259 |
{ |
| 260 |
b_object * obj = (*iter).second; |
| 261 |
objects.push_back(obj); |
| 262 |
} |
| 263 |
return objects.size(); |
| 264 |
} |
| 265 |
|
| 266 |
//----------------------------------------------------------------------------- |
| 267 |
// Map name standard's and user's |
| 268 |
//----------------------------------------------------------------------------- |
| 269 |
int b_objtype::mapping(TypeNameMapping& mapdata) |
| 270 |
{ |
| 271 |
TRACE_FUNCTION(TRL_LOW, "b_objtype::mapping"); |
| 272 |
|
| 273 |
m_userName = mapdata.m_typeName; |
| 274 |
NameMap::iterator iter = mapdata.m_mapping.begin(); |
| 275 |
for ( ; iter != mapdata.m_mapping.end(); iter++) |
| 276 |
{ |
| 277 |
const string& hosts = (*iter).first; |
| 278 |
const string& users = (*iter).second; |
| 279 |
b_attribute * attr = const_cast<b_attribute *>(this->find(hosts)); |
| 280 |
if (attr == NULL) |
| 281 |
{ |
| 282 |
TRACE_DEBUG((_TX("Undefined addtibute \"%s\"\n"), hosts.c_str())); |
| 283 |
continue; // Ignore undefined name |
| 284 |
} |
| 285 |
attr->userName(users); |
| 286 |
} |
| 287 |
|
| 288 |
ObjSpec::instance()->entryUserName(m_name, m_userName); |
| 289 |
|
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
//----------------------------------------------------------------------------- |
| 294 |
// Dump |
| 295 |
//----------------------------------------------------------------------------- |
| 296 |
void b_objtype::dump() const |
| 297 |
{ |
| 298 |
ACE_DEBUG((LM_DEBUG, ACE_TEXT("\n*** Class: %s(%s) ***\n"), |
| 299 |
m_name.c_str(), m_userName.c_str())); |
| 300 |
ACE_DEBUG((LM_DEBUG, ACE_TEXT(" current seed-num is %d.\n"), |
| 301 |
m_seed.value())); |
| 302 |
ACE_DEBUG((LM_DEBUG, ACE_TEXT(" [ attributes ]\n"))); |
| 303 |
b_attributes::const_iterator at_iter = m_attrs.begin(); |
| 304 |
for ( ; at_iter != m_attrs.end(); at_iter++) |
| 305 |
{ |
| 306 |
const b_attribute * attr = *at_iter; |
| 307 |
attr->dump(); |
| 308 |
} |
| 309 |
|
| 310 |
ACE_DEBUG((LM_DEBUG, ACE_TEXT(" [ objects ]\n"))); |
| 311 |
b_objectMap::const_iterator obj_iter = m_objs.begin(); |
| 312 |
for ( ; obj_iter != m_objs.end(); obj_iter++) |
| 313 |
{ |
| 314 |
const b_object * obj = (*obj_iter).second; |
| 315 |
ACE_DEBUG((LM_DEBUG, ACE_TEXT(" %s\n"), obj->getObjID())); |
| 316 |
} |
| 317 |
} |
| 318 |
|