File: base.h

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 (196 lines) | stat: -rw-r--r-- 4,588 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
////////////////////////////////////////////////////////////////////////////////
// Test program for The Loki Library
// Copyright (c) 2006 Richard Sposato
// 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 authors make no representations about the 
//     suitability of this software for any purpose. It is provided "as is" 
//     without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////

// $Id: base.h 761 2006-10-17 20:48:18Z syntheticpp $

#include <assert.h>


// ----------------------------------------------------------------------------

class BaseClass
{
public:
    BaseClass( void )
    {
        s_constructions++;
    }

    virtual ~BaseClass( void )
    {
        s_destructions++;
    }

    // These 2 functions are so we can pretend we have a COM object.
    void AddRef( void ) {}
    void Release( void ) {}

    // This function is used only for the DeepCopy policy.
    virtual BaseClass * Clone( void ) const
    {
        return new BaseClass();
    }

    void DoThat( void ) const {}

    static inline bool AllDestroyed( void )
    {
        return ( s_constructions == s_destructions );
    }

    static inline bool ExtraConstructions( void )
    {
        return ( s_constructions > s_destructions );
    }

    static inline bool ExtraDestructions( void )
    {
        return ( s_constructions < s_destructions );
    }

    static inline unsigned int GetCtorCount( void )
    {
        return s_constructions;
    }

    static inline unsigned int GetDtorCount( void )
    {
        return s_destructions;
    }

private:
    /// Not implemented.
    BaseClass( const BaseClass & );
    /// Not implemented.
    BaseClass & operator = ( const BaseClass & );

    static unsigned int s_constructions;
    static unsigned int s_destructions;
};


// ----------------------------------------------------------------------------

class PublicSubClass : public BaseClass
{
public:
    // This function is used only for the DeepCopy policy.
    virtual BaseClass * Clone( void ) const
    {
        return new PublicSubClass;
    }
};

// ----------------------------------------------------------------------------

class PrivateSubClass : private BaseClass
{
public:
    // This function is used only for the DeepCopy policy.
    virtual BaseClass * Clone( void ) const
    {
        return new PrivateSubClass;
    }
};


// ----------------------------------------------------------------------------

/** @class MimicCOM Acts like a COM object by having an intrusive ref count.
 */
class MimicCOM
{
public:

    static MimicCOM * QueryInterface( void )
    {
        MimicCOM * p = new MimicCOM;
        p->AddRef();
        return p;
    }

    virtual ~MimicCOM( void )
    {
        s_destructions++;
    }

    void AddRef( void )
    {
        m_count++;
        m_AddRefCount++;
    }

    void Release( void )
    {
        m_ReleaseCount++;
        assert( 0 < m_count );
        m_count--;
        if ( 0 == m_count )
        {
            /** @note I consider "delete this;" to be very unsafe!  I'm only
             using it here for the purpose of testing.
             */
            delete this;
        }
    }

    void DoThat( void ) {}

    static inline bool AllDestroyed( void )
    {
        return ( s_constructions == s_destructions );
    }

    static inline bool ExtraConstructions( void )
    {
        return ( s_constructions > s_destructions );
    }

    static inline bool ExtraDestructions( void )
    {
        return ( s_constructions < s_destructions );
    }

    static inline unsigned int GetCtorCount( void )
    {
        return s_constructions;
    }

    static inline unsigned int GetDtorCount( void )
    {
        return s_destructions;
    }

private:
    /// Not implemented.
    MimicCOM( const MimicCOM & );
    /// Not implemented.
    MimicCOM & operator = ( const MimicCOM & );

    MimicCOM( void )
        : m_count( 0 )
        , m_AddRefCount( 0 )
        , m_ReleaseCount( 0 )
    {
        s_constructions++;
    }

    static unsigned int s_constructions;
    static unsigned int s_destructions;

    unsigned int m_count;
    unsigned int m_AddRefCount;
    unsigned int m_ReleaseCount;
};

// ----------------------------------------------------------------------------