Interface based programming in C++

I am having problems with interface based programming. I have recently found out that stl containers cannot store abstract classes ( eg. vector<ISystem> ). I could use pointers but i have to delete them. Then I thought of using auto_ptr. This is where I am having problems.

This is the result of my compilation:

Producing runner.cpp
perl C:\cxxtest/cxxtestgen.pl -o runner.cpp --error-print test/SystemEventHandlerTestSuite.h
Producing object files
g++ -c -o bin/debug/runner.o -IC:\cxxtest -Itest -Isrc  runner.cpp
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/mingw32/bits/c++allocator.h:34:0,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/allocator.h:48,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/string:43,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/locale_classes.h:42,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/ios_base.h:43,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ios:43,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/istream:40,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/sstream:39,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/complex:47,
                 from C:\cxxtest/cxxtest/StdHeaders.h:13,
                 from C:\cxxtest/cxxtest/StdValueTraits.h:11,
                 from C:\cxxtest/cxxtest/ValueTraits.h:344,
                 from C:\cxxtest/cxxtest/TestSuite.h:13,
                 from C:\cxxtest/cxxtest/RealDescriptions.h:9,
                 from C:\cxxtest/cxxtest/TestRunner.h:11,
                 from runner.cpp:10:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ext/new_allocator.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>, _Tp* = std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/vector.tcc:113:4:   instantiated from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>, _Alloc = std::allocator<std::auto_ptr<PSHUygulamasi::ISystemEvntMsg> >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>*, std::vector<std::auto_ptr<PSHUygulamasi::ISystemEvntMsg> > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>*, value_type = std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>]'
test/SystemEventHandlerTestSuite.h:83:25:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ext/new_allocator.h:105:9: error: passing 'const std::auto_ptr<PSHUygulamasi::ISystemEvntMsg>' as 'this' argument of 'std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = PSHUygulamasi::ISystemEvntMsg, _Tp = PSHUygulamasi::ISystemEvntMsg]' discards qualifiers
.
.
.

make: *** [runner.o] Error 1


and this is the code:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * SystemEventHandlerTestSuite.h
 *
 *  Created on: 28 Nis 2011
 *      Author: Ahmet
 */

#ifndef SYSTEMEVENTHANDLERTESTSUITE_H_
#define SYSTEMEVENTHANDLERTESTSUITE_H_

#include <cxxtest/TestSuite.h>
#include "../src/CSystemEventHandler.h"
#include "../src/ISystemObserver.h"
#include "../src/CBaseSystemEvntMsg.h"
#include "MockSVC.h"

using namespace PSHUygulamasi;

class SystemEventHandlerTestSuite: public CxxTest::TestSuite {
		class MockObserver: public PSHUygulamasi::ISystemObserver {
			private:
				vector<auto_ptr<PSHUygulamasi::ISystemEvntMsg> > expectedMessages;

			public:
				~MockObserver() {
					expectedMessages.erase( expectedMessages.begin(),
							expectedMessages.end() );
				}

				void update( vector<auto_ptr<ISystemEvntMsg> > messages ) {
					vector<auto_ptr<ISystemEvntMsg> >::const_iterator it;
					vector<auto_ptr<ISystemEvntMsg> >::const_iterator itExp;

					TS_ASSERT_EQUALS( messages.size(), expectedMessages.size() );
					for( it = messages.begin(), itExp = expectedMessages.begin(); it
							< messages.end(); ++it, ++itExp ) {
						TS_ASSERT_EQUALS( (*it)->getTime(), (*itExp)->getTime() );
						TS_ASSERT_EQUALS( (*it)->getType(), (*itExp)->getType() );
					}
				}

				void addExpectedMessages( const vector<auto_ptr<ISystemEvntMsg> > &messages ) {
					expectedMessages = messages;
				}

		};

	protected:
		MockSVC *svc;
	public:
		void setUp() {
			svc = new MockSVC;
		}
		void tearDown() {
			delete svc;
		}

		void test_WaitEvent() {
			svc->setNextEvent( EVT_COM1 );
			TS_ASSERT_EQUALS( svc->wait_event(), EVT_COM1 );
		}

		void test_ReadClock() {
			char *zaman;

			svc->setNextTime( "201112030446465" );
			TS_ASSERT_EQUALS( svc->read_clock( zaman ), 0 );
			TS_ASSERT_SAME_DATA( zaman, "201112030446465", 15 );
		}

		void test_ObserverNotify() {
			CSystemEventHandler eventHandler;
			PSHUygulamasi::ISystemObserver *observer;
			vector<auto_ptr<ISystemEvntMsg> > messages;

			observer = new MockObserver;
			eventHandler.registerObserver( auto_ptr<PSHUygulamasi::ISystemObserver> ( observer ) );

			svc->setNextTime( "201104280313405" );
			svc->setNextEvent( EVT_ACTIVATE | EVT_BAR | EVT_CLK );

			messages.insert( messages.end(), auto_ptr<ISystemEvntMsg>(new CBaseSystemEvntMsg( "201104280313405",
					"EVT_ACTIVATE" ) ) );
			messages.insert( messages.end(), auto_ptr<ISystemEvntMsg>( new CBaseSystemEvntMsg( "201104280313405",
					"EVT_BAR" ) ) );
			messages.insert( messages.end(), auto_ptr<ISystemEvntMsg>( new CBaseSystemEvntMsg( "201104280313405",
					"EVT_CLK" ) ) );
			(dynamic_cast<MockObserver *>(observer))->addExpectedMessages( messages );
			eventHandler.run();
		}
};

#endif /* SYSTEMEVENTHANDLERTESTSUITE_H_ */
Topic archived. No new replies allowed.