Skip to content

Commit 3389d65

Browse files
committed
move header files into include/promise-cpp/
1 parent 5e533a1 commit 3389d65

22 files changed

+610
-577
lines changed

CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ project(promise)
1818
option(PROMISE_BUILD_SHARED "Build shared library" OFF)
1919

2020
set(my_headers
21-
include/promise.hpp
22-
include/any.hpp
23-
include/add_ons.hpp
24-
include/call_traits.hpp
21+
include/promise-cpp/promise.hpp
22+
include/promise-cpp/any.hpp
23+
include/promise-cpp/add_ons.hpp
24+
include/promise-cpp/call_traits.hpp
2525
)
2626

2727
set(my_sources

Readme.md

+55-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# C++ promise/A+ library in Javascript style.
22

33
<!-- TOC -->
4-
5-
- [C++ promise/A+ library in Javascript style.](#c-promisea-library-in-javascript-style)
64
- [What is promise-cpp ?](#what-is-promise-cpp-)
5+
- [Features](#features)
76
- [Examples](#examples)
87
- [Examples list](#examples-list)
98
- [Compiler required](#compiler-required)
10-
- [Build tips](#build-tips)
9+
- [Usage](#usage)
10+
- [Used as header only library](#used-as-header-only-library)
11+
- [Used as static library](#used-as-static-library)
12+
- [Used as shared library](#used-as-shared-library)
13+
- [Build tips about asio examples](#build-tips-about-asio-examples)
1114
- [Sample code 1](#sample-code-1)
1215
- [Sample code 2](#sample-code-2)
1316
- [Global functions](#global-functions)
@@ -35,28 +38,36 @@
3538
- [DeferLoop::doContinue();](#deferloopdocontinue)
3639
- [DeferLoop::doBreak(const RET_ARG... &ret_arg);](#deferloopdobreakconst-ret_arg-ret_arg)
3740
- [And more ...](#and-more-)
38-
- [about exceptions](#about-exceptions)
39-
- [about the chaining parameter](#about-the-chaining-parameter)
41+
- [About exceptions](#about-exceptions)
42+
- [About the chaining parameter](#about-the-chaining-parameter)
4043
- [Match rule for chaining parameters](#match-rule-for-chaining-parameters)
4144
- [Resolved parameters](#resolved-parameters)
4245
- [Rejected parameters](#rejected-parameters)
4346
- [Omit parameters](#omit-parameters)
44-
- [copy the promise object](#copy-the-promise-object)
45-
- [handle uncaught exceptional or rejected parameters](#handle-uncaught-exceptional-or-rejected-parameters)
47+
- [Copy the promise object](#copy-the-promise-object)
48+
- [Handle uncaught exceptional or rejected parameters](#handle-uncaught-exceptional-or-rejected-parameters)
4649
- [about multithread](#about-multithread)
47-
4850
<!-- /TOC -->
4951

5052
## What is promise-cpp ?
5153

52-
Promise-cpp is **header only** library that implements promise/A+ standard. (To support header only, compiled with macro PROMISE_HEADONLY)
53-
54-
Promise-cpp is easy to use, just #include "promise.hpp" is enough. With promise-cpp, you can resolve or reject any type of data without writing complex template code.
54+
Promise-cpp is library that implements promise/A+ standard, which can be the base component in event-looped asychronized programming. It is NOT std::promise.
55+
56+
## Features
57+
58+
Similar to Javascript Promise API.
59+
60+
Type safety: the resolved/rejected arguments can be captured by the "then" function with same arguments type.
61+
62+
Exceptions supports: cpp exception will be received by the "on_rejected" function.
5563

56-
Promise-cpp is depended only on STL (c++11 or higher).
57-
Although some of the asio examples are linked against boost library, promise-cpp itself is absolutely **workable without boost library** and can be used together with other asynchronized libraries.
64+
Optional header-only configuration enabled with the PROMISE_HEADONLY macro
5865

59-
Promise-cpp can be the base component in event-looped asychronized programming, which is NOT std::promise.
66+
Easy to use, just #include "promise-cpp/promise.hpp" is enough, code based on standard c++11 syntax, no external dependencies required.
67+
68+
Easy to integrate with other libararies (see examples of [asio](example/asio_timer.cpp), [qt](example/qt_timer) and [mfc](example/mfc_timer)).
69+
70+
Useful extended functions on promise object: doWhile, raceAndResolve, raceAndReject
6071

6172
## Examples
6273

@@ -92,7 +103,25 @@ The library has passed test on these compilers --
92103

93104
* clang 3.4.2
94105

95-
### Build tips
106+
### Usage
107+
108+
#### Used as header only library
109+
110+
To use as header only library, just define macro PROMISE_HEADONLY when compiling.
111+
112+
#### Used as static library
113+
114+
```
115+
cmake /path/to/promise_source
116+
```
117+
118+
#### Used as shared library
119+
120+
```
121+
cmake -DPROMISE_BUILD_SHARED=ON /path/to/promise_source
122+
```
123+
124+
#### Build tips about asio examples
96125

97126
Some of the [examples](example) use boost::asio as io service, and use boost::beast as http service.
98127
You need to [boost_1_66](https://www.boost.org/doc/libs/1_66_0/more/getting_started/index.html)
@@ -104,12 +133,6 @@ For examples, you can build with boost library --
104133
> cmake -DBOOST_ROOT=/path/to/boost_source /path/to/promise_source
105134
```
106135

107-
If you do not have boost installed, just build by --
108-
109-
```
110-
> cmake /path/to/promise_source
111-
```
112-
113136
### Sample code 1
114137

115138
Example of asio http client. [(full code here)](example/asio_http_client.cpp)
@@ -195,7 +218,7 @@ int main() {
195218
## Global functions
196219

197220
### Promise newPromise(FUNC func);
198-
Creates a new Promise object with a user-defined function.
221+
Creates a new promise object with a user-defined function.
199222
The user-defined functions, used as parameters by newPromise, must have a parameter Defer d.
200223
for example --
201224

@@ -222,7 +245,7 @@ return reject("some_error");
222245

223246
### Promise all(const PROMISE_LIST &promise_list);
224247
Wait until all promise objects in "promise_list" are resolved or one of which is rejected.
225-
The "promise_list" can be any container that has Promise object as element type.
248+
The "promise_list" can be any container that has promise object as element type.
226249

227250
> for (Promise &promise : promise_list) { ... }
228251
@@ -244,7 +267,7 @@ all(promise_list).then([](){
244267
Returns a promise that resolves or rejects as soon as one of
245268
the promises in the iterable resolves or rejects, with the value
246269
or reason from that promise.
247-
The "promise_list" can be any container that has Promise object as element type.
270+
The "promise_list" can be any container that has promise object as element type.
248271

249272
> for (Promise &promise : promise_list) { ... }
250273
@@ -446,7 +469,7 @@ Break the doWhile loop (ret_arg will be transferred).
446469
447470
## And more ...
448471
449-
### about exceptions
472+
### About exceptions
450473
To throw any object in the callback functions above, including on_resolved, on_rejected, on_always,
451474
will same as d.reject(the_throwed_object) and returns immediately.
452475
for example --
@@ -460,7 +483,7 @@ return newPromise([](Defer d){
460483
```
461484
For the better performance, we suggest to use function reject instead of throw.
462485

463-
### about the chaining parameter
486+
### About the chaining parameter
464487
Any type of parameter can be used when call resolve, reject or throw, except that the plain string or array.
465488
To use plain string or array as chaining parameters, we may wrap it into an object.
466489

@@ -533,15 +556,19 @@ newPromise([](Defer d){
533556
534557
The reject parameters follows the the same omit rule as resolved parameters.
535558
536-
### copy the promise object
559+
### Copy the promise object
537560
To copy the promise object is allowed and effective, please do that when you need.
538561
539562
```cpp
540563
Promise promise = newPromise([](Defer d){});
541564
Promise promise2 = promise; //It's safe and effective
542565
```
543566

544-
### handle uncaught exceptional or rejected parameters
567+
<!--
568+
### Life time of the internal storage inside a promise chain
569+
-->
570+
571+
### Handle uncaught exceptional or rejected parameters
545572

546573
The uncaught exceptional or rejected parameters are ignored by default. We can specify a handler function to do with these parameters --
547574

add_ons/asio/io.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef INC_ASIO_IO_HPP_
2828
#define INC_ASIO_IO_HPP_
2929

30-
#include "../../include/promise.hpp"
30+
#include "promise-cpp/promise.hpp"
3131
#include <boost/asio/ip/tcp.hpp>
3232
#include <boost/asio/connect.hpp>
3333
#include <boost/beast/core.hpp>

add_ons/asio/timer.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
// void clearTimeout(Promise promise);
4242
//
4343

44-
#include "../../include/promise.hpp"
44+
#include "promise-cpp/promise.hpp"
4545
#include <chrono>
4646
#include <boost/asio.hpp>
4747
#include <boost/asio/steady_timer.hpp>

add_ons/qt/promise_qt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// void clearTimeout(Promise promise);
4141
//
4242

43-
#include "../../include/promise.hpp"
43+
#include "promise-cpp/promise.hpp"
4444
#include "promise_qt.hpp"
4545
#include <chrono>
4646
#include <QObject>

add_ons/qt/promise_qt.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
// void clearTimeout(Defer d);
4242
//
4343

44-
#include "../../include/promise.hpp"
44+
#include "promise-cpp/promise.hpp"
4545
#include <chrono>
4646
#include <QObject>
4747
#include <QTimerEvent>

add_ons/simple_task/simple_task.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <chrono>
3939
#include <thread>
4040
#include <utility>
41-
#include "../../include/promise.hpp"
41+
#include "promise-cpp/promise.hpp"
4242

4343

4444
class Service {

add_ons/windows/promise_windows.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// void clearTimeout(Defer d);
4343
//
4444

45-
#include "../../include/promise.hpp"
45+
#include "promise-cpp/promise.hpp"
4646
#include <chrono>
4747
#include <map>
4848
#include <windows.h>

example/asio_http_client.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <memory>
3535
#include <string>
3636
#include <regex>
37-
#include "promise.hpp"
3837
#include "add_ons/asio/io.hpp"
3938

4039
using namespace promise;

example/asio_http_server.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
#include <vector>
3838

3939

40-
// Boost asio use mulithread in this example,
41-
// so we enable multithread mode of "promise library".
42-
#define PM_MULTITHREAD
43-
#include "promise.hpp"
4440
#include "add_ons/asio/io.hpp"
4541

4642
using namespace promise;

example/chain_defer_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "promise.hpp"
1+
#include "promise-cpp/promise.hpp"
22
#include <sstream>
33
#include <iostream>
44

example/mfc_timer/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ set(PROJECT_SOURCES
2222
res/mfc_timer.ico
2323
res/mfctimer.rc2
2424

25-
../../include/promise.hpp
26-
../../include/any.hpp
27-
../../include/add_ons.hpp
28-
../../include/call_traits.hpp
25+
../../include/promise-cpp/promise.hpp
26+
../../include/promise-cpp/any.hpp
27+
../../include/promise-cpp/add_ons.hpp
28+
../../include/promise-cpp/call_traits.hpp
2929
../../add_ons/windows/promise_windows.hpp
3030
)
3131

example/qt_timer/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ set(PROJECT_SOURCES
3232
mainwindow.cpp
3333
mainwindow.h
3434
mainwindow.ui
35-
../../include/promise.hpp
36-
../../include/any.hpp
37-
../../include/add_ons.hpp
38-
../../include/call_traits.hpp
35+
../../include/promise-cpp/promise.hpp
36+
../../include/promise-cpp/any.hpp
37+
../../include/promise-cpp/add_ons.hpp
38+
../../include/promise-cpp/call_traits.hpp
3939
../../add_ons/qt/promise_qt.hpp
4040
)
4141

example/simple_benchmark_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <iostream>
3131
#include <string>
3232
#include <chrono>
33-
#include "promise.hpp"
33+
#include "promise-cpp/promise.hpp"
3434
#include "add_ons/simple_task/simple_task.hpp"
3535

3636
using namespace promise;

example/simple_timer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <chrono>
3131
#include <thread>
3232
#include <utility>
33-
#include "promise.hpp"
33+
#include "promise-cpp/promise.hpp"
3434
#include "add_ons/simple_task/simple_task.hpp"
3535

3636
using namespace promise;

example/test0.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
#include <stdio.h>
2828
#include <string>
29-
#include "promise.hpp"
29+
#include "promise-cpp/promise.hpp"
3030

3131
using namespace promise;
3232

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)