Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Skyward Boardcore
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Avionics
Software Development
Skyward Boardcore
Wiki
Singleton
Changes
Page history
New page
Templates
Clone repository
[Singleton] added more details and extended example
authored
4 years ago
by
Luca Conterio
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Singleton.md
+25
-3
25 additions, 3 deletions
Singleton.md
with
25 additions
and
3 deletions
Singleton.md
View page @
ffbf8152
...
...
@@ -4,7 +4,7 @@ A singleton is a class that can be **instantiated only once**: this can be very
In boardcore, you can achieve this by extending the Singleton template which you can find in
`src/shared/`
.
##
Methods
##
Implementation
The Singleton interface is a template class which exposes a single method:
```
cpp
inline
static
T
*
getInstance
()
...
...
@@ -17,6 +17,9 @@ What this method does is:
-
If an instance of this class already exists, return that instance.
-
If an instance of this class does not exist yet, create and return a new one.
> :warning: **WARNING: By definition, a Singleton is an object whose constructor is defined _private_ !**
> **In order to instantiate it or get a reference, the _getInstance()_ method has to be called.**
## Example
From the
`Singleton.h`
comment:
...
...
@@ -27,7 +30,26 @@ From the `Singleton.h` comment:
*/
class
Foo
:
public
Singleton
<
Foo
>
{
private:
Foo
()
{}
// private constructor
friend
class
Singleton
<
Foo
>
;
public:
void
someFunction
()
{
...
// do stuff
}
private
:
Foo
()
{}
// PRIVATE CONSTRUCTOR
};
```
And in the main:
```
cpp
int
main
()
{
Foo
*
foo
=
Foo
::
getInstance
();
foo
->
someFunction
();
return
0
;
}
```
\ No newline at end of file
This diff is collapsed.
Click to expand it.