Example #1
0
 Warehourse(std::ifstream& fin, std::string name = "untitled"):_name(name) /*throw*/ {
     while (!fin.eof()) {
         replenish(Commodity(fin)); /*throw*/
     }
     fin.close();
     
     //Note: There a no need to rethrow the exception
     //case if a exception is throwed. the construction is unfinished. so the destructor won't get called
     //thus the old information won't  get covered
 }
Example #2
0
	/// Reads space from the buffer
	void readSpace() {
		while (true) {
			for (; currentPtr!=endPtr; ++currentPtr) {
				if (*currentPtr!=' '&&*currentPtr!='\n') {
					goto done;
				}
			}
			if (!replenish()) 
				break;
		}
		done:
		return;
	}
Example #3
0
	T readNumber() {
		T number=0;
		while (true) {
			for (; currentPtr!=endPtr; ++currentPtr) {
				if (*currentPtr>='0'&&*currentPtr<='9') {
					number = (number*10) + (*currentPtr - '0');
				} else {
					goto done;
				}
			}
			if (!replenish()) break;
		}
		done:
		return number;
	}
Example #4
0
void
location_production()
{
  int where;
  int i, enclosed;
  int terr, encl_terr;
  int has_city;
  float pop_grow = 0.0;
  int pop_limit = 200, pop, dpop = 0;

  loop_loc(where) {
    terr = subkind(where);
    has_city = 0;

    for (i = 0; terr_prod[i].terr; i++)
      if (terr_prod[i].terr == terr) {
        replenish(where, terr_prod[i].item, terr_prod[i].qty,
                  terr_prod[i].max);
      }

    /*
     *  Mon Sep 16 11:42:22 1996 -- Scott Turner
     *
     *  Now check for production from enclosed locations...
     *
     */
    loop_here(where, enclosed) {
      encl_terr = subkind(enclosed);
      if (encl_terr == sub_city)
        has_city = 1;
      for (i = 0; terr_prod2[i].terr; i++)
        if (terr_prod2[i].terr == encl_terr) {
          replenish(where, terr_prod2[i].item, terr_prod2[i].qty,
                    terr_prod2[i].max);
        };
    }
    next_here;

    /*
     *  First limit poppy fields to normal production level.
     *  Then double opium if poppy field was specially tended.
     */
    if (terr == sub_poppy_field) {
      int n;

      n = has_item(where, item_opium);
      if (n > POPPY_OPIUM)
        consume_item(where, item_opium, n - POPPY_OPIUM);

      if (rp_misc(where) && rp_misc(where)->opium_double) {
        rp_misc(where)->opium_double = FALSE;
        gen_item(where, item_opium, has_item(where, item_opium));
      }
    }

    if (terr == sub_island ||
        (loc_depth(where) == LOC_province && has_ocean_access(where)))
      replenish(where, item_flotsam, 30, 30);

    /*
     *  Sun Dec  1 10:34:41 1996 -- Scott Turner
     *
     *  Peasant production.  Depends upon the location (and
     *  whether it contains a city).
     *
     *  Has_city is set up above...
     *
     *  Tue Sep 22 13:20:18 1998 -- Scott Turner
     *
     *  Faery ought not have peasants.  It should have (I guess)
     *  elf peasants, although what you can do with those is
     *  open to conjecture :-)
     *
     */
    if (pop = has_item(where, item_peasant)) {
      if (has_city) {
        pop_grow = 0.03;
        pop_limit = 10000;
      }
      else {
        switch (terr) {
        case sub_plain:
        case sub_forest:
          pop_grow = 0.01;
          pop_limit = 1000;
          break;
        case sub_mountain:
        case sub_swamp:
          pop_grow = 0.005;
          pop_limit = 1000;
          break;
        default:
          pop_grow = 0.000;
          pop_limit = 500;
          break;
        };
      };

      /*
       *  Might be an effect here.
       *
       */
      if (get_effect(where, ef_grow, 0, 0)) {
        wout(where, "The peasants seem particularly happy this month.");
        pop_grow += 0.02;
      };

      dpop = pop * pop_grow;
      if (pop_grow > 0.0 && dpop < 1)
        dpop = 1;
      /*
       *  Lose population at a reasonable rate.
       *
       */
      if (pop > pop_limit)
        dpop = -(pop - pop_limit) / 10;

      if (p_subloc(province(where))->loot && dpop > 0) {
        wout(where,
             "Pillaging traumatizes the population and no growth occurs.");
      }
      else if (dpop > 0) {
        if (pop > 100)
          wout(where, "The population grows by %s peasant%s.",
               nice_num(dpop), add_s(dpop));
        gen_item(where, item_peasant, dpop);
      }
      else {
        if (pop > 100)
          wout(where, "Overcrowding causes %s peasant death%s.",
               nice_num(-dpop), add_s(-dpop));
        consume_item(where, item_peasant, -dpop);
      };
    };

    /*
     *  Sat Apr 18 16:57:53 1998 -- Scott Turner
     *
     *  Special case for gold production from peasants.
     *  They generate 1 gold per 20 peasants (1/10 in cities)
     *  which accumulates to be removed by various means.
     *
     *  Only in civilized (> 100) provinces
     */
    if ((pop = has_item(where, item_peasant)) > 100) {
      if (has_city) {
        dpop = pop * 0.10;
      }
      else {
        dpop = pop * 0.05;
      };
      gen_item(where, item_gold, dpop);
    };
  }
Example #5
0
// MANIPULATORS
void *ConcurrentPool::allocate()
{
    Link *p;
    for (;;) {
        p = d_freeList.loadRelaxed();
        if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(!p)) {
            BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
            bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex);
            p = d_freeList;
            if (!p) {
                replenish();
                continue;
            }
        }

        if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY
                  (2 != bsls::AtomicOperations::addIntNv(&p->d_refCount, 2))) {
            BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
            for (int i = 0; i < 3; ++i) {
                // To avoid unnecessary contention, assume that if we did not
                // get the first reference, then the other thread is about to
                // complete the pop.  Wait for a few cycles until he does.  If
                // he does not complete then go on and try to acquire it
                // ourselves.

                if (d_freeList.loadRelaxed() != p) {
                    break;
                }
            }
        }

        // Force a dependent read of 'd_next_p' to make sure that we're not
        // racing against another thread calling 'deallocate' for 'p' and that
        // checked the refcount *before* we incremented it, put back 'p' in the
        // freelist with a potentially different 'd_next_p'.
        //
        // There are two possibilities in this particular case:
        //   - The following 'loadRelaxed()' will return the new 'freelist'
        //     value (== p) and because of the release barrier before the last
        //     CAS in deallocate, we can observe the new 'd_next_p' value (this
        //     relies on dependent load ordering)
        //   - loadRelaxed() will return the "old" (!= p) and the CAS and thus
        //     the condition will be false.
        //
        // Note that 'h' is made volatile so that the compiler does not replace
        // the 'h->d_inUse' load with 'p->d_inUse' (and thus removing the data
        // dependency).  TBD to be completely thorough 'h->d_next_p' needs a
        // load dependent barrier (no-op on all current architectures though).

        const Link * volatile h = d_freeList.loadRelaxed();

        // gcc 4.3, 4.4 seems to have trouble processing likely(a && b), using
        // likely(a) && likely(b) fixes the problem.  3.4.6 seems to generate
        // the proper code though.

        if (BSLS_PERFORMANCEHINT_PREDICT_LIKELY(h == p)
         && BSLS_PERFORMANCEHINT_PREDICT_LIKELY(
                                d_freeList.testAndSwap(p, h->d_next_p) == p)) {
            break;
        }

        BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
        for (;;) {
            int refCount = bsls::AtomicOperations::getInt(&p->d_refCount);

            if (refCount & 1) {
                if (refCount ==
                        bsls::AtomicOperations::testAndSwapInt(
                            &p->d_refCount,
                            refCount,
                            refCount^1)) {
                    // The node is now free but not on the free list.  Try to
                    // take it.

                    return static_cast<void *>(const_cast<Link **>(
                                                      &p->d_next_p)); // RETURN
                }
            }
            else if (refCount ==
                        bsls::AtomicOperations::testAndSwapInt(
                            &p->d_refCount,
                            refCount,
                            refCount - 2)) {
                break;
            }
        }
    }

    return static_cast<void *>(const_cast<Link **>(&p->d_next_p));
}