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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Eris/TypeInfo.h>
#include <Eris/Log.h>
#include <Eris/Exceptions.h>
#include <Atlas/Objects/Root.h>
#include <cassert>
using namespace Atlas;
namespace Eris {
////////////////////////////////////////////////////////////////////////////////////////////////////////
TypeInfo::TypeInfo(const std::string &id, TypeService *engine) :
_bound(false),
_name(id),
_typeid(INVALID_TYPEID),
_engine(engine)
{
if (_name == "root") {
_bound = true;
// _typeid = 0; ?
}
}
TypeInfo::TypeInfo(const Atlas::Objects::Root &atype, TypeService *engine) :
_bound(false),
_name(atype.getId()),
_typeid(INVALID_TYPEID),
_engine(engine)
{
if (_name == "root")
_bound = true;
processTypeData(atype);
}
bool TypeInfo::isA(TypeInfoPtr tp)
{
// preliminary stuff
if (safeIsA(tp)) return true;
if (!isBound()) {
// can't give a reply, need to get those parents bound. we assume it's in
// progress. Need to pick a sensible repost condition here
//Signal & sig = signalWhenBound(this);
Eris::log(LOG_DEBUG, "throwing OperationBlocked doing isA on %s", _name.c_str());
throw OperationBlocked(getBoundSignal());
}
return false;
}
bool TypeInfo::safeIsA(TypeInfoPtr tp)
{
assert(tp);
if (tp == this) // uber fast short-circuit for type equality
return true;
return _ancestors.count(tp); // non-authorative
}
void TypeInfo::processTypeData(const Atlas::Objects::Root &atype)
{
std::string id = atype.getId();
if (id != _name)
throw InvalidOperation("Mis-targeted INFO operation (for " + id + ')');
if (atype.hasAttr("IntegerType")) {
_typeid = atype.getAttr("IntegerType").asInt();
}
Atlas::Message::Element::ListType parents = atype.getParents();
for (unsigned int I=0; I<parents.size(); ++I) {
addParent(_engine->getTypeByName(parents[I].asString()));
}
// expand the children ? why not ..
if (atype.hasAttr("children")) {
assert(atype.getAttr("children").isList());
Message::Element::ListType children = atype.getAttr("children").asList();
for (Atlas::Message::Element::ListType::iterator I=children.begin(); I!=children.end(); ++I) {
assert(I->isString());
addChild(_engine->getTypeByName(I->asString()));
}
}
setupDepends();
validateBind();
}
bool TypeInfo::operator==(const TypeInfo &x) const
{
if (_typeid == INVALID_TYPEID)
return _name == x._name;
else
return _typeid == x._typeid;
}
bool TypeInfo::operator<(const TypeInfo &x) const
{
if (_typeid == INVALID_TYPEID)
return _name == x._name;
else
return _typeid == x._typeid;
}
void TypeInfo::addParent(TypeInfoPtr tp)
{
// do lots of sanity checking, since a corrupt type heirarchy would really screw us over
assert(tp);
if (_parents.count(tp)) {
// it's critcial we bail fast here to avoid infitite mutual recursion with addChild
return;
}
if (_ancestors.count(tp)) {
Eris::log(LOG_WARNING, "Adding %s as parent of %s, but already marked as ancestor",
tp->_name.c_str(), _name.c_str());
throw InvalidOperation("Bad inheritance graph : new parent is ancestor");
}
// update the gear
_parents.insert(tp);
addAncestor(tp);
// note this will never recurse deep becuase of the fast exiting up top
tp->addChild(this);
}
void TypeInfo::addChild(TypeInfoPtr tp)
{
assert(tp);
if (_children.count(tp)) {
return; // same need for early bail-out here
}
_children.insert(tp);
// again this will not recurse due to the termination code
tp->addParent(this);
}
void TypeInfo::addAncestor(TypeInfoPtr tp)
{
assert(tp);
_ancestors.insert(tp);
TypeInfoSet& parentAncestors = tp->_ancestors;
_ancestors.insert(parentAncestors.begin(), parentAncestors.end());
// tell all our childen!
for (TypeInfoSet::iterator C=_children.begin(); C!=_children.end();++C)
(*C)->addAncestor(tp);
}
bool TypeInfo::isBound()
{
return _bound;
}
void TypeInfo::validateBind()
{
if (_bound) return;
// check all our parents
for (TypeInfoSet::iterator P=_parents.begin(); P!=_parents.end();++P)
if (!(*P)->isBound()) return;
Eris::log(LOG_VERBOSE, "Bound type %s", _name.c_str());
_bound = true;
Bound.emit();
// emit the global signal too
_engine->BoundType.emit(this);
TypeInfoSet dependants(_engine->extractDependantsForType(this));
if (dependants.empty())
return;
/* okay, we have a bunch of types that we wdaiting on this on to be bound,
let's try and validate them too. Once we're done, we can remove the dependancy list
for this type. */
for (TypeInfoSet::iterator D=dependants.begin(); D!=dependants.end(); ++D) {
(*D)->validateBind();
}
}
Signal& TypeInfo::getBoundSignal()
{
if (isBound())
throw InvalidOperation("Type node is already bound, what are you playing at?");
Eris::log(LOG_DEBUG, "in TypeInfo::getBoundSignal() for %s", _name.c_str());
setupDepends();
return Bound;
}
/** Compute the dependancies of this type. Basically we add ourselves to the dependant set
of every currenly unbound parent. We will have validateBind() called that many times, and
on the final call (whichever parent is bound last), we ouirselves will become bound. */
void TypeInfo::setupDepends()
{
for (TypeInfoSet::iterator P=_parents.begin(); P!=_parents.end();++P) {
if ((*P)->isBound())
continue;
_engine->markTypeDependantOnType(this, *P);
}
}
const std::string& TypeInfo::getName() const
{
return _name;
}
StringSet TypeInfo::getParentsAsSet()
{
StringSet ret;
for (TypeInfoSet::iterator P=_parents.begin(); P!=_parents.end();++P) {
ret.insert((*P)->getName());
}
return ret;
}
} // of namespace Eris
|