VirtualBox

Changeset 101294 in vbox


Ignore:
Timestamp:
Sep 27, 2023 2:48:22 PM (12 months ago)
Author:
vboxsync
Message:

FE/Qt: bugref:10523: Some refactoring in UIGuestOSType classes:

  • Make UIGuestOSType private to its manager,
  • Use a map to locate correct UIGuestOSType wrt. typeId, which is a most common type of query on in the type array.
Location:
trunk/src/VBox/Frontends/VirtualBox/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIGuestOSType.cpp

    r101278 r101294  
    3030
    3131
     32/** A wrapper around CGuestOSType. Some of the properties are cached here for performance. */
     33class SHARED_LIBRARY_STUFF UIGuestOSType
     34{
     35
     36public:
     37
     38
     39    UIGuestOSType(const CGuestOSType &comGuestOSType);
     40    UIGuestOSType();
     41
     42    const QString &getFamilyId() const;
     43    const QString &getFamilyDescription() const;
     44    const QString &getId() const;
     45    const QString &getVariant() const;
     46    const QString &getDescription() const;
     47
     48    /** @name Wrapper getters for CGuestOSType member.
     49      * @{ */
     50        KStorageBus             getRecommendedHDStorageBus() const;
     51        ULONG                   getRecommendedRAM() const;
     52        KStorageBus             getRecommendedDVDStorageBus() const;
     53        ULONG                   getRecommendedCPUCount() const;
     54        KFirmwareType           getRecommendedFirmware() const;
     55        bool                    getRecommendedFloppy() const;
     56        LONG64                  getRecommendedHDD() const;
     57        KGraphicsControllerType getRecommendedGraphicsController() const;
     58    /** @} */
     59
     60    bool isOk() const;
     61
     62private:
     63
     64    /** @name CGuestOSType properties. Cached here for a faster access.
     65      * @{ */
     66        mutable QString m_strFamilyId;
     67        mutable QString m_strFamilyDescription;
     68        mutable QString m_strId;
     69        mutable QString m_strVariant;
     70        mutable QString m_strDescription;
     71    /** @} */
     72
     73    CGuestOSType m_comGuestOSType;
     74};
     75
     76UIGuestOSTypeManager::UIGuestOSTypeManager()
     77    :m_guestOSTypes(new QList<UIGuestOSType>())
     78{
     79}
     80
    3281void UIGuestOSTypeManager::reCacheGuestOSTypes(const CGuestOSTypeVector &guestOSTypes)
    3382{
    34     m_guestOSTypes.clear();
     83    m_typeIdIndexMap.clear();
     84    m_guestOSTypes->clear();
    3585    m_guestOSFamilies.clear();
    36     //m_guestOSTypesPerFamily.clear();
     86
    3787    QVector<CGuestOSType> otherOSTypes;
    3888    foreach (const CGuestOSType &comType, guestOSTypes)
     
    52102void UIGuestOSTypeManager::addGuestOSType(const CGuestOSType &comType)
    53103{
    54     m_guestOSTypes << UIGuestOSType(comType);
    55     QPair<QString, QString> family = QPair<QString, QString>(m_guestOSTypes.last().getFamilyId(), m_guestOSTypes.last().getFamilyDescription());
     104    m_guestOSTypes->append(UIGuestOSType(comType));
     105    m_typeIdIndexMap[m_guestOSTypes->last().getId()] = m_guestOSTypes->size() - 1;
     106    QPair<QString, QString> family = QPair<QString, QString>(m_guestOSTypes->last().getFamilyId(), m_guestOSTypes->last().getFamilyDescription());
    56107    if (!m_guestOSFamilies.contains(family))
    57108        m_guestOSFamilies << family;
     
    65116QStringList UIGuestOSTypeManager::getVariantListForFamilyId(const QString &strFamilyId) const
    66117{
     118    AssertReturn(m_guestOSTypes, QStringList());
    67119    QStringList variantList;
    68     foreach (const UIGuestOSType &type, m_guestOSTypes)
     120    foreach (const UIGuestOSType &type, *m_guestOSTypes)
    69121    {
    70122        if (type.getFamilyId() != strFamilyId)
     
    80132{
    81133    UIGuestOSTypeInfo typeInfoList;
    82     foreach (const UIGuestOSType &type, m_guestOSTypes)
     134    AssertReturn(m_guestOSTypes, typeInfoList);
     135    foreach (const UIGuestOSType &type, *m_guestOSTypes)
    83136    {
    84137        if (type.getFamilyId() != strFamilyId)
     
    95148{
    96149    UIGuestOSTypeInfo typeInfoList;
     150    AssertReturn(m_guestOSTypes, typeInfoList);
    97151    if (strVariant.isEmpty())
    98152        return typeInfoList;
    99153
    100     foreach (const UIGuestOSType &type, m_guestOSTypes)
     154    foreach (const UIGuestOSType &type, *m_guestOSTypes)
    101155    {
    102156        if (type.getVariant() != strVariant)
     
    109163}
    110164
    111 UIGuestOSType UIGuestOSTypeManager::findGuestTypeById(const QString &strTypeId) const
    112 {
    113     if (strTypeId.isEmpty())
    114         return UIGuestOSType();
    115     foreach (const UIGuestOSType &type, m_guestOSTypes)
    116     {
    117         if (type.getId() == strTypeId)
    118             return type;
    119     }
    120     return UIGuestOSType();
     165QString UIGuestOSTypeManager::getFamilyId(const QString &strTypeId) const
     166{
     167    AssertReturn(m_guestOSTypes, QString());
     168    /* Let QVector<>::value check for the bounds. It returns a default constructed value when it is out of bounds. */
     169    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getFamilyId();
     170}
     171
     172QString UIGuestOSTypeManager::getVariant(const QString  &strTypeId) const
     173{
     174    AssertReturn(m_guestOSTypes, QString());
     175    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getVariant();
    121176}
    122177
    123178KGraphicsControllerType UIGuestOSTypeManager::getRecommendedGraphicsController(const QString &strTypeId) const
    124179{
    125     if (strTypeId.isEmpty())
    126         return KGraphicsControllerType_Null;
    127     foreach (const UIGuestOSType &type, m_guestOSTypes)
    128     {
    129         if (type.getId() == strTypeId)
    130             return type.getRecommendedGraphicsController();
    131     }
    132     return KGraphicsControllerType_Null;
     180    AssertReturn(m_guestOSTypes, KGraphicsControllerType_Null);
     181    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedGraphicsController();
    133182}
    134183
    135184ULONG UIGuestOSTypeManager::getRecommendedRAM(const QString &strTypeId) const
    136185{
    137     if (strTypeId.isEmpty())
    138         return 0;
    139     foreach (const UIGuestOSType &type, m_guestOSTypes)
    140     {
    141         if (type.getId() == strTypeId)
    142             return type.getRecommendedRAM();
    143     }
    144     return 0;
     186    AssertReturn(m_guestOSTypes, 0);
     187    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedRAM();
    145188}
    146189
    147190ULONG UIGuestOSTypeManager::getRecommendedCPUCount(const QString &strTypeId) const
    148191{
    149     if (strTypeId.isEmpty())
    150         return 0;
    151     foreach (const UIGuestOSType &type, m_guestOSTypes)
    152     {
    153         if (type.getId() == strTypeId)
    154             return type.getRecommendedCPUCount();
    155     }
    156     return 0;
     192    AssertReturn(m_guestOSTypes, 0);
     193    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedCPUCount();
    157194}
    158195
    159196KFirmwareType UIGuestOSTypeManager::getRecommendedFirmware(const QString &strTypeId) const
    160197{
    161     if (strTypeId.isEmpty())
    162         return KFirmwareType_Max;
    163     foreach (const UIGuestOSType &type, m_guestOSTypes)
    164     {
    165         if (type.getId() == strTypeId)
    166             return type.getRecommendedFirmware();
    167     }
    168     return KFirmwareType_Max;
     198    AssertReturn(m_guestOSTypes, KFirmwareType_Max);
     199    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedFirmware();
    169200}
    170201
    171202QString UIGuestOSTypeManager::getDescription(const QString &strTypeId) const
    172203{
    173     if (strTypeId.isEmpty())
    174         return QString();
    175     foreach (const UIGuestOSType &type, m_guestOSTypes)
    176     {
    177         if (type.getId() == strTypeId)
    178             return type.getDescription();
    179     }
    180     return QString();
     204    AssertReturn(m_guestOSTypes, QString());
     205    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getDescription();
    181206}
    182207
    183208LONG64 UIGuestOSTypeManager::getRecommendedHDD(const QString &strTypeId) const
    184209{
    185     if (strTypeId.isEmpty())
    186         return 0;
    187     foreach (const UIGuestOSType &type, m_guestOSTypes)
    188     {
    189         if (type.getId() == strTypeId)
    190             return type.getRecommendedHDD();
    191     }
    192     return 0;
     210    AssertReturn(m_guestOSTypes, 0);
     211    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedHDD();
    193212}
    194213
    195214KStorageBus UIGuestOSTypeManager::getRecommendedHDStorageBus(const QString &strTypeId) const
    196215{
    197     if (strTypeId.isEmpty())
    198         return KStorageBus_Null;
    199     foreach (const UIGuestOSType &type, m_guestOSTypes)
    200     {
    201         if (type.getId() == strTypeId)
    202             return type.getRecommendedHDStorageBus();
    203     }
    204     return KStorageBus_Null;
     216    AssertReturn(m_guestOSTypes, KStorageBus_Null);
     217    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedHDStorageBus();
    205218}
    206219
    207220KStorageBus UIGuestOSTypeManager::getRecommendedDVDStorageBus(const QString &strTypeId) const
    208221{
    209     if (strTypeId.isEmpty())
    210         return KStorageBus_Null;
    211     foreach (const UIGuestOSType &type, m_guestOSTypes)
    212     {
    213         if (type.getId() == strTypeId)
    214             return type.getRecommendedDVDStorageBus();
    215     }
    216     return KStorageBus_Null;
    217 }
    218 
     222    AssertReturn(m_guestOSTypes, KStorageBus_Null);
     223    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedDVDStorageBus();
     224}
    219225
    220226bool UIGuestOSTypeManager::getRecommendedFloppy(const QString &strTypeId) const
    221227{
    222     if (strTypeId.isEmpty())
    223         return false;
    224     foreach (const UIGuestOSType &type, m_guestOSTypes)
    225     {
    226         if (type.getId() == strTypeId)
    227             return type.getRecommendedFloppy();
    228     }
    229     return false;
     228    AssertReturn(m_guestOSTypes, false);
     229    return m_guestOSTypes->value(m_typeIdIndexMap.value(strTypeId, -1)).getRecommendedFloppy();
    230230}
    231231
     
    334334    return KGraphicsControllerType_Null;
    335335}
    336 
    337 bool UIGuestOSType::operator==(const UIGuestOSType &other)
    338 {
    339     return m_comGuestOSType == other.m_comGuestOSType;
    340 }
    341 
    342 bool UIGuestOSType::operator!=(const UIGuestOSType &other)
    343 {
    344     return m_comGuestOSType != other.m_comGuestOSType;
    345 }
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIGuestOSType.h

    r101285 r101294  
    4242
    4343class UIGuestOSType;
    44 
     44class UIFoo;
    4545
    4646/** A wrapper and manager class for Guest OS types (IGuestOSType). Logically we structure os types into families
     
    5252
    5353public:
     54
     55    UIGuestOSTypeManager();
     56    UIGuestOSTypeManager(const UIGuestOSTypeManager &other) = delete;
    5457
    5558    /* A list of all OS families. 'first' of each pair is famil Id and 'second' is family description. */
     
    6568    UIGuestOSTypeInfo getTypeListForVariant(const QString &strVariant) const;
    6669
    67     UIGuestOSType findGuestTypeById(const QString &strTypeId) const;
    68 
    69     KGraphicsControllerType getRecommendedGraphicsController(const QString &strTypeId) const;
    70     ULONG getRecommendedRAM(const QString &strTypeId) const;
    71     ULONG getRecommendedCPUCount(const QString &strTypeId) const;
    72     KFirmwareType getRecommendedFirmware(const QString &strTypeId) const;
    73     QString getDescription(const QString &strTypeId) const;
    74     LONG64 getRecommendedHDD(const QString &strTypeId) const;
    75     KStorageBus getRecommendedHDStorageBus(const QString &strTypeId) const;
    76     KStorageBus getRecommendedDVDStorageBus(const QString &strTypeId) const;
    77     bool getRecommendedFloppy(const QString &strTypeId) const;
     70    /** @name Getters UIGuestOSType properties. They utilize a map for faster access to UIGuestOSType instance with @p strTypeId
     71      * @{ */
     72        QString                 getFamilyId(const QString &strTypeId) const;
     73        QString                 getVariant(const QString  &strTypeId) const;
     74        KGraphicsControllerType getRecommendedGraphicsController(const QString &strTypeId) const;
     75        ULONG                   getRecommendedRAM(const QString &strTypeId) const;
     76        ULONG                   getRecommendedCPUCount(const QString &strTypeId) const;
     77        KFirmwareType           getRecommendedFirmware(const QString &strTypeId) const;
     78        QString                 getDescription(const QString &strTypeId) const;
     79        LONG64                  getRecommendedHDD(const QString &strTypeId) const;
     80        KStorageBus             getRecommendedHDStorageBus(const QString &strTypeId) const;
     81        KStorageBus             getRecommendedDVDStorageBus(const QString &strTypeId) const;
     82        bool                    getRecommendedFloppy(const QString &strTypeId) const;
     83    /** @} */
    7884
    7985private:
     
    8187    void addGuestOSType(const CGuestOSType &comType);
    8288
    83     QVector<UIGuestOSType> m_guestOSTypes;
    84     /* First item of the pair is family id and the 2nd is family description. */
     89    /** The type list. Here it is a pointer to QVector to delay definition of UIGuestOSType. */
     90    QVector<UIGuestOSType> *m_guestOSTypes;
     91    /** A map to prevent linear search of UIGuestOSType instances wrt. typeId. Key is typeId and value
     92      * is index to m_guestOSTypes list. */
     93    QMap<QString, int> m_typeIdIndexMap;
     94    /** First item of the pair is family id and the 2nd is family description. */
    8595    UIGuestOSTypeInfo m_guestOSFamilies;
    86 
    8796};
    8897
    89 /** A wrapper around CGuestOSType. */
    90 class SHARED_LIBRARY_STUFF UIGuestOSType
    91 {
    92 
    93 public:
    94 
    95 
    96     UIGuestOSType(const CGuestOSType &comGuestOSType);
    97     UIGuestOSType();
    98 
    99     const QString &getFamilyId() const;
    100     const QString &getFamilyDescription() const;
    101     const QString &getId() const;
    102     const QString &getVariant() const;
    103     const QString &getDescription() const;
    104 
    105     /** @name Wrapper getters for CGuestOSType member.
    106       * @{ */
    107         KStorageBus getRecommendedHDStorageBus() const;
    108         ULONG getRecommendedRAM() const;
    109         KStorageBus getRecommendedDVDStorageBus() const;
    110         ULONG getRecommendedCPUCount() const;
    111         KFirmwareType getRecommendedFirmware() const;
    112         bool getRecommendedFloppy() const;
    113         LONG64 getRecommendedHDD() const;
    114         KGraphicsControllerType getRecommendedGraphicsController() const;
    115     /** @} */
    116 
    117     bool isOk() const;
    118     bool operator==(const UIGuestOSType &other);
    119     bool operator!=(const UIGuestOSType &other);
    120 
    121 private:
    122 
    123     /** @name CGuestOSType properties. Cached here for a faster access.
    124       * @{ */
    125         mutable QString m_strFamilyId;
    126         mutable QString m_strFamilyDescription;
    127         mutable QString m_strId;
    128         mutable QString m_strVariant;
    129         mutable QString m_strDescription;
    130     /** @} */
    131 
    132     CGuestOSType m_comGuestOSType;
    133 
    134 };
    13598
    13699#endif /* !FEQT_INCLUDED_SRC_globals_UIGuestOSType_h */
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/editors/UINameAndSystemEditor.cpp

    r101286 r101294  
    166166    const UIGuestOSTypeManager * const pGuestOSTypeManager = uiCommon().guestOSTypeManager();
    167167    AssertReturn(pGuestOSTypeManager, false);
    168     const UIGuestOSType &type = pGuestOSTypeManager->findGuestTypeById(strTypeId);
    169     if (!type.isOk())
    170         return false;
     168    // const UIGuestOSType &type = pGuestOSTypeManager->findGuestTypeById(strTypeId);
     169    // if (!type.isOk())
     170    //     return false;
    171171
    172172    int iFamilyComboIndex = -1;
     
    175175    {
    176176        QString strComboFamilyId = m_pComboFamily->itemData(i, FamilyID).toString();
    177         if (!strComboFamilyId.isEmpty() && strComboFamilyId == type.getFamilyId())
     177        if (!strComboFamilyId.isEmpty() && strComboFamilyId == pGuestOSTypeManager->getFamilyId(strTypeId))
    178178            iFamilyComboIndex = i;
    179179    }
     
    185185
    186186    /* If variant is not empty then try to select correct index. This will populate type combo: */
    187     if (!type.getVariant().isEmpty())
     187    QString strVariant = pGuestOSTypeManager->getVariant(strTypeId);
     188    if (!strVariant.isEmpty())
    188189    {
    189190        int index = -1;
    190191        for (int i = 0; i < m_pComboVariant->count() && index == -1; ++i)
    191192        {
    192             if (type.getVariant() == m_pComboVariant->itemText(i))
     193            if (strVariant == m_pComboVariant->itemText(i))
    193194                index = i;
    194195        }
  • trunk/src/VBox/Frontends/VirtualBox/src/settings/editors/UINameAndSystemEditor.h

    r101286 r101294  
    119119    /** Returns the VM OS family ID. */
    120120    QString familyId() const;
    121 
    122     /** Returns the VM OS type. */
    123     //UIGuestOSTypeII type() const;
    124121
    125122    /** Passes the @p fError to QILineEdit::mark(bool) effectively marking it for error. */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette