• Advertisement
Sign in to follow this  

'pass through member function'

This topic is 1274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I was wondering if there's s a clean way to do this:

 

- assume we have class A and B

- class B has a member 'SetThingie(const int pValue)'

- class A has a vector of class B

- class A has a 'current B' int

- class A has a member 'SetThingie(const int pValue)', same as class B

- when class A SetThingie is called, it should call SetThingie of the class B object in the vector, with index 'current B'

 

Now the most straigt forward you would be something like this:

class ClassA
{
public:
    void SetThingie(const int pValue);

private:
    int currentIndex;
    std::vector<ClassB> mObjects;
}

class ClassB
{
public:
    void SetThingie(const int pValue);
}

void ClassA::SetThingie(const int pValue)
{
    mObjects[currentIndex].SetIndex(pValue);
}

Do you think there are easier/ cleaner ways to do this?

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement

Hi.

Class A is the ShaderManager class, class B is a individual effect.

I want to be able to set shader constants through the ShaderManager.

 

In pseudo code:

 

ShaderManager->SetCurrentPermutation(5);

ShaderManager->SetWorldMatrix(myMatrix);

 

The SetWorldMatrix should call the SetWorldMatrix of the effect class,

ShaderManager class has a std::vector<EffectClass>

 

I don't think it's a flyweight pattern, because the member functions are not 100% the same (the ShaderManager class functions simply call the similar named function of the Effect class (for the 'current effect' = index of the vector of effect objects)

Share this post


Link to post
Share on other sites

Seems reasonable to me. Is there something about this approach that you're unhappy with? One can over-think things like this.

Share this post


Link to post
Share on other sites
You could treat Class A as an iterator:
class B;
class A {
public:
	B& operator*() {
		return vec[idx];
	}

	B* operator->() {
		return &vec[idx];
	}
private:
	int idx;
	vector<B> vec;
};

class B {
public:
	void frobnicate();
};

void test(A& a) {
	(*a).frobnicate();
	a->frobnicate();
}

Share this post


Link to post
Share on other sites

If you are wanting to directly call arbitrary member functions on every object, you can pass in member functions as function pointers:

#include <iostream>
#include <vector>

class ClassB
{
public:
    void SetThingie(const std::string &str);
    void DoSomething(float f, int i);
};

class ClassA
{
public:
	ClassA(int objCount)
	{
		this->objects.resize(objCount);
	}
	
	template<typename MemberFunc, typename ...Args>
	typename std::enable_if<std::is_member_function_pointer<MemberFunc>::value, void>::type
    CallMemberFuncOnObjects(MemberFunc memberFunc, Args&& ...args)
    {
    	int id = 0;
    	
    	for(auto &object : this->objects)
    	{
    		std::cout << "ClassB #" << ++id << " = ";
    		(object.*memberFunc)( args... );
    	}
    	
    	std::cout << std::endl;
    }

private:
    std::vector<ClassB> objects;
};

void ClassB::SetThingie(const std::string &str)
{
    std::cout << str << std::endl;
}

void ClassB::DoSomething(float f, int i)
{
	std::cout << "(" << f << ", " << i << ")" << std::endl;
}

int main()
{
	ClassA myClassA(5);
	
	myClassA.CallMemberFuncOnObjects(&ClassB::SetThingie, "Hello world!");
	myClassA.CallMemberFuncOnObjects(&ClassB::DoSomething, 3.14f, 357);
	
	return 0;
}

[Ideone test code]

 

The same thing works with just a single object in the array (using currentIndex), but that wouldn't serve much point because you could just access the object and call the function directly, as fastcall22 pointed out.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

Hi,

 

I was wondering if there's s a clean way to do this:

 

- assume we have class A and B

- class B has a member 'SetThingie(const int pValue)'

- class A has a vector of class B

- class A has a 'current B' int

- class A has a member 'SetThingie(const int pValue)', same as class B

- when class A SetThingie is called, it should call SetThingie of the class B object in the vector, with index 'current B'

 

Now the most straigt forward you would be something like this:

class ClassA
{
public:
    void SetThingie(const int pValue);

private:
    int currentIndex;
    std::vector<ClassB> mObjects;
}

class ClassB
{
public:
    void SetThingie(const int pValue);
}

void ClassA::SetThingie(const int pValue)
{
    mObjects[currentIndex].SetIndex(pValue);
}

Do you think there are easier/ cleaner ways to do this?

 

What about intefacing the Effect with Shader?

class ShaderBase {
public:
    
    virtual void setWorldMatrix(MATRIX &matrix) = 0; //-- For interface class.
     void SetIndex(int &value);
     std::vector<ShaderBase> ShaderArray;
     int currentIndex;
private:

};

class Effect : public ShaderBase {
  public:
 
  void setWorldMatrix(MATRIX &matrix); //-- Because shader base has a function called setWorldMatrix(MATRIX &) you overwrite the virtual function in ShaderBase 
   
private:
}

Or are you talking about using inheritences in classes? I thought interfacing was what you're looking for because of the SetThingy thingy thing example.

Share this post


Link to post
Share on other sites

Wow, seems that that are other ways to do this.

To be honest, I'm not sure if they're improving readability of the code, and in the end maybe the most straight forward way will be the way to go.

 

The last solution although also looks interesting, but I believe this will need moving around (const) ref's when just need to set a shader constant (For each constant). So for no I'll go for the straight forward option.

 

Thanks again for sketching the options

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement