<commit_before>
#include <list>
#include <sstream>

#include <boost/thread/mutex.hpp>

#include "scsi/base.h"
#include "scsi/util.h"

namespace {
// This mutex guards the global Machine::p_state_infos
typedef boost::mutex info_mutex_t;
info_mutex_t info_mutex;
}

StateBase::~StateBase() {}

StateBase::StateBase(const Config&) :next_elem(0), pyptr(0) {}

ElementVoid::ElementVoid(const Config& conf)
    :name(conf.get<std::string>("name"))
    ,index(0)
    ,p_conf(conf)
{}

ElementVoid::~ElementVoid() {}

void ElementVoid::show(std::ostream& strm) const
{
    strm<<"Element "<<index<<": "<<name<<" ("<<type_name()<<")\n";
}

Machine::Machine(const Config& c)
    :p_elements()
    ,p_trace(NULL)
    ,p_info()
{
    std::string type(c.get<std::string>("sim_type"));

    info_mutex_t::scoped_lock G(info_mutex);

    p_state_infos_t::iterator it = p_state_infos.find(type);
    if(it==p_state_infos.end()) {
        std::ostringstream msg;
        msg<<"Unsupport sim_type '"<<type<<"'";
        throw key_error(msg.str());
    }

    p_info = it->second;

    typedef Config::vector_t elements_t;
    elements_t Es(c.get<elements_t>("elements"));

    p_elements_t result;
    result.reserve(Es.size());

    size_t idx=0;
    for(elements_t::iterator it=Es.begin(), end=Es.end(); it!=end; ++it)
    {
        const Config& EC = *it;

        const std::string& etype(EC.get<std::string>("type"));

        state_info::elements_t::iterator eit = p_info.elements.find(etype);
        if(eit==p_info.elements.end())
            throw key_error(etype);

        element_builder_t builder = eit->second;

        ElementVoid *E = (*builder)(EC);

        *const_cast<size_t*>(&E->index) = idx++; // ugly

        result.push_back(E);
    }

    G.unlock();
<commit_msg>improve error messages from Machine ctor<commit_after>
#include <list>
#include <sstream>

#include <boost/thread/mutex.hpp>

#include "scsi/base.h"
#include "scsi/util.h"

namespace {
// This mutex guards the global Machine::p_state_infos
typedef boost::mutex info_mutex_t;
info_mutex_t info_mutex;
}

StateBase::~StateBase() {}

StateBase::StateBase(const Config&) :next_elem(0), pyptr(0) {}

ElementVoid::ElementVoid(const Config& conf)
    :name(conf.get<std::string>("name"))
    ,index(0)
    ,p_conf(conf)
{}

ElementVoid::~ElementVoid() {}

void ElementVoid::show(std::ostream& strm) const
{
    strm<<"Element "<<index<<": "<<name<<" ("<<type_name()<<")\n";
}

Machine::Machine(const Config& c)
    :p_elements()
    ,p_trace(NULL)
    ,p_info()
{
    std::string type(c.get<std::string>("sim_type"));

    info_mutex_t::scoped_lock G(info_mutex);

    p_state_infos_t::iterator it = p_state_infos.find(type);
    if(it==p_state_infos.end()) {
        std::ostringstream msg;
        msg<<"Unsupport sim_type '"<<type<<"'";
        throw key_error(msg.str());
    }

    p_info = it->second;

    typedef Config::vector_t elements_t;
    elements_t Es(c.get<elements_t>("elements"));

    p_elements_t result;
    result.reserve(Es.size());

    size_t idx=0;
    for(elements_t::iterator it=Es.begin(), end=Es.end(); it!=end; ++it)
    {
        const Config& EC = *it;

        const std::string& etype(EC.get<std::string>("type"));

        state_info::elements_t::iterator eit = p_info.elements.find(etype);
        if(eit==p_info.elements.end())
            throw key_error(etype);

        element_builder_t builder = eit->second;

        ElementVoid *E;
        try{
            E = (*builder)(EC);
        }catch(key_error& e){
            std::ostringstream strm;
            strm<<"Error while initializing element "<<idx<<" '"<<EC.get<std::string>("name", "<invalid>")
               <<"' : missing required parameter '"<<e.what()<<"'";
            throw key_error(strm.str());

        }catch(std::exception& e){
            std::ostringstream strm;
            strm<<"Error while constructing element "<<idx<<" '"<<EC.get<std::string>("name", "<invalid>")
               <<"' : "<<e.what();
            throw std::runtime_error(strm.str());
        }

        *const_cast<size_t*>(&E->index) = idx++; // ugly

        result.push_back(E);
    }

    G.unlock();

{'fraction_non_alphanumeric': 0.18258359393793602, 'fraction_numerical': 0.0019244647582391148, 'mean_word_length': 3.447058823529412, 'pattern_counts': {'":': 2, '<': 89, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '597562', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1472, 'n_words': 248}