Jump to content

  • Log In with Google      Sign In   
  • Create Account

'pass through member function'


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 cozzie   Members   -  Reputation: 1612

Like
0Likes
Like

Posted 26 July 2014 - 02:12 AM

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, 26 July 2014 - 02:12 AM.


Sponsor:

#2 Felix Ungman   Members   -  Reputation: 1034

Like
0Likes
Like

Posted 26 July 2014 - 03:11 AM

Could you elaborate on what these names represent, what exactly is the role of a ClassA, ClassB and a Thingie?

 

Is this an implementation of the http://en.wikipedia.org/wiki/Flyweight_pattern ?


openwar  - the real-time tactical war-game platform


#3 cozzie   Members   -  Reputation: 1612

Like
0Likes
Like

Posted 26 July 2014 - 08:50 AM

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)



#4 rip-off   Moderators   -  Reputation: 8222

Like
3Likes
Like

Posted 26 July 2014 - 12:15 PM

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



#5 fastcall22   Crossbones+   -  Reputation: 4333

Like
1Likes
Like

Posted 26 July 2014 - 01:41 PM

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();
}

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#6 cozzie   Members   -  Reputation: 1612

Like
0Likes
Like

Posted 26 July 2014 - 02:05 PM

Thanks, maybe I am overthinking this.

@belfegor: what do you mean?

#7 belfegor   Crossbones+   -  Reputation: 2615

Like
0Likes
Like

Posted 26 July 2014 - 02:20 PM

I wanted to suggest CRTP but realize it could not work with a vector.



#8 cozzie   Members   -  Reputation: 1612

Like
0Likes
Like

Posted 26 July 2014 - 02:52 PM

Ah ok, thanks

#9 Servant of the Lord   Crossbones+   -  Reputation: 19556

Like
0Likes
Like

Posted 26 July 2014 - 03:18 PM

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, 26 July 2014 - 03:24 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 26 July 2014 - 08:03 PM

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.


Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#11 cozzie   Members   -  Reputation: 1612

Like
1Likes
Like

Posted 27 July 2014 - 02:15 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS