mardi 10 septembre 2019

How to make the current coding structure more flexible

I need help with my current workflow , though everthing works as expected but i would like to make the structure more robust.

I have a application where i change the data by string literal.

WAVEFRONT*TREEVIEW*Main$Text*GEOMETRY*TEXT SET TEXT ABCD

as shown in the image where "Main$Text" is the path of the data item the the tree structure.

enter image description here

1) First step-> i have a class to handle the command string and take appropriate action.

int SumCommandInterface::receiveCommand(std::string stdtsrCommand , const QModelIndex & RootIndex, TreeModel *myModel)
{   
    std::vector<std::string> vectorStrCommand;
    std::vector<std::string> vectorStrStarSplit;
    std::vector<std::string> vectorStrContainerNames;   
    boost::algorithm::trim(stdtsrCommand);
    boost::split(vectorStrCommand, stdtsrCommand, boost::is_any_of(" "), boost::token_compress_on);
    boost::split(vectorStrStarSplit, vectorStrCommand[0], boost::is_any_of("*"), boost::token_compress_on); 
    if (vectorStrStarSplit[1] == "TREEVIEW")  // for tree view
    {
        if (vectorStrCommand.size() < 4)
            return -1;

        boost::split(vectorStrContainerNames, vectorStrStarSplit[2], boost::is_any_of("$"), boost::token_compress_on);
        TreeItem* itm;
        QModelIndex  Index = RootIndex;
        for (auto &strName : vectorStrContainerNames)
        {
            if (!getTreeItem(strName, Index, myModel, &itm))
            {
                return 0;
            }
            Index = myModel->indexForTreeItem(itm);         
        }
        PluginTypeLookUp lookUp = lookUpPluginMap[vectorStrStarSplit[3]];
        Container *cont = itm->GetContainer();
        switch (lookUp)
        {
        case PluginTypeLookUp::GEOMTERY: // This can be function , texture etc, currently only showing code for geometry
        {
            Geometry *geom = cont->GetGeometry();
            if(vectorStrCommand[2] == "TEXT" )
                setFontPluginParam< Geometry , std::string >( geom, vectorStrCommand[2], vectorStrCommand[3]);

            if (vectorStrCommand[2] == "USESHADOW")
                setFontPluginParam< Geometry , bool >(geom, vectorStrCommand[2], (vectorStrCommand[3] == "0" ? false : true) );

            if (vectorStrCommand[2] == "FONTSIZE" || vectorStrCommand[2] ==  "SHADOWDIRECTION")
                setFontPluginParam< Geometry, int >(geom, vectorStrCommand[2], std::stoi(vectorStrCommand[3]) );

            if (vectorStrCommand[2] == "SHADOWDISTANCE" || vectorStrCommand[2] == "SHADOWOPACITY" || vectorStrCommand[2] == "KERNING" )
                setFontPluginParam< Geometry, float >(geom, vectorStrCommand[2], std::stof(vectorStrCommand[3]));

        }
        break;
        case PluginTypeLookUp::TEXTURE:
        {
            Sum_Texture_2D *texture;
            texture = cont->GetTexturePointer();

            if(vectorStrCommand[2] == "TEXTUREPATH" )
                setTexturePluginParam< Sum_Texture_2D, std::string >(texture, vectorStrCommand[2], vectorStrCommand[3]);

            if (vectorStrCommand[2] == "POSITIONX" || vectorStrCommand[2] ==  "POSITIONY" || vectorStrCommand[2] ==  "POSITIONZ" || vectorStrCommand[2] ==  "SCALINGX" || vectorStrCommand[2] ==  "SCALINGY" || vectorStrCommand[2] ==  "SCALINGZ" || vectorStrCommand[2] ==  "ROTATIONX" || vectorStrCommand[2] ==  "ROTATIONY" || vectorStrCommand[2] ==  "ROTATIONZ ")
            {
                setTexturePluginParam< Sum_Texture_2D, float >(texture, vectorStrCommand[2], std::stof(vectorStrCommand[3]));
            }

            break;
        }
        default:
            break;
        }
        return 1;
    }   
    return 0;
}

Currently i am using a lookup map to check what geometry type is , for some other geometry the command will look like.

WAVEFRONT*TREEVIEW*AMain$Text*GEOMETRY*SPHERE SET SEGMENTS 32

so i would need this step for SPHERE

if(vectorStrCommand[2] == "SPHERE" )
                    setFontPluginParam< Geometry , std::string >( geom, vectorStrCommand[2], vectorStrCommand[3])

This step for TEXT

if(vectorStrCommand[2] == "TEXT" )
                    setFontPluginParam< Geometry , std::string >( geom, vectorStrCommand[2], vectorStrCommand[3])

The current issue i am facing is that if i have 30 different geometry i would need to add 30 if conditions.

can i handle this step in more graceful way.

Aucun commentaire:

Enregistrer un commentaire