A
Alf P. Steinbach
Consider ...
<code>
// Copyright (c) Alf P. Steinbach, 2010.
#include "_config.h"
#include <progrock/cppx/pointers/Shared.h>
#include <iostream>
namespace {
using namespace progrock;
cppx::Size count = 0;
class Foo
{
protected:
~Foo()
{
using namespace std;
--count;
cout << "Foo destroyed" << endl;
}
public:
Foo()
{
using namespace std;
cout << "Foo constructed" << endl;
++count;
}
Foo( int x )
{
using namespace std;
cout << "Foo constructed with arg " << x << endl;
++count;
}
Foo( int x, char const s[] )
{
using namespace std;
cout << "Foo constructed with args " << x << " and \"" << s << "\""
<< endl;
++count;
}
};
void test()
{
using namespace cppx;
Shared< Foo > r1 = newObject();
assert( count == 1 );
Shared< Foo > r2( r1 );
assert( count == 1 );
Shared< Foo > r3( newObject(), args( 42 ) );
assert( count == 2 );
Shared< Foo > r4( newObject(), args( 42, "blah blah" ) );
assert( count == 3 );
r4 = r2;
assert( count == 2 );
r3 = r2;
assert( count == 1 );
r3 = r3;
assert( count == 1 );
}
}; // namespace anon
void testShared()
{
test();
assert( count == 0 );
}
</code>
Is the notation in the 'test' routine OK, or would it be better (in some sense)
with some macro, e.g. like
Shared< Foo > r( CPPX_NEW_SHARED( 42, "blah blah" ) );
?
Cheers,
- Alf (notation-aware)
<code>
// Copyright (c) Alf P. Steinbach, 2010.
#include "_config.h"
#include <progrock/cppx/pointers/Shared.h>
#include <iostream>
namespace {
using namespace progrock;
cppx::Size count = 0;
class Foo
{
protected:
~Foo()
{
using namespace std;
--count;
cout << "Foo destroyed" << endl;
}
public:
Foo()
{
using namespace std;
cout << "Foo constructed" << endl;
++count;
}
Foo( int x )
{
using namespace std;
cout << "Foo constructed with arg " << x << endl;
++count;
}
Foo( int x, char const s[] )
{
using namespace std;
cout << "Foo constructed with args " << x << " and \"" << s << "\""
<< endl;
++count;
}
};
void test()
{
using namespace cppx;
Shared< Foo > r1 = newObject();
assert( count == 1 );
Shared< Foo > r2( r1 );
assert( count == 1 );
Shared< Foo > r3( newObject(), args( 42 ) );
assert( count == 2 );
Shared< Foo > r4( newObject(), args( 42, "blah blah" ) );
assert( count == 3 );
r4 = r2;
assert( count == 2 );
r3 = r2;
assert( count == 1 );
r3 = r3;
assert( count == 1 );
}
}; // namespace anon
void testShared()
{
test();
assert( count == 0 );
}
</code>
Is the notation in the 'test' routine OK, or would it be better (in some sense)
with some macro, e.g. like
Shared< Foo > r( CPPX_NEW_SHARED( 42, "blah blah" ) );
?
Cheers,
- Alf (notation-aware)