boost::variant accessing the objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class GH_CollisionDetection;
class GH_HeadTracking;
struct application{
	std::string id;
	std::string path;
	boost::variant<GH_CollisionDetection *, GH_HeadTracking *> appObject;
};


class GH_BaseApp
{
public:
	GH_BaseApp();
	~GH_BaseApp();

private:
	void createApplications(void);
	void insertApplication(int id, application mApp);
	static std::map<int,application> applicationCollection;
	void runApplication(int id);
};


std::map<int,application> GH_BaseApp::applicationCollection;

void GH_BaseApp::createApplications()
{
	static std::auto_ptr<GH_CollisionDetection> collisionDetetcion(new GH_CollisionDetection(*this));
	application mApp;
	mApp.id = "Collision Detection";
	mApp.path = "Collision Detection Path";
	mApp.appObject = collisionDetetcion.get();
	insertApplication(1,mApp);


	static std::auto_ptr<GH_HeadTracking> headTracking(new GH_HeadTracking(*this));
	application mApp2;
	mApp2.id = "HeadTracker";
	mApp2.path = "Head Tracker Path";
	mApp2.appObject = headTracking.get();
	insertApplication(2,mApp2);
}

void GH_BaseApp::insertApplication(int id, application mApp)
{
	applicationCollection.insert(std::pair<int,application>(id,mApp));
}


void GH_BaseApp::runApplication(int id)
{
	std::map<int,application>::iterator appIt = applicationCollection.begin();
	appIt=applicationCollection.find(id);
	if(appIt!=applicationCollection.end())
	{
		std::cout<<"Found application "<<id<<" : "<<appIt->second.id<<std::endl;
		//HOW TO ACCESS MEMBER FUNCTIONS OF THE appObject ??
		//

		appIt->second.appObject.sayHi(); //!!!

		//more general appIt->second.appObject.myMemberFunction();
		//In the situations where this will be used I know what kind of application the variant contains.
		//eg in a collisionDetection case, I would like to be able to do : appIt->second.appObject->updateCollisionRays();
		//How can this be accomplished ?? 
	}
	else
	{
		std::cout<<"Application "<<id<<" not found!"<<std::endl;
	}
}



//ERROR I GET :
//using appIt->second.appObject->sayHi();
1>.\GH_BaseApp.cpp(199) : error C2819: type 'boost::variant<T0_,T1>' does not have an overloaded member 'operator ->'
1> with
1> [
1> T0_=GH_CollisionDetection *,
1> T1=GH_HeadTracking *
1> ]
1> did you intend to use '.' instead?
1>.\GH_BaseApp.cpp(199) : error C2039: 'sayHi' : is not a member of 'boost::variant<T0_,T1>'
1> with
1> [
1> T0_=GH_CollisionDetection *,
1> T1=GH_HeadTracking *
1> ]

//using appIt->second.appObject.sayHi();
1>GH_BaseApp.cpp
1>.\GH_BaseApp.cpp(199) : error C2039: 'sayHi' : is not a member of 'boost::variant<T0_,T1>'
1> with
1> [
1> T0_=GH_CollisionDetection *,
1> T1=GH_HeadTracking *
1> ]
Your second and third errors are because you cannot call
appObject.sayHi() directly. appObject is a boost::variant. You have to
get the value out of it first. You either need to write a visitor or use
get<>() which will require you to know at compile time the type that the
variant is currently holding.

http://www.boost.org/doc/libs/1_35_0/doc/html/variant/tutorial.html#variant.tutorial.basic
Topic archived. No new replies allowed.