示例#1
0
/* config_set_string; Sets a value in the config, possibly overriding
 * an existing value
 */
void config_set_string(const char *directive, const char* s) {
    stringmap S;

    S = stringmap_find(config, directive);
    if (S) {
      xfree(S->d.v);
      S->d = item_ptr(xstrdup(s));
    }
    else {
      stringmap_insert(config, directive, item_ptr(xstrdup(s)));
    }
}
示例#2
0
item_ptr fork_database::fetch_block( const block_id_type& id )const
{
   auto itr = _index.get<block_id>().find(id);
   if( itr != _index.get<block_id>().end() )
      return *itr;
   return item_ptr();
}
示例#3
0
void wsplayer_t::delegate_step()
{
    incer_t<int> hld_thinking(thinking);

	field=game().field();
	root=item_ptr(new item_t(*this,field.back()));

	ObjectProgress::log_generator lg(true);

	init_states();

	try
	{
		root->process_deep_common();
	}
	catch(e_cancel&)
	{
		lg<<"canceled";
        hld_thinking.reset();
        throw;
	}

	if(!root->get_wins().empty())
	{
		unsigned int depth=root->get_chain_depth()-1;
		std::string chain=print_chain(root->get_wins().get_best());
		lg<<"wsplayer_t::delegate_step(): find win chain_depth="<<depth<<": "<<chain;
	}
	if(!root->get_fails().empty() && root->get_neitrals().empty())lg<<"wsplayer_t::delegate_step(): find fail chain_depth="<<(root->get_chain_depth()-1)
		<<": "<<print_chain(root->get_fails().get_best());

	point p=*root->get_next_step();
    game().OnNextStep(*this,p);
}
示例#4
0
 // C++ allocator interface
 T* allocate(size_t n) {
     assert(n == 1);
     if( next_free == -1 ) {
         throw std::bad_alloc();
     }
     const int current = next_free;
     next_free = indicator(next_free);
     return item_ptr(current);
 }
示例#5
0
void wsplayer_t::solve()
{
	field=game().field();
	wide_item_t* wr=new wide_item_t(*this,field.back());
	root=item_ptr(wr);

	init_states();
	wr->process_deep_common();
}
item_ptr fork_database::fetch_block(const block_id_type& id)const
{
   auto& index = _index.get<block_id>();
   auto itr = index.find(id);
   if( itr != index.end() )
      return *itr;
   auto& unlinked_index = _unlinked_index.get<block_id>();
   auto unlinked_itr = unlinked_index.find(id);
   if( unlinked_itr != unlinked_index.end() )
      return *unlinked_itr;
   return item_ptr();
}
示例#7
0
/**
 * Updates or creates (as appropriate) the menu item with the given @a id.
 */
void wmi_container::set_item(const std::string& id, const vconfig& menu_item)
{
	// Try to insert a dummy value. This combines looking for an existing
	// entry with insertion.
	map_t::iterator add_it = wml_menu_items_.insert(map_t::value_type(id, item_ptr())).first;

	if ( add_it->second )
		// Create a new menu item based on the old. This leaves the old item
		// alone in case someone else is holding on to (and processing) it.
		add_it->second.reset(new wml_menu_item(id, menu_item, *add_it->second));
	else
		// This is a new menu item.
		add_it->second.reset(new wml_menu_item(id, menu_item));
}
示例#8
0
void 
load_activity::load_item(unsigned int count, outputer::shared_ptr outer)
{/*{{{*/
	try
	{
		log_item::shared_ptr item_ptr(new log_item); 
        log_item& item=*item_ptr;
        _analyser_ptr->analyse(*_stream_in,item);
		if(_filter_rule_ptr && 
			! _filter_rule_ptr->is_true(item_ptr))
		{	
			if(_logger.isDebugEnabled())	
				_logger.debugStream()<<" filter it : "<< *item_ptr;	
		}
		else
		{
			outer->output(item_ptr);
		}
        if(_stream_in->bad()) 
            _stream_in->clear();
        if(_stream_in->fail())
            _stream_in->clear();
	}
	catch(data_error& e)
	{
		if(_stream_in->bad()) throw;
		if(_stream_in->eof()) return;
		_stream_in->clear();
		_stream_in->ignore(1024,'\n');
		if(_logger.isErrorEnabled())
			_logger.errorStream()<<e.what()<<"line:"<<count;
	}
	catch(exception& ex)
	{
		_stream_in->ignore(1024,'\n');
		_logger.errorStream()<<ex.what()<<"line:"<<count;
	}
}/*}}}*/
 // C++-like allocator interface
 T* allocate() {
     unsigned long allocated_item_coded = next_free.load();
     
     int allocated_item;
     //tprintf(tinfra::err, "T[%i]: allocate first try with allocated_item %i\n", get_thread_string(), idx_decode(allocated_item_coded));
     while( true ) {
         allocated_item = idx_decode(allocated_item_coded);
         if( allocated_item == -1 ) {
             throw std::bad_alloc();
         }
         const int new_next_free = item_next_free_idx(allocated_item);
             // so here is the race
             //  1  (allocated_item AI, new_next_free NNF) pair is "read"
             //  2. (tries to commit, but ... in meantime)
             //  3.   (other thread allocates AI as a race)
             //  3.1  (NNF is allocates by other thread) ...
             //  4.   (other thread deallocates AI and it appears again as NF but now points to NNF2) 
             //  5. (commit succedds with NNF because AI is same as in 1) 
             //     BUG, NF points at NNF, which is not necessarily allocated
             //     should point at NNF2 (!?) impossible
             //     or TXN this kind of race should be detected and whole TXN shall be retried
         assert(new_next_free != allocated_item);
         //if(rand() % 2 ) my_nanosleep(1);
         const unsigned long new_next_free_coded = idx_encode(new_next_free);
         const bool change_succeded = next_free.compare_exchange_strong(allocated_item_coded, new_next_free_coded);
         if( !change_succeded ) {
             allocated_item = idx_decode(allocated_item_coded);
             //tprintf(tinfra::err, "T[%i]: allocate, retrying with allocated_item %i\n", get_thread_string(), allocated_item);
             continue;
         } else {
             //tprintf(tinfra::err, "T[%i]: allocated %i, next_free = %i\n", get_thread_string(), allocated_item, new_next_free);
         }
         break;
     }
     my_assert(_check_allocated(allocated_item));
     ++n_allocated;
     return item_ptr(allocated_item);
 }
