PyObject* LoadIterator::iternext() { PyObject* result; switch (mode) { case 1: // Iterate over loads on a resource if (ir == res->getLoads().end()) return NULL; result = const_cast<Load*>(&*ir); ++ir; break; case 2: // Iterate over loads on an operation if (io == oper->getLoads().end()) return NULL; result = const_cast<Load*>(&*io); ++io; break; case 3: // Iterate over all loads while (io == oper->getLoads().end()) { if (++ioo == Operation::end()) return NULL; oper = &*ioo; io = Operation::loadlist::const_iterator(oper->getLoads().begin()); } result = const_cast<Load*>(&*io); ++io; break; default: throw LogicException("Unknown iterator mode"); } Py_INCREF(result); return result; }
Problem* Problem::List::push(const MetaClass* m, const Object* o, Date st, Date nd, double w) { // Find the end of the list Problem* cur = first; while (cur && cur->nextProblem && cur->getOwner() != o) cur = cur->nextProblem; if (cur && cur->getOwner() == o) // Duplicate problem: stop here. return cur; // Create a new problem Problem *p; if (m == ProblemCapacityOverload::metadata) p = new ProblemCapacityOverload(const_cast<Resource*>(dynamic_cast<const Resource*>(o)), st, nd, w, false); else if (m == ProblemMaterialShortage::metadata) p = new ProblemMaterialShortage(const_cast<Buffer*>(dynamic_cast<const Buffer*>(o)), st, nd, w, false); else if (m == ProblemBeforeCurrent::metadata) p = new ProblemBeforeCurrent(const_cast<Operation*>(dynamic_cast<const Operation*>(o)), st, nd, w); else if (m == ProblemBeforeFence::metadata) p = new ProblemBeforeFence(const_cast<Operation*>(dynamic_cast<const Operation*>(o)), st, nd, w); else throw LogicException("Problem factory can't create this type of problem"); // Link the problem in the list if (cur) cur->nextProblem = p; else first = p; return p; }
DECLARE_EXPORT ItemDistribution::~ItemDistribution() { // Delete the association from the related objects if (getOrigin()) getOrigin()->origins.erase(this); if (getDestination()) getDestination()->destinations.erase(this); // Delete all owned distribution operations while (firstOperation) delete firstOperation; // Unlink from previous item if (it) { if (it->firstItemDistribution == this) // Remove from head it->firstItemDistribution = next; else { // Remove from middle ItemDistribution *j = it->firstItemDistribution; while (j->next && j->next != this) j = j->next; if (j) j->next = next; else throw LogicException("Corrupted ItemDistribution list"); } } // Trigger level and cluster recomputation HasLevel::triggerLazyRecomputation(); }
void Problem::removeProblem() { // Fast delete method: the code triggering this method is responsible of // maintaining the problem container if (!owner) return; if (owner->firstProblem == this) // Removal from the head of the list owner->firstProblem = nextProblem; else { // Removal from the middle of the list Problem *prev = owner->firstProblem; for (Problem* cur = owner->firstProblem; cur; cur=cur->nextProblem) { if (cur == this) { // Found it! prev->nextProblem = nextProblem; return; } prev = cur; } // The problem wasn't found in the list. This shouldn't happen... throw LogicException("Corrupted problem list"); } }
void MetaClass::addClass (const string& a, const string& b, bool def, creatorDefault f) { // Find or create the category MetaCategory* cat = const_cast<MetaCategory*>(MetaCategory::findCategoryByTag(a.c_str())); // Check for a valid category if (!cat) throw LogicException("Category " + a + " not found when registering class " + b); // Update fields type = b.empty() ? "unspecified" : b; typetag = &Keyword::find(type.c_str()); category = cat; // Update the metadata table cat->classes[Keyword::hash(b)] = this; // Register this tag also as the default one, if requested if (isDefault) cat->classes[Keyword::hash("default")] = this; // Set method pointers to nullptr factoryMethod = f; }
DECLARE_EXPORT Buffer* OperationItemDistribution::getDestination() const { for (flowlist::const_iterator i = getFlows().begin(); i != getFlows().end(); ++i) if (i->getQuantity() > 0.0) return i->getBuffer(); throw LogicException("Transfer operation doesn't produce material"); }
/** * Create one way output out of room. * @throws LogicException when output_DIR is not known */ Cube * ModelFactory::createOutputItem(const std::string &kind, const V2 &loc, const std::string &shape) { Dir::eDir outDir = Dir::DIR_NO; if ("output_left" == kind) { outDir = Dir::DIR_LEFT; } else if ("output_right" == kind) { outDir = Dir::DIR_RIGHT; } else if ("output_up" == kind) { outDir = Dir::DIR_UP; } else if ("output_down" == kind) { outDir = Dir::DIR_DOWN; } else { throw LogicException(ExInfo("unknown border dir") .addInfo("kind", kind)); } Cube *model = new Cube(loc, Cube::FIXED, Cube::NONE, false, new Shape(shape)); model->setOutDir(outDir); return model; }
DECLARE_EXPORT void Demand::removeDelivery(OperationPlan * o) { // Valid opplan check if (!o) return; // See if the demand field on the operationplan points to this demand if (o->dmd != this) throw LogicException("Delivery operationplan incorrectly registered"); // Remove the reference on the operationplan o->dmd = NULL; // Required to avoid endless loop o->setDemand(NULL); // Find in the list of deliveries OperationPlanList::iterator j = deli.begin(); while (j!=deli.end() && *j!=o) ++j; // Check that the operation is found // It is possible it is not found! This happens if e.g. an operationplan // is created but destroyed again before it is initialized. if (j!=deli.end()) { // Remove from the list deli.erase(j); // Mark the demand as being changed, so the problems can be redetected setChanged(); } }
OperationItemSupplier::OperationItemSupplier( ItemSupplier* i, Buffer *b ) : supitem(i) { if (!i || !b || !i->getSupplier()) throw LogicException( "An OperationItemSupplier always needs to point to " "a itemsupplier and a buffer" ); stringstream o; o << "Purchase " << b->getName() << " from " << i->getSupplier()->getName(); setName(o.str()); setDuration(i->getLeadTime()); setSizeMultiple(i->getSizeMultiple()); setSizeMinimum(i->getSizeMinimum()); setLocation(b->getLocation()); setSource(i->getSource()); setCost(i->getCost()); setFence(i->getFence()); setHidden(true); new FlowEnd(this, b, 1); initType(metadata); // Optionally, create a load if (i->getResource()) new LoadDefault(this, i->getResource(), i->getResourceQuantity()); // Insert in the list of ItemSupplier operations. // The list is not sorted (for performance reasons). nextOperation = i->firstOperation; i->firstOperation = this; }
Buffer* OperationItemDistribution::getOrigin() const { for (flowlist::const_iterator i = getFlows().begin(); i != getFlows().end(); ++i) if (i->getQuantity() < 0.0) return i->getBuffer(); throw LogicException("Transfer operation doesn't consume material"); }
OperationItemDistribution::OperationItemDistribution( ItemDistribution* i, Buffer *src, Buffer* dest ) : itemdist(i) { if (!i || !src || !dest) throw LogicException( "An OperationItemDistribution always needs to point to " "a ItemDistribution, a source buffer and a destination buffer" ); stringstream o; o << "Ship " << dest->getItem()->getName() << " from " << src->getName() << " to " << dest->getName(); setName(o.str()); setDuration(i->getLeadTime()); setSizeMultiple(i->getSizeMultiple()); setSizeMinimum(i->getSizeMinimum()); setLocation(dest->getLocation()); setSource(i->getSource()); setCost(i->getCost()); setFence(i->getFence()); setHidden(true); new FlowEnd(this, dest, 1); new FlowStart(this, src, -1); initType(metadata); // Optionally, create a load if (i->getResource()) new LoadDefault(this, i->getResource(), i->getResourceQuantity()); // Insert in the list of ItemDistribution operations. // The list is not sorted (for performance reasons). nextOperation = i->firstOperation; i->firstOperation = this; }
DECLARE_EXPORT void ItemDistribution::setItem(Item* i) { // Unlink from previous item if (it) { if (it->firstItemDistribution == this) // Remove from head it->firstItemDistribution = next; else { // Remove from middle ItemDistribution *j = it->firstItemDistribution; while (j->next && j->next != this) j = j->next; if (j) j->next = next; else throw LogicException("Corrupted ItemDistribution list"); } } // Update item it = i; // Link at the new owner. // We insert ourself at the head of the list. if (it) { next = it->firstItemDistribution; it->firstItemDistribution = this; } // Trigger level and cluster recomputation HasLevel::triggerLazyRecomputation(); }
DECLARE_EXPORT void Load::setAlternateName(string n) { if (!getOperation()) throw LogicException("Can't set an alternate load before setting the operation"); Load *x = getOperation()->loaddata.find(n); if (!x) throw DataException("Can't find load with name '" + n + "'"); setAlternate(x); }
void Renderer::removeProcessor(shared_ptr<ProcessorBase> val) { auto it = find(cbegin(processors), cend(processors), val); if(it == cend(processors)) throw LogicException(EXCEPTION_INFO, "no such processor"); val->stop(); processors.erase(it); }
Object* MetaCategory::ControllerDefault (const MetaClass* cat, const AttributeList& in) { Action act = ADD; switch (act) { case REMOVE: throw DataException ("Entity " + cat->type + " doesn't support REMOVE action"); case CHANGE: throw DataException ("Entity " + cat->type + " doesn't support CHANGE action"); default: /* Lookup for the class in the map of registered classes. */ const MetaClass* j; if (cat->category) // Class metadata passed: we already know what type to create j = cat; else { // Category metadata passed: we need to look up the type const DataElement* type = in.get(Tags::tag_type); j = static_cast<const MetaCategory&>(*cat).findClass(*type ? Keyword::hash(type->getString()) : MetaCategory::defaultHash); if (!j) { string t(*type ? type->getString() : "default"); throw LogicException("No type " + t + " registered for category " + cat->type); } } // Call the factory method Object* result = j->factoryMethodDefault(); // Run the callback methods if (!result->getType().raiseEvent(result, SIG_ADD)) { // Creation denied delete result; throw DataException("Can't create object"); } // Creation accepted return result; } throw LogicException("Unreachable code reached"); return NULL; }
void Renderer::addProcessor(shared_ptr<ProcessorBase> val) { auto it = find(cbegin(processors), cend(processors), val); if(it != cend(processors)) throw LogicException(EXCEPTION_INFO, "has such processor"); val->start(graphics); processors.push_back(val); }
DECLARE_EXPORT void Flow::setAlternateName(const string& n) { if (!getOperation()) throw LogicException("Can't set an alternate flow before setting the operation"); Flow *x = getOperation()->flowdata.find(n); if (!x) throw DataException("Can't find flow with name '" + n + "'"); setAlternate(x); }
/** * Reactivate state after pause. * @throws LogicException when state is already active */ void GameState::resumeState() { if (m_active) { throw LogicException(ExInfo("resume - state is already active") .addInfo("name", getName())); } m_active = true; own_resumeState(); }
/** * @throws LogicException when state is not active */ void GameState::updateState() { if (!m_active) { throw LogicException(ExInfo("update - state is not active") .addInfo("name", getName())); } own_updateState(); }
/** * Read dots postions and level descriptions. * @throws LogicException when cannot parse data file */ void WorldMap::initMap(const Path &mapfile) { WorldBranch parser(NULL); m_startNode = parser.parseMap(mapfile, &m_ending, m_descPack); if (NULL == m_startNode) { throw LogicException(ExInfo("cannot create world map") .addInfo("file", mapfile.getNative())); } }
/** * @throws LogicException when agent is not initialized. */ void BaseAgent::update() { if (!m_initialized) { throw LogicException(ExInfo("agent is not ready") .addInfo("name", getName())); } own_update(); }
DECLARE_EXPORT void ResourceSkill::endElement (XMLInput& pIn, const Attribute& pAttr, const DataElement& pElement) { if (pAttr.isA (Tags::tag_resource)) { Resource *r = dynamic_cast<Resource*>(pIn.getPreviousObject()); if (r) setResource(r); else throw LogicException("Incorrect object type during read operation"); } else if (pAttr.isA (Tags::tag_skill)) { Skill *s = dynamic_cast<Skill*>(pIn.getPreviousObject()); if (s) setSkill(s); else throw LogicException("Incorrect object type during read operation"); } else if (pAttr.isA(Tags::tag_priority)) setPriority(pElement.getInt()); else if (pAttr.isA(Tags::tag_effective_end)) setEffectiveEnd(pElement.getDate()); else if (pAttr.isA(Tags::tag_effective_start)) setEffectiveStart(pElement.getDate()); else if (pAttr.isA(Tags::tag_action)) { delete static_cast<Action*>(pIn.getUserArea()); pIn.setUserArea( new Action(MetaClass::decodeAction(pElement.getString().c_str())) ); } else if (pIn.isObjectEnd()) { // The resourceskill data is now all read in. See if it makes sense now... Action a = pIn.getUserArea() ? *static_cast<Action*>(pIn.getUserArea()) : ADD_CHANGE; delete static_cast<Action*>(pIn.getUserArea()); try { validate(a); } catch (...) { delete this; throw; } } }
/** * @throws LogicException when state is not active */ void GameState::pauseState() { if (!m_active) { throw LogicException(ExInfo("pause - state is not active") .addInfo("name", getName())); } own_pauseState(); m_active = false; m_onBg = false; }
HasProblems::EntityIterator& HasProblems::EntityIterator::operator++() { switch (type) { case 0: // Buffer if (*bufIter != Buffer::end()) if (++(*bufIter) != Buffer::end()) return *this; ++type; delete bufIter; resIter = new Resource::iterator(Resource::begin()); if (*resIter != Resource::end()) return *this; // Note: no break statement case 1: // Resource if (*resIter != Resource::end()) if (++(*resIter) != Resource::end()) return *this; ++type; delete resIter; operIter = new OperationPlan::iterator(OperationPlan::begin()); if (*operIter != OperationPlan::end()) return *this; // Note: no break statement case 2: // Operationplan if (*operIter != OperationPlan::end()) if (++(*operIter) != OperationPlan::end()) return *this; ++type; delete operIter; demIter = new Demand::iterator(Demand::begin()); if (*demIter != Demand::end()) return *this; // Note: no break statement case 3: // Demand if (*demIter != Demand::end()) if (++(*demIter) != Demand::end()) return *this; ++type; delete demIter; opIter = new Operation::iterator(Operation::begin()); if (*opIter != Operation::end()) return *this; // Note: no break statement case 4: // Operation if (*opIter != Operation::end()) if (++(*opIter) != Operation::end()) return *this; // Ended recursing of all entities ++type; delete opIter; opIter = nullptr; return *this; } throw LogicException("Unreachable code reached"); }
DECLARE_EXPORT PeggingIterator& PeggingIterator::operator++() { // Validate if (states.empty()) throw LogicException("Incrementing the iterator beyond it's end"); if (!downstream) throw LogicException("Incrementing an upstream iterator"); // Mark the top entry in the stack as invalid, so it can be reused. first = true; // Find other operationplans to add to the stack state t = states.back(); // Copy the top element followPegging(t.opplan, t.quantity, t.offset, t.level); // Pop invalid top entry from the stack. // This will happen if we didn't find an operationplan to replace the // top entry. if (first) states.pop_back(); return *this; }
void Pit::fill_area(Physical& physical) { RowCol rc = physical.rc(); for(int r = rc.r; r < rc.r + physical.rows(); r++) { for(int c = rc.c; c < rc.c + physical.columns(); c++) { RowCol target{r, c}; auto result = m_content_map.emplace(std::make_pair(target, &physical)); if(!result.second) throw LogicException("Pit: Attempt to block already blocked space."); } } }
void Pit::fall_block(Block& block) { RowCol rc = block.rc(); RowCol to { rc.r+1, rc.c }; if(at(to)) throw LogicException("Pit: Attempt to move block to occupied location."); auto erased = m_content_map.erase(rc); assert(1 == erased); // sanity check: this space must have been previously occupied auto emplace_result = m_content_map.emplace(to, &block); assert(emplace_result.second); // sanity check: this space must be free to place a block in block.set_rc(to); }
DECLARE_EXPORT OperationItemSupplier::OperationItemSupplier( ItemSupplier* i, Buffer *b ) : supitem(i) { if (!i || !b || !i->getSupplier()) throw LogicException( "An OperationItemSupplier always needs to point to " "a itemsupplier and a buffer" ); stringstream o; o << "Purchase " << b->getName() << " from " << i->getSupplier()->getName(); setName(o.str()); setDuration(i->getLeadTime()); setSizeMultiple(i->getSizeMultiple()); setSizeMinimum(i->getSizeMinimum()); setLocation(b->getLocation()); setSource(i->getSource()); setCost(i->getCost()); setFence(i->getFence()); setHidden(true); new FlowEnd(this, b, 1); initType(metadata); // Optionally, create a load if (i->getResource()) new LoadDefault(this, i->getResource(), i->getResourceQuantity()); // Insert in the list of ItemSupplier operations. // We keep the list sorted by the operation name. if (!i->firstOperation || getName() < i->firstOperation->getName()) { // New head of the list nextOperation = i->firstOperation; i->firstOperation = this; } else { // Insert in the middle or at the tail OperationItemSupplier* o = i->firstOperation; while (o->nextOperation) { if (b->getName() < o->nextOperation->getName()) break; o = o->nextOperation; } nextOperation = o->nextOperation; o->nextOperation = this; } }
DECLARE_EXPORT HasProblems* HasProblems::EntityIterator::operator->() const { switch (type) { // Buffer case 0: return &**bufIter; // Resource case 1: return &**resIter; // Operationplan case 2: return &**operIter; // Demand case 3: return &**demIter; default: throw LogicException("Unreachable code reached"); } }
/** * Define controls symbols for extra fish. * Format: "fish_extra-UDLR" * @throws LogicException when symbols are not specified */ ControlSym ModelFactory::parseExtraControlSym(const std::string &kind) { static const std::string PREFIX = "fish_extra-"; if (kind.size() != PREFIX.size() + 4) { throw LogicException(ExInfo("you must specify control symbols") .addInfo("kind", kind)); } char up = kind[PREFIX.size()]; char down = kind[PREFIX.size() + 1]; char left = kind[PREFIX.size() + 2]; char right = kind[PREFIX.size() + 3]; return ControlSym(up, down, left, right); }