File: main.cpp

package info (click to toggle)
libloki 0.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,608 kB
  • sloc: cpp: 30,475; ansic: 1,974; makefile: 365; php: 316; perl: 108
file content (264 lines) | stat: -rw-r--r-- 6,150 bytes parent folder | download | duplicates (5)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Copyright (c) 2006 Peter K�mmel
// Permission to use, copy, modify, distribute and sell this software for any 
//     purpose is hereby granted without fee, provided that the above copyright 
//     notice appear in all copies and that both that copyright notice and this 
//     permission notice appear in supporting documentation.
// The author makes no representations about the 
//     suitability of this software for any purpose. It is provided "as is" 
//     without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////

// $Id: main.cpp 895 2008-08-08 22:06:26Z syntheticpp $


#include <loki/ScopeGuard.h>

#include <vector>
#include <string>
#include <iostream>
#include <assert.h>

void HasNone( void )
{
}

void HasOne( int p1 )
{
    (void)p1;
}

void HasTwo( int p1, int p2 )
{
    (void)p1;
    (void)p2;
}

void HasThree( int p1, int p2, int p3 )
{
    (void)p1;
    (void)p2;
    (void)p3;
}

void HasFour( int p1, int p2, int p3, int p4 )
{
    (void)p1;
    (void)p2;
    (void)p3;
    (void)p4;
}

void HasFive( int p1, int p2, int p3, int p4, int p5 )
{
    (void)p1;
    (void)p2;
    (void)p3;
    (void)p4;
    (void)p5;
}

void Decrement( unsigned int & x ) 
{ 
    --x; 
}

struct UserDatabase
{
    void AddFriend(const std::string&, const std::string&)
    {
        throw 55;
    }
};

class User
{
public:
    User(UserDatabase* db) : fCount(0), pDB_(db)
    {}

    std::string GetName() const;

    void AddFriend(User& newFriend);
    void AddFriendGuarded(User& newFriend);
    void AddFriendGuardedMacros(User& newFriend);

    size_t countFriends() const;

	void DoSomething() const;
   
    unsigned int fCount;

    void HasNone( void ) const;
    void HasOne( int p1 ) const;
    void HasTwo( int p1, int p2 ) const;
    void HasThree( int p1, int p2, int p3 ) const;
    void HasFour( int p1, int p2, int p3, int p4 ) const;
    void HasFive( int p1, int p2, int p3, int p4, int p5 ) const;

private:
	void CheckIfValid( const char * function, unsigned int line ) const;

    typedef std::vector<User*> UserCont;
    UserCont friends_;
    UserDatabase* pDB_;
};

void User::HasNone( void ) const
{
}

void User::HasOne( int p1 ) const
{
    (void)p1;
}

void User::HasTwo( int p1, int p2 ) const
{
    (void)p1;
    (void)p2;
}

void User::HasThree( int p1, int p2, int p3 ) const
{
    (void)p1;
    (void)p2;
    (void)p3;
}

void User::HasFour( int p1, int p2, int p3, int p4 ) const
{
    (void)p1;
    (void)p2;
    (void)p3;
    (void)p4;
}

void User::HasFive( int p1, int p2, int p3, int p4, int p5 ) const
{
    (void)p1;
    (void)p2;
    (void)p3;
    (void)p4;
    (void)p5;
}

void User::DoSomething() const
{
}

void User::CheckIfValid( const char * function, unsigned int line ) const
{
	assert( friends_.size() == fCount );
	(void)function;
	(void)line;
}

std::string User::GetName() const
{
    return "A name";
}

size_t User::countFriends() const
{
    return friends_.size();
}

void User::AddFriend(User& newFriend)
{
	::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this,
		&User::CheckIfValid, __FUNCTION__, __LINE__ );
	(void)invariantGuard;
    friends_.push_back(&newFriend);
    fCount++;
    pDB_->AddFriend(GetName(), newFriend.GetName());
}

