std::find_if

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

std::find_if

Postby theLizard » Thu Apr 28, 2016 6:54 pm

Relates to my Class Properties question http://bcbj.org/forums/viewtopic.php?f=10&t=3020&p=8936&sid=c660dcff5c6fe8df68bd509a3e9aa7c7&sid=c660dcff5c6fe8df68bd509a3e9aa7c7#p8936

I don't think there is a viable solution for this without lots of code so considering that the mountain won't come to me, I will have to go to the mountain.

The simplest way to achieve my goals is to make components from my classes, I have tested this theory and it is working with simple code

Code: Select all

if(dynamic_cast<TChartBase*>(html->GetObjects()[0]) != NULL)
   {
   TChartBase* b = dynamic_cast<TChartBase*>(html->GetObjects()[0]);
        //Do stuff
        }



In the code above, html->GetObjects()[0] is a vector of TObjects

The vector will contain objects of class types, since the vector may contain as many objects as required for the task, I need a way to find an object in the vector based on a property of the class type.

I would like to implement an std::find_if using generic code on the objects using code something similar to below

Code: Select all
struct IsObject
   {
   TObject* _object;
   IsObject(TObject* object) : _object(object) {}
   bool operator()(const IsObject * item)const
      {

      if(dynamic_cast<TChartBase*>(GetObjects()[0]) != NULL)

      return(item->??? == ???);
      }
   };



This code does not work and I think I know why but can someone suggest how I could get this to work.

GetObjects() returns a vector of objects, this is not recognized by the struct

I could get it to work by having a struct for each of my classes but I am sure there is a way to make one struct handle all (i hope)

Cheers
theLizard
BCBJ Master
BCBJ Master
 
Posts: 447
Joined: Wed Mar 18, 2009 2:14 pm

Re: std::find_if

Postby rlebeau » Mon May 09, 2016 2:28 pm

Your struct is not designed correctly. It should look more like this instead:

Code: Select all
struct HasBasePropertyValue
{
    const DataType &_Value;

    HasBasePropertyValue(const DataType &Value) : _Value(Value) {}

    bool operator()(const TObject * item) const
    {
        TChartBase *Base = dynamic_cast<TChartBase*>(item);
        if (Base != NULL)
            return (Base->SomeProperty == _Value);
        return false;
    }
};


Code: Select all
std::vector<TObject*> vec;
...
std::vector<TObject*>::iterator iter = std::find_if(vec.begin(), vec.end(), HasBasePropertyValue(SomeValue));
if (iter != vec.end())
{
    TObject *obj = *iter;
    ...
}


theLizard wrote:I could get it to work by having a struct for each of my classes but I am sure there is a way to make one struct handle all (i hope)


It can be done, but not easily. You would have to make the struct use templates, or resort to accessing Delphi-style RTTI.
Last edited by rlebeau on Mon May 09, 2016 3:56 pm, edited 2 times in total.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: std::find_if

Postby theLizard » Mon May 09, 2016 3:07 pm

Thanks Remy, very much appreciated.

You have given me a direction to follow, I will modify my structs as per your suggestion.

Cheers
theLizard
BCBJ Master
BCBJ Master
 
Posts: 447
Joined: Wed Mar 18, 2009 2:14 pm


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 12 guests