aboutsummaryrefslogtreecommitdiff
path: root/src/tests/sharedcore.cpp
blob: 381661a1737960750323abb0991877f43ccd9396 (plain)
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
#include "bu/sharedcore.h"
#include "bu/sio.h"

using namespace Bu;

struct ShintCore
{
	int val;
};
class Shint : public Bu::SharedCore<struct ShintCore>
{
public:
	Shint()
	{
		core->val = 0;
	}

	Shint( int val )
	{
		core->val = val;
	}

	int getVal() const
	{
		return core->val;
	}

	void setValBad( int val )
	{
		core->val = val;
	}
	
	void setVal( int val )
	{
		_hardCopy();
		core->val = val;
	}

	bool operator==( const Shint &rhs )
	{
		if( core == rhs.core )
		{
			sio << "Same pointer (" << Fmt::ptr() << core << ")" << sio.nl;
			return true;
		}
		if( core->val == rhs.core->val )
		{
			sio << "Same value " << core->val << " ("
				<< Fmt::ptr() << core << " vs "
				<< Fmt::ptr() << rhs.core << ")"
				<< sio.nl;
			return true;
		}
		sio << "Different" << sio.nl;
		return false;
	}
};

#define line( x ) sio << __FILE__ ": " << __LINE__ << ": " << #x << sio.nl; x

int main()
{
	line( Shint a; )
	line( Shint b( 5 ); )

	line( a == b; )

	line( b = a; )
	line( a == b; )

	line( b.setValBad( 12 ); )
	sio << a.getVal() << " != " << b.getVal() << sio.nl;
	line( a == b; )

	line( a.setVal( 3 ); )
	sio << a.getVal() << " != " << b.getVal() << sio.nl;
	line( a == b; )

	line( a.setVal( b.getVal() ); )
	line( a == b; )

	{
		Shint c( b );
		Shint d( c );
		sio << c.getVal();
		d.setVal( 43 );
	}

	sio << b.getVal();
}