void User::AddFriendGuarded(User& newFriend)
{
	::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this,
		&User::CheckIfValid, __FUNCTION__, __LINE__ );
	(void)invariantGuard;
	::Loki::ScopeGuard guard1 = ::Loki::MakeObjGuard( *this, &User::DoSomething );
    (void)guard1;

    friends_.push_back(&newFriend);
    Loki::ScopeGuard guard = Loki::MakeObjGuard(friends_, &UserCont::pop_back);
    
    fCount++;
    Loki::ScopeGuard guardRef = Loki::MakeGuard(Decrement, Loki::ByRef(fCount));

    pDB_->AddFriend(GetName(), newFriend.GetName());
    guard.Dismiss();
    guardRef.Dismiss();
}

void User::AddFriendGuardedMacros(User&)
{
	::Loki::ScopeGuard invariantGuard = ::Loki::MakeObjGuard( *this,
		&User::CheckIfValid, __FUNCTION__, __LINE__ );
	(void)invariantGuard;
    LOKI_ON_BLOCK_EXIT_OBJ(friends_, &UserCont::pop_back); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
    // GCC 4.2 Bug
#if defined(__GNUC__)
#define GCC_VERSION (__GNUC__ * 10 + __GNUC_MINOR__ * 1)
#endif
#if !defined(__GNUC__) || (GCC_VERSION < 42) || (GCC_VERSION > 42)
    LOKI_ON_BLOCK_EXIT(Decrement, Loki::ByRef(fCount)); (void) LOKI_ANONYMOUS_VARIABLE(scopeGuard);
#endif
}

void DoStandaloneFunctionTests()
{
    ::Loki::ScopeGuard guard0 = ::Loki::MakeGuard( &HasNone );
    ::Loki::ScopeGuard guard1 = ::Loki::MakeGuard( &HasOne, 1 );
    ::Loki::ScopeGuard guard2 = ::Loki::MakeGuard( &HasTwo, 1, 2 );
    ::Loki::ScopeGuard guard3 = ::Loki::MakeGuard( &HasThree, 1, 2, 3 );
    ::Loki::ScopeGuard guard4 = ::Loki::MakeGuard( &HasFour, 1, 2, 3, 4 );
    ::Loki::ScopeGuard guard5 = ::Loki::MakeGuard( &HasFive, 1, 2, 3, 4, 5 );
    (void)guard0;
    (void)guard1;
    (void)guard2;
    (void)guard3;
    (void)guard4;
    (void)guard5;
}

void DoMemberFunctionTests( User & user )
{
    ::Loki::ScopeGuard guard0 = ::Loki::MakeObjGuard( user, &User::HasNone );
    ::Loki::ScopeGuard guard1 = ::Loki::MakeObjGuard( user, &User::HasOne,   1 );
    ::Loki::ScopeGuard guard2 = ::Loki::MakeObjGuard( user, &User::HasTwo,   1, 2 );
    ::Loki::ScopeGuard guard3 = ::Loki::MakeObjGuard( user, &User::HasThree, 1, 2, 3 );
    (void)guard0;
    (void)guard1;
    (void)guard2;
    (void)guard3;
}

int main()
{
    UserDatabase db;

    User u1(&db);
    User u2(&db);

    try{ u1.AddFriend(u2); }
    catch (...){}
    std::cout << "u1 countFriends: " << u1.countFriends() << "\n";
    std::cout << "u1 fCount      : " << u1.fCount << "\n";

    try{ u2.AddFriendGuarded(u1); }
    catch (...){}
    std::cout << "u2 countFriends: " << u2.countFriends() << "\n";
    std::cout << "u2 fCount      : " << u2.fCount << "\n";

    DoStandaloneFunctionTests();
    DoMemberFunctionTests( u1 );

#if defined(__BORLANDC__) || defined(_MSC_VER)
    system("PAUSE");
#endif

}