Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kicad-source-mirror
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Elphel
kicad-source-mirror
Commits
cc6ca277
Commit
cc6ca277
authored
Aug 02, 2013
by
tomasz.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added COROUTINE and DELEGATE class examples.
parent
ca138b8e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
0 deletions
+92
-0
Makefile
include/tool/examples/Makefile
+9
-0
coroutine_example.cpp
include/tool/examples/coroutine_example.cpp
+48
-0
delegate_example.cpp
include/tool/examples/delegate_example.cpp
+35
-0
No files found.
include/tool/examples/Makefile
0 → 100644
View file @
cc6ca277
CXXFLAGS
=
-I
../..
-I
../../boost
all
:
delegate_example coroutine_example
delegate_example
:
delegate_example.cpp
g++
-o
$@
$^
$(CXXFLAGS)
coroutine_example
:
coroutine_example.cpp
g++
-o
$@
$^
$(CXXFLAGS)
-lboost_context
\ No newline at end of file
include/tool/examples/coroutine_example.cpp
0 → 100644
View file @
cc6ca277
#include <cstdio>
#include <string>
#include <tool/coroutine.h>
using
namespace
std
;
typedef
COROUTINE
<
int
,
int
>
MyCoroutine
;
class
MyClass
{
public
:
int
CountTo
(
int
n
)
{
printf
(
"%s: Coroutine says hi. I will count from 1 to %d and yield each value.
\n
"
,
__FUNCTION__
,
n
);
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
{
printf
(
"%s: Yielding %d
\n
"
,
__FUNCTION__
,
i
);
cofunc
.
Yield
(
i
);
}
}
void
Run
()
{
cofunc
=
MyCoroutine
(
this
,
&
MyClass
::
CountTo
);
printf
(
"%s: Calling coroutine that will count from 1 to 5.
\n
"
,
__FUNCTION__
);
cofunc
.
Call
(
5
);
while
(
cofunc
.
Running
())
{
printf
(
"%s: Got value: %d
\n
"
,
__FUNCTION__
,
cofunc
.
ReturnValue
());
cofunc
.
Resume
();
}
printf
(
"%s: Done!
\n
"
,
__FUNCTION__
);
}
MyCoroutine
cofunc
;
};
main
()
{
MyClass
obj
;
obj
.
Run
();
return
0
;
}
include/tool/examples/delegate_example.cpp
0 → 100644
View file @
cc6ca277
#include <cstdio>
#include <string>
#include <tool/delegate.h>
using
namespace
std
;
class
MyClass
{
public
:
int
MyMethod
(
const
string
&
arg
)
{
printf
(
"MyClass(this = %p)::MyMethod() called with string '%s', length %d
\n
"
,
this
,
arg
.
c_str
(),
arg
.
length
());
return
arg
.
length
();
}
};
typedef
DELEGATE
<
int
,
const
string
&>
MyDelegate
;
main
()
{
MyClass
t1
;
MyClass
t2
;
MyDelegate
ptr1
(
&
t1
,
&
MyClass
::
MyMethod
);
MyDelegate
ptr2
(
&
t2
,
&
MyClass
::
MyMethod
);
int
retval1
,
retval2
;
retval1
=
ptr1
(
"apples"
);
retval2
=
ptr2
(
"cherries"
);
printf
(
"Object 1 returned %d, object 2 returned %d
\n
"
,
retval1
,
retval2
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment