1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Framework *
* *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza, *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/core/objectmodel/BaseObject.h>
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/objectmodel/Event.h>
#include <sofa/core/objectmodel/KeypressedEvent.h>
#include <sofa/core/componentmodel/topology/Topology.h>
#include <sofa/helper/TagFactory.h>
#include <iostream>
using std::cerr;
using std::endl;
namespace sofa
{
namespace core
{
namespace objectmodel
{
BaseObject::BaseObject()
: Base()
, f_listening(initData( &f_listening, false, "listening", "if true, handle the events, otherwise ignore the events"))
, f_tags(initData( &f_tags, "tags", "list of the subsets the objet belongs to"))
, context_(NULL)
/* , m_isListening(false)
, m_printLog(false)*/
{
}
BaseObject::~BaseObject()
{}
void BaseObject::parse( BaseObjectDescription* arg )
{
std::vector< std::string > attributeList;
arg->getAttributeList(attributeList);
for (unsigned int i=0;i<attributeList.size();++i)
{
std::vector< BaseData* > dataModif = findGlobalField(attributeList[i]);
for (unsigned int d=0;d<dataModif.size();++d)
{
const char* val = arg->getAttribute(attributeList[i]);
if (val)
{
std::string valueString(val);
/* test if data is a link */
if (valueString[0] == '@')
{
std::size_t posPath = valueString.rfind('/');
if (posPath == std::string::npos) posPath = 0;
std::size_t posDot = valueString.rfind('.');
std::string objectName;
std::string dataName;
BaseObject* obj = NULL;
/* if '.' not found, try to find the data in the current object */
if (posPath == 0 && posDot == std::string::npos)
{
obj = this;
objectName = this->getName();
dataName = valueString;
}
else
{
if (posDot == std::string::npos) // no data specified, look for one with the same name
{
objectName = valueString;
dataName = attributeList[i];
}
else
{
objectName = valueString.substr(1,posDot-1);
dataName = valueString.substr(posDot+1);
}
if (objectName[0] == '[')
{
if (objectName[objectName.size()-1] != ']')
{
serr<<"ERROR: Missing ']' in at the end of "<< objectName << sendl;
break;
}
objectName = objectName.substr(1, objectName.size()-2);
if (objectName.empty())
{
serr<<"ERROR: Missing object level between [] in : " << val << sendl;
break;
}
int objectLevel = atoi(objectName.c_str());
helper::vector<BaseObject*> objects;
getContext()->get<BaseObject>(&objects, BaseContext::Local);
helper::vector<BaseObject*>::iterator it;
for(it = objects.begin(); it != objects.end(); ++it)
{
if ((*it) == this)
{
it += objectLevel;
if ((*it) != NULL)
{
obj = (*it);
}
break;
}
}
}
else
{
obj = getContext()->get<BaseObject>(objectName);
}
if (obj == NULL)
{
serr<<"could not find object for option "<< attributeList[i] <<": " << objectName << sendl;
break;
}
}
BaseData* parentData = obj->findField(dataName);
if (parentData == NULL)
{
serr<<"could not read value for option "<< attributeList[i] <<": " << val << sendl;
break;
}
/* set parent value to the child */
if (!dataModif[d]->setParentValue(parentData))
{
serr<<"could not copy value from parent Data "<< valueString << ". Incompatible Data types" << sendl;
break;
}
parentData->addOutput(dataModif[d]);
/* children Data can be modified changing the parent Data value */
dataModif[d]->setReadOnly(true);
break;
}
if( !(dataModif[d]->read( valueString ))) serr<<"could not read value for option "<< attributeList[i] <<": " << val << sendl;
}
}
}
}
void BaseObject::setContext(BaseContext* n)
{
context_ = n;
}
const BaseContext* BaseObject::getContext() const
{
//return (context_==NULL)?BaseContext::getDefault():context_;
return context_;
}
BaseContext* BaseObject::getContext()
{
return (context_==NULL)?BaseContext::getDefault():context_;
//return context_;
}
void BaseObject::init()
{
}
void BaseObject::bwdInit()
{
}
/// Update method called when variables used in precomputation are modified.
void BaseObject::reinit()
{
//sout<<"WARNING: the reinit method of the object "<<this->getName()<<" does nothing."<<sendl;
}
/// Save the initial state for later uses in reset()
void BaseObject::storeResetState()
{ }
/// Reset to initial state
void BaseObject::reset()
{ }
void BaseObject::writeState( std::ostream& )
{ }
/// Called just before deleting this object
/// Any object in the tree bellow this object that are to be removed will be removed only after this call,
/// so any references this object holds should still be valid.
void BaseObject::cleanup()
{ }
/// Handle an event
void BaseObject::handleEvent( Event* /*e*/ )
{
/*
serr<<"BaseObject "<<getName()<<" ("<<getTypeName()<<") gets an event"<<sendl;
if( KeypressedEvent* ke = dynamic_cast<KeypressedEvent*>( e ) )
{
serr<<"BaseObject "<<getName()<<" gets a key event: "<<ke->getKey()<<sendl;
}
*/
}
/// Handle topological Changes from a given Topology
void BaseObject::handleTopologyChange(core::componentmodel::topology::Topology* t)
{
if (t == this->getContext()->getTopology())
{
// sout << getClassName() << " " << getName() << " processing topology changes from " << t->getName() << sendl;
handleTopologyChange();
}
}
/// Handle state Changes from a given Topology
void BaseObject::handleStateChange(core::componentmodel::topology::Topology* t)
{
if (t == this->getContext()->getTopology())
handleStateChange();
}
// void BaseObject::setListening( bool b )
// {
// m_isListening = b;
// }
//
// bool BaseObject::isListening() const
// {
// return m_isListening;
// }
//
// BaseObject* BaseObject::setPrintLog( bool b )
// {
// m_printLog = b;
// return this;
// }
//
// bool BaseObject::printLog() const
// {
// return m_printLog;
// }
double BaseObject::getTime() const
{
return getContext()->getTime();
}
bool BaseObject::hasTag(Tag t) const
{
return (f_tags.getValue().count( t ) > 0 );
}
void BaseObject::addTag(Tag t)
{
f_tags.beginEdit()->insert(t);
f_tags.endEdit();
}
void BaseObject::removeTag(Tag t)
{
f_tags.beginEdit()->erase(t);
f_tags.endEdit();
}
} // namespace objectmodel
} // namespace core
} // namespace sofa
|