예제 #1
0
파일: Struct.cpp 프로젝트: Astron/Bamboo
// add_field adds a new Field to the struct.
bool Struct::add_field(std::unique_ptr<Field> field)
{
    Field *ref = field.get();

    // Field must not be null
    if(field == nullptr) {
        return false;
    }

    // Structs can't share a field
    if(field->record() != nullptr && field->record() != this) {
        return false;
    }

    // Structs can't have moleculars
    if(field->as_molecular()) {
        return false;
    }
    // Structs can't have methods
    if(field->type()->as_method()) {
        return false;
    }

    // Struct fields are accessible by name
    if(!field->name().empty()) {
        // Structs can't have Constructors
        if(field->name() == m_name) {
            return false;
        }

        // Try to add the field
        bool inserted = m_fields_by_name.insert(
                            unordered_map<string, Field *>::value_type(field->name(), ref)).second;
        if(!inserted) {
            // But the field had a name conflict
            return false;
        }
        // The index of this named field is the next available index
        m_indices_by_name[field->name()] = (unsigned int)m_fields.size();
    }

    // Struct fields are accessible by id.
    m_module->add_field(ref);
    m_fields_by_id.insert(unordered_map<int, Field *>::value_type(field->id(), ref));

    // Add it to the accessible list of fields
    m_fields.push_back(ref);
    if(has_fixed_size() || m_fields.size() == 1) {
        if(field->type()->has_fixed_size()) {
            m_size += field->type()->fixed_size();
        } else {
            m_size = 0;
        }
    }

    // Transfer ownership of the Field to the Struct
    m_owned_fields.push_back(move(field));

    return true;
}
예제 #2
0
// add_field adds a new Field to the struct.
bool Struct::add_field(Field* field)
{
    // Field must not be null
    if(field == (Field*)NULL)
    {
        return false;
    }

    // Structs can't share a field
    if(field->get_struct() != NULL && field->get_struct() != this)
    {
        return false;
    }

    // Structs can't have moleculars
    if(field->as_molecular())
    {
        return false;
    }
    // Structs can't have methods
    if(field->get_type()->as_method())
    {
        return false;
    }

    // Struct fields are accessible by name
    if(!field->get_name().empty())
    {
        // Structs can't have Constructors
        if(field->get_name() == m_name)
        {
            return false;
        }

        // Try to add the field
        bool inserted = m_fields_by_name.insert(
                            unordered_map<string, Field*>::value_type(field->get_name(), field)).second;

        if(!inserted)
        {
            // But the field had a name conflict
            return false;
        }
    }

    // Struct fields are accessible by id.
    m_file->add_field(field);
    m_fields_by_id.insert(unordered_map<int, Field*>::value_type(field->get_id(), field)).second;

    m_fields.push_back(field);
    if(has_fixed_size() || m_fields.size() == 1)
    {
        if(field->get_type()->has_fixed_size())
        {
            m_size += field->get_type()->get_size();
        }
        else
        {
            m_size = 0;
        }
    }
    return true;
}