示例#10
0
/* read_config_file:
 * Read a configuration file consisting of key: value tuples, returning a
 * stringmap of the results. Prints errors to stderr, rather than using
 * syslog, since this file is called at program startup. Returns 1 on success
 * or 0 on failure. */
int read_config_file(const char *f, int whinge) {
    int ret = 0;
    FILE *fp;
    char *line;
    int i = 1;

    line = xmalloc(MAX_CONFIG_LINE);

    fp = fopen(f, "rt");
    if (!fp) {
        if(whinge) fprintf(stderr, "%s: %s\n", f, strerror(errno)); 
        goto fail;
    }

    while (fgets(line, MAX_CONFIG_LINE, fp)) {
        char *key, *value, *r;

        for (r = line + strlen(line) - 1; r > line && *r == '\n'; *(r--) = 0);

        /* Get continuation lines. Ugly. */
        while (*(line + strlen(line) - 1) == '\\') {
            if (!fgets(line + strlen(line) - 1, MAX_CONFIG_LINE - strlen(line), fp))
                break;
            for (r = line + strlen(line) - 1; r > line && *r == '\n'; *(r--) = 0);
        }

        /* Strip comment. */
        key = strpbrk(line, "#\n");
        if (key) *key = 0;

        /*    foo  : bar baz quux
         * key^    ^value          */
        key = line + strspn(line, " \t");
        value = strchr(line, ':');

        if (value) {
            /*    foo  : bar baz quux
             * key^  ^r ^value         */
            ++value;

            r = key + strcspn(key, " \t:");
            if (r != key) {
                item *I;
                *r = 0;

                /*    foo\0: bar baz quux
                 * key^      ^value      ^r */
                value += strspn(value, " \t");
                r = value + strlen(value) - 1;
                while (strchr(" \t", *r) && r > value) --r;
                *(r + 1) = 0;

                /* (Removed check for zero length value.) */

                /* Check that this is a valid key. */
                if (!is_cfgdirective_valid(key))
                    fprintf(stderr, "%s:%d: warning: unknown directive \"%s\"\n", f, i, key);
                else if ((I = stringmap_insert(config, key, item_ptr(xstrdup(value)))))
                    /* Don't warn of repeated directives, because they
                     * may have been specified via the command line
                     * Previous option takes precedence.
                     */
                    fprintf(stderr, "%s:%d: warning: repeated directive \"%s\"\n", f, i, key);
            }
        }

        memset(line, 0, MAX_CONFIG_LINE); /* security paranoia */

        ++i;
    }

    ret = 1;

fail:
    if (fp) fclose(fp);
    if (line) xfree(line);

    return ret;
}
示例#11
0
void row::append( Item * item )
{
    std::unique_ptr<Item> item_ptr(item);
    Items.push_back( std::move(item_ptr) );
}