ObjectTest.cpp 2.29 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 4 5 6 7 8 9
#include <VAException.h>
#include <VAObject.h>
#include <VAObjectRegistry.h>
#include <VAStruct.h>

#include <iostream>

using namespace std;

10 11
class TestObject : public CVAObject
{
Jonas Stienen's avatar
Jonas Stienen committed
12
public:
13 14 15
	inline TestObject() : CVAObject( "TestObject" )
	{
	};
Jonas Stienen's avatar
Jonas Stienen committed
16

17
	inline CVAStruct CallObject( const CVAStruct& ) { return CVAStruct(); };
Jonas Stienen's avatar
Jonas Stienen committed
18

19 20
	inline int HandleMessage( const CVAStruct* pArgumentMessage, CVAStruct* pReturnMessage )
	{
Jonas Stienen's avatar
Jonas Stienen committed
21
		// No arguments => Do nothing
22 23
		if( !pArgumentMessage ) 
			return 0;
Jonas Stienen's avatar
Jonas Stienen committed
24

25
		const CVAStructValue* pCommand = pArgumentMessage->GetValue( "command" );
Jonas Stienen's avatar
Jonas Stienen committed
26 27

		// No command => Do nothing
28 29
		if( !pCommand ) 
			return 0;
Jonas Stienen's avatar
Jonas Stienen committed
30 31

		// Command must be a string
32 33
		if( !pCommand->IsString() )
			return -1;
Jonas Stienen's avatar
Jonas Stienen committed
34 35

		std::string sCommand = *pCommand;
36 37
		for( auto & c : sCommand ) 
			c = char( toupper( c ) );
Jonas Stienen's avatar
Jonas Stienen committed
38

39 40
		if( sCommand == "SAYHELLO" )
		{
Jonas Stienen's avatar
Jonas Stienen committed
41 42 43 44 45 46
			cout << "Object \"" << GetObjectName() << "\" says hello!" << endl;

			// No return values
			return 0;
		}

47 48 49
		if( sCommand == "PRINTID" )
		{
			cout << "Object \"" << GetObjectName() << "\" has ID " << GetObjectID() << endl;
Jonas Stienen's avatar
Jonas Stienen committed
50 51 52 53 54

			// No return values
			return 0;
		}

55 56
		if( sCommand == "TEST" )
		{
Jonas Stienen's avatar
Jonas Stienen committed
57 58
			cout << "Object \"" << GetObjectName() << "\" was called >test< " << endl;
			pReturnMessage->Clear();
59
			( *pReturnMessage )[ "Number" ] = 4711;
Jonas Stienen's avatar
Jonas Stienen committed
60 61 62 63 64
			return pReturnMessage->Size();
		}

		// Invalid command
		return -1;
65
	};
Jonas Stienen's avatar
Jonas Stienen committed
66 67
};

68 69 70 71
int main( int, char** )
{
	try
	{
Jonas Stienen's avatar
Jonas Stienen committed
72 73 74
		CVAObjectRegistry reg;
		TestObject obj;

75
		reg.RegisterObject( &obj );
Jonas Stienen's avatar
Jonas Stienen committed
76 77 78
		cout << "Test object registered. Got ID " << obj.GetObjectID() << endl;

		// Search for object
79 80
		CVAObject* pObj = reg.GetObjectByName( "TestObject" );
		cout << pObj << endl;
Jonas Stienen's avatar
Jonas Stienen committed
81 82 83 84

		CVAStruct in, out;
		int result;

85 86
		in[ "command" ] = "SayHello";
		result = obj.HandleMessage( &in, &out );
Jonas Stienen's avatar
Jonas Stienen committed
87
		cout << "Result = " << result << endl;
88
		if( result != 0 )
Jonas Stienen's avatar
Jonas Stienen committed
89 90
			cout << "Return message = " << out << endl;

91 92
		in[ "command" ] = "PrintID";
		result = obj.HandleMessage( &in, &out );
Jonas Stienen's avatar
Jonas Stienen committed
93
		cout << "Result = " << result << endl;
94
		if( result != 0 )
Jonas Stienen's avatar
Jonas Stienen committed
95 96
			cout << "Return message = " << out << endl;

97 98
		in[ "command" ] = "Test";
		result = obj.HandleMessage( &in, &out );
Jonas Stienen's avatar
Jonas Stienen committed
99
		cout << "Result = " << result << endl;
100
		if( result != 0 )
Jonas Stienen's avatar
Jonas Stienen committed
101 102
			cout << "Return message = " << out << endl;

103 104
	}
	catch( CVAException& e ) {
Jonas Stienen's avatar
Jonas Stienen committed
105 106 107 108 109 110
		cerr << "Error: " << e << endl;
		return e.GetErrorCode();
	}

	return 0;
}