summaryrefslogtreecommitdiff
path: root/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-31 14:27:26 +0800
committerchai <chaifix@163.com>2021-10-31 14:27:26 +0800
commit601442f94fc0dcfdc5a117c5f87d90b156d53045 (patch)
treeb006bcd6a28a965a900c64f4716007fcb45eee98 /ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp
parent26f05c6e3dcac9995345fb5a2b031be7e3ea79e9 (diff)
+static initiator
Diffstat (limited to 'ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp')
-rw-r--r--ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp
new file mode 100644
index 0000000..26ff046
--- /dev/null
+++ b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp
@@ -0,0 +1,76 @@
+// StaticConstructorSample.cpp : Defines the entry point for the console application.
+//
+#include "stdafx.h"
+#include <iostream>
+#include <string>
+
+// Sample Files:
+#include "MyClass.h"
+#include "MyTemplate.h"
+
+
+// Sample code for StartUp
+STATIC_STARTUP_CODE()
+{
+ std::cout << "Starting up..." << std::endl;
+}
+
+// Sample code for FinishUp
+STATIC_FINISHUP_CODE()
+{
+ std::cout << "Finishing up..." << std::endl;
+}
+
+
+// Template for testing each Type:
+template <typename Type>
+void TestType(const std::string& strType)
+{
+ using namespace std;
+
+ // Static Constructor should have been already invoked:
+ cout << "Show static data members before creating any instance of " << strType << ":" << endl;
+ cout << "String: " << Type::StaticStr() << endl;
+ cout << "PI: " << Type::PI() << endl;
+
+ // Temporary block:
+ // Destructor of local variable will be called at the end of this block.
+ {
+ Type myObj;
+ cout << "Show static data members after creating an instance of " << strType << ":" << endl;
+ cout << "String: " << myObj.StaticStr() << endl;
+ cout << "PI: " << myObj.PI() << endl;
+ }
+
+ cout << "Show static data members after destroying the instance of " << strType << ":" << endl;
+ cout << "String: " << Type::StaticStr() << endl;
+ cout << "PI: " << Type::PI() << endl;
+ cout << endl;
+}
+
+
+// Declaration of alias (class names) for the template instaces:
+typedef MyTemplate<int> MyTemplateInt;
+typedef MyTemplate<double> MyTemplateDouble;
+
+// Invoke the StaticConstructor & StaticDestructor for each one of the template instances:
+// Should be called with alias (class names) for the template instaces.
+INVOKE_STATIC_CONSTRUCTOR(MyTemplateInt);
+INVOKE_STATIC_CONSTRUCTOR(MyTemplateDouble);
+
+
+// Main:
+int _tmain(int argc, _TCHAR* argv[])
+{
+ // Static Constructors have already been called before entering this function...
+
+ // Run the same tests for each one of the classes:
+ TestType<MyClass>("MyClass");
+ TestType<MyTemplateInt>("MyTemplateInt");
+ TestType<MyTemplateDouble>("MyTemplateDouble");
+
+ // Static Destructors will be called after exiting this function...
+ return 0;
+}
+
+