~stolowski/unity-scopes-api/canned-query-data

« back to all changes in this revision

Viewing changes to include/unity/api/scopes/ScopeBase.h

  • Committer: Michi Henning
  • Date: 2013-05-29 04:57:50 UTC
  • Revision ID: michi.henning@canonical.com-20130529045750-gwultik6nxtd7yb9
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#ifndef UNITY_API_SCOPES_SCOPEBASE_H
 
20
#define UNITY_API_SCOPES_SCOPEBASE_H
 
21
 
 
22
#include <unity/util/NonCopyable.h>
 
23
#include <unity/api/scopes/Version.h>
 
24
 
 
25
#include <memory>
 
26
 
 
27
namespace unity
 
28
{
 
29
 
 
30
namespace api
 
31
{
 
32
 
 
33
namespace scopes
 
34
{
 
35
 
 
36
/**
 
37
\file ScopeBase.h
 
38
\class ScopeBase
 
39
\brief Base class for a scope implementation.
 
40
 
 
41
Scopes are provided to the Unity run time by providing them in a shared library (one library per scope).
 
42
Each scope must implement a class that derives from ScopeBase, for example:
 
43
 
 
44
~~~
 
45
#include <unity/api/scopes/ScopeBase.h>
 
46
 
 
47
class MyScope : public unity::api::scopes::ScopeBase
 
48
{
 
49
public:
 
50
    MyScope();
 
51
    virtual ~MyScope();
 
52
 
 
53
    virtual void initialize();
 
54
    virtual void finalize();
 
55
 
 
56
    virtual void start();
 
57
    virtual void stop();    // Optional
 
58
};
 
59
~~~
 
60
 
 
61
The derived class must provide implementations of the pure virtual methods initialize(), finalize(), and start();
 
62
In addition, the librarya must provide two functions with "C" linkage. The create function must return a pointer to
 
63
the derived instance, which the Unity run time passes back to the destroy function before the scope library is
 
64
unloaded. Typically, the create and destroy functions will simply call new and delete, respectively. (However,
 
65
there is no requirement that the derived class instance must be heap allocated.
 
66
 
 
67
Rather than hard-coding the names of the functions, use the #UNITY_API_SCOPE_CREATE_FUNCTION and
 
68
#UNITY_API_SCOPE_DESTROY_FUNCTION macros, for example:
 
69
 
 
70
~~~
 
71
unity::api::scopes::ScopeBase*
 
72
UNITY_API_SCOPE_CREATE_FUNCTION()
 
73
{
 
74
    return new MyScope; // Example only, heap-allocation is not mandatory
 
75
}
 
76
 
 
77
void
 
78
UNITY_API_SCOPE_DESTROY_FUNCTION(unity::api::scopes::ScopeBase* scope)
 
79
{
 
80
    delete scope;       // Example only, heap-allocation is not mandatory
 
81
}
 
82
~~~
 
83
 
 
84
After the Unity run time has obtained a pointer to the class instance from the create function, it calls initialize(),
 
85
followed by start(), which passes the thread of control to the scope implementation. When the scope should complete
 
86
its activities, the run time calls stop(). Once stop() (which may be overridden by the derived class) returns,
 
87
the run time calls finalize(). The calls to initialize(), start(), and finalize() are made by the same thread.
 
88
(However, stop() is called by a different thread.)
 
89
Once finalize() completes, the run time calls the destroy function and then unloads the library from memory.
 
90
 
 
91
The implementation of the scope is free to use the thread that calls start() in any way it deems fit, such as
 
92
to run an event loop. If the implementation of start() does not need to use the thread calling start(),
 
93
the implementation can call run(). run() blocks the calling thread until the run time has called stop()
 
94
and stop() has completed, at which point run() unblocks the calling thread and returns.
 
95
The derived class can override stop() to, for example, arrange for its event loop to terminate and join with
 
96
any threads it has created.
 
97
*/
 
98
 
 
99
namespace internal
 
100
{
 
101
class ScopeBaseImpl;
 
102
}
 
103
 
 
104
class UNITY_API ScopeBase : private util::NonCopyable
 
105
{
 
106
public:
 
107
    /// @cond
 
108
    virtual ~ScopeBase();
 
109
    /// @endcond
 
110
 
 
111
    /**
 
112
    Called by the Unity run time after loading the scope's library. If initialize() throws an exception,
 
113
    start() and finalize() will _not_ be called.
 
114
    */
 
115
    virtual void initialize() = 0;
 
116
 
 
117
    /**
 
118
    Called by the Unity run time after stop() completes. Exceptions from finalize() are ignored.
 
119
    */
 
120
    virtual void finalize() = 0;
 
121
 
 
122
    /**
 
123
    Called by the Unity run time after initialize() completes. If start() throws an exception, stop()
 
124
    will _not_ be called (but finalize() _will_ be called).
 
125
    */
 
126
    virtual void start() = 0;
 
127
 
 
128
    /**
 
129
    Called by the Unity run time when the scope should shut down. Exceptions from stop() are ignored.
 
130
    */
 
131
    virtual void stop();
 
132
 
 
133
    /**
 
134
    This method suspends the calling thread, so run() blocks the caller. The call completes after the Unity
 
135
    run time has called stop() and stop() has returned.
 
136
    */
 
137
    void run() noexcept;
 
138
 
 
139
    /**
 
140
    This method returns the version information for the scopes API that the scope was compiled with.
 
141
    */
 
142
    void compiled_version(int& v_major, int& v_minor, int& v_micro) noexcept;
 
143
 
 
144
    /**
 
145
    This method returns the version information for the scopes API that the scope was linked with.
 
146
    */
 
147
    void runtime_version(int& v_major, int& v_minor, int& v_micro) noexcept;
 
148
 
 
149
protected:
 
150
    /// @cond
 
151
    ScopeBase();
 
152
    /// @endcond
 
153
 
 
154
private:
 
155
    std::unique_ptr<internal::ScopeBaseImpl> p_;
 
156
};
 
157
 
 
158
// We inline this function so we are guaranteed to be returned the version of the scopes library that the derived
 
159
// class was compiled with.
 
160
 
 
161
inline
 
162
void
 
163
ScopeBase::
 
164
compiled_version(int& v_major, int& v_minor, int& v_micro) noexcept
 
165
{
 
166
    v_major = UNITY_SCOPES_VERSION_MAJOR;
 
167
    v_minor = UNITY_SCOPES_VERSION_MINOR;
 
168
    v_micro = UNITY_SCOPES_VERSION_MICRO;
 
169
}
 
170
 
 
171
} // namespace scopes
 
172
 
 
173
} // namespace api
 
174
 
 
175
} // namespace unity
 
176
 
 
177
/**
 
178
Expands to the identifier of the scope creation function. @hideinitializer
 
179
*/
 
180
#define UNITY_API_SCOPE_CREATE_FUNCTION unity_api_scope_create
 
181
 
 
182
/**
 
183
Expands to the identifier of the scope destroy function. @hideinitializer
 
184
*/
 
185
#define UNITY_API_SCOPE_DESTROY_FUNCTION unity_api_scope_destroy
 
186
 
 
187
/// @cond
 
188
extern "C" unity::api::scopes::ScopeBase* UNITY_API_SCOPE_CREATE_FUNCTION();
 
189
extern "C" void UNITY_API_SCOPE_DESTROY_FUNCTION(unity::api::scopes::ScopeBase*);
 
190
/// @endcond
 
191
 
 
192
#endif