Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
ThreadListener.h
Go to the documentation of this file.
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #pragma once
22 
23 /** @file
24  * @brief Interface for ThreadListener callback methods.
25  */
26 
27 #include "debug/CrtDbgInc.h"
28 #include "unicode/UString.h"
29 
30 namespace HFCore
31 {
32  /**
33  * @brief Interface for ThreadListener callback methods.
34  *
35  * <b>Implement listener methods as needed:</b>
36  * @code
37  * #pragma once
38  * #include "hffwk.h"
39  *
40  * class GameMain : public HFCore::Game,
41  * public HFCore::ThreadListener
42  * {
43  * public:
44  * typedef struct
45  * {
46  * int32_hf val1;
47  * int32_hf val2;
48  * } ThreadData;
49  *
50  * GameMain();
51  * virtual ~GameMain();
52  *
53  * // singleton:
54  * static GameMain *instance();
55  * static void destroy();
56  *
57  * // implementation of HFCore::Game:
58  * virtual void init();
59  * virtual void update(fp32_hf dt);
60  * virtual void draw(HFCore::Graphics *g);
61  * // and the rest of methods needed..
62  *
63  * // implementation of HFCore::ThreadListener:
64  * virtual void threadMain(HF_ThreadInfo *threadInfo);
65  *
66  * private:
67  * // singleton instance:
68  * static GameMain *gInstance;
69  * thread_hf mThreadID;
70  * HF_ThreadInfo *mThreadInfo;
71  * ThreadData *mThreadData;
72  * };
73  * @endcode
74  *
75  * <b>Spawn a new thread:</b>
76  * @code
77  * GameMain::~GameMain()
78  * {
79  * // make sure thread is done:
80  * envDebugLog("Waiting for thread to finish...\r\n");
81  * int32_hf threadRetVal = 0;
82  * HFCORE_ENV->waitThread(mThreadID, &threadRetVal);
83  * envDebugLog("Thread return value: %d\r\n", threadRetVal);
84  * // delete thread data:
85  * HF_SAFE_DELETE(mThreadInfo);
86  * HF_SAFE_DELETE(mThreadData);
87  * }
88  *
89  * void GameMain::createThread()
90  * {
91  * // init data for thread:
92  * mThreadInfo = new HF_ThreadInfo();
93  * assert(mThreadInfo != NULL);
94  * memset(mThreadInfo, 0, sizeof(HF_ThreadInfo));
95  *
96  * mThreadData = new ThreadData();
97  * assert(mThreadData != NULL);
98  * memset(mThreadData, 0, sizeof(ThreadData));
99  * mThreadData->val1 = 1001;
100  * mThreadData->val2 = 2002;
101  *
102  * mThreadInfo->id = 1;
103  * mThreadInfo->data = (void *)mThreadData;
104  *
105  * // spawn a new thread:
106  * mThreadID = HFCORE_ENV->createThread(mThreadInfo, this);
107  * }
108  *
109  * int32_hf GameMain::threadMain(HF_ThreadInfo *threadInfo)
110  * {
111  * ThreadData *threadData = (ThreadData *)threadInfo->data;
112  *
113  * for (int32_hf i = 6; i >= 0; i--)
114  * {
115  * envDebugLog("threadMain counting down: %d\r\n", i);
116  * HFCORE_ENV->sleep(100);
117  * }
118  *
119  * return (threadData->val1 + threadData->val2);
120  *}
121  * @endcode
122  */
124  {
125  public:
126  ThreadListener() {}
127  virtual ~ThreadListener() {}
128 
129  /**
130  * @brief This method is called just after the thread has been
131  * created using Environment::createThread() method.
132  *
133  * @param threadInfo Pointer to HF_ThreadInfo struct.
134  */
135  virtual int32_hf threadMain(HF_ThreadInfo *threadInfo) = 0;
136  };
137 }
Thread info struct used for HFCore::ThreadListener callbacks.
Definition: HFDataTypes.h:633
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
Interface for ThreadListener callback methods.
Interface for unicode string class UString.
Definition: Actor.h:34
virtual int32_hf threadMain(HF_ThreadInfo *threadInfo)=0
This method is called just after the thread has been created using Environment::createThread() method...