source: trunk/Utilities/Miscellaneous/StaticConstructor/StaticConstructor.cpp @ 221

Last change on this file since 221 was 221, checked in by roman, 10 years ago

CAutoPtr version by Igor M.

File size: 2.0 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2008-2013
3// Created by Roman Ryltsov roman@alax.info
4
5#include "stdafx.h"
6#include <atlbase.h>
7#include <atlcom.h>
8
9class CModule :
10        public CAtlExeModuleT<CModule>
11{
12};
13
14class CFoo
15{
16public:
17        INT m_nValue;
18
19public:
20// CFoo
21        CFoo()
22        {
23                m_nValue++;
24                Sleep(100);
25                m_nValue++;
26        }
27        CFoo(INT nValue)
28        {
29                m_nValue = nValue;
30                Sleep(100);
31                m_nValue++;
32        }
33        CFoo(INT nValue1, INT nValue2)
34        {
35                m_nValue = nValue1 + nValue2;
36                Sleep(100);
37                m_nValue++;
38        }
39
40        ~CFoo()
41        {
42                _tprintf(_T("~CFoo()\n"));
43        }
44};
45
46#define STATICLOCAL_PART1(type, name) \
47        static type* g_p##name = NULL; \
48        { \
49                ATLASSERT(_pAtlModule); \
50                CComCritSecLock<CComCriticalSection> Lock(_pAtlModule->m_csStaticDataInitAndTypeInfo); \
51                static CAutoPtr<type> g_pPrivate##name; \
52                if(!g_p##name) \
53                { \
54                        g_pPrivate##name.Attach(new type(
55
56#define STATICLOCAL_PART2(type, name) \
57                        )); \
58                        g_p##name = (type*) g_pPrivate##name; \
59                } \
60        } \
61        type& g_##name = *g_p##name;
62
63#define STATICLOCAL0(type, name) \
64        STATICLOCAL_PART1(type, name) STATICLOCAL_PART2(type, name)
65#define STATICLOCAL1(type, name, parameter1) \
66        STATICLOCAL_PART1(type, name) parameter1 STATICLOCAL_PART2(type, name)
67#define STATICLOCAL2(type, name, parameter1, parameter2) \
68        STATICLOCAL_PART1(type, name) parameter1, parameter2 STATICLOCAL_PART2(type, name)
69
70DWORD WINAPI ThreadProc(INT_PTR*)
71{
72        #if TRUE
73                //STATICLOCAL0(CFoo, Foo);
74                //STATICLOCAL1(CFoo, Foo, 10);
75                STATICLOCAL2(CFoo, Foo, 20, 30);
76        #else
77                static CFoo g_Foo;
78        #endif
79        _tprintf(_T("Thread %05d, Value %02d\n"), GetCurrentThreadId(), g_Foo.m_nValue);
80        return 0;
81}
82
83int _tmain(int argc, _TCHAR* argv[])
84{
85        CModule Module;
86        HANDLE phObjects[32];
87        for(SIZE_T nIndex = 0; nIndex < _countof(phObjects); nIndex++)
88                phObjects[nIndex] = AtlCreateThread<INT_PTR>(&ThreadProc, 0);
89        WaitForMultipleObjects(_countof(phObjects), phObjects, TRUE, INFINITE);
90        return 0;
91}
92
Note: See TracBrowser for help on using the repository browser.