Skip to content

Commit

Permalink
[doc] cppnow-2017
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-jusiak committed May 9, 2017
1 parent 434d947 commit fc4b88c
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions doc/cppnow-2017/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,34 @@

## Type constraints

#### +/- Concepts-Lite syntax in C++14/17 (predicates)
#### C++14/17 Type constraints (~Concepts-Lite predicates)

```cpp
template<class T> | struct Foo {
constexpr auto Fooable = | int foo();
$requires(T)(auto&& t) ( | };
int( t.foo() ) |
); | template<class T, REQUIRES(Fooable<T>)>
| void foo(T&&);
template<class T> | struct Foo {
constexpr auto Fooable = | int foo();
$requires(T)(auto&& t) ( | };
int( t.foo() ) |
); | template<class T, REQUIRES(Fooable<T>)>
| void foo(T&&);
```
<!-- .element: style="margin-left:0%; width:100%" -->

#### Concepts-Lite (type with predicates)
#### Non-dependable constraints

```cpp
struct Readable { | struct Reader {
template<class T, REQUIRES( | Reader(Reader&&) = default;
MoveConstructible<T> && | Reader& operator=(Reader&&) = default;
MoveAssignable<T> && | int read();
Callable<T, int()>($(read)) | };
) Readable(T); |
}; | static_assert(
| std::is_convertible<Readable, Reader>{}
| );
struct Readable { | // Readable Implementation
template<class T> |
auto operator()() const { | struct Reader { // no inheritance
MoveConstructible<T> && <|> Reader(Reader&&) = default; // ✔
MoveAssignable<T> && <|> Reader& operator=(Reader&&) = default; // ✔
Callable<T, int()>($(read))<|> int read(); // ✔
}; ^ | };
}; \__ |
\ | static_assert(
callable using ---/ | is_satisfied_by<Readable, Reader>{}
function name | );
```
<!-- .element: style="margin-left:0%; width:100%" -->

----

Expand All @@ -104,16 +108,18 @@
void onError(std::string_view msg) { throw T{msg}; }
};
```
<!-- .element: style="margin-left:0%; width:100%" -->

#### policy design
```cpp
template<class TPolicy = class TErrorPolicy>
class App : TPolicy {
void run() {
if (false) { TPolicy::onError("error!"); }
if (...) { TPolicy::onError("error!"); }
}
};
```
<!-- .element: style="margin-left:0%; width:100%" -->

#### Static dispatch and wiring
```cpp
Expand All @@ -125,6 +131,7 @@
injector.create<App>().run(); // App is a template!
}
```
<!-- .element: style="margin-left:0%; width:100%" -->

----

Expand Down Expand Up @@ -153,7 +160,7 @@
di::bind<Readable>.to<FileReader>(), // concepts checking at wiring!
di::bind<Printable>.to<ConsolePrinter>()
);
injector.create<App>().run(); // App is a template!
di::make<App>(injector).run(); // App is a template!
}
```

Expand All @@ -165,8 +172,8 @@
template<class TReader = Readable> // type = concept
class App {
TReader reader;
any<Printable> printer; // type erasure based on the same concept!
any<Printable> printer; // type erasure based on the same concept
// as concepts example
public:
App(TReader reader, any<Printable> printer) // 100% value semantics
: reader(reader), printer(printer)
Expand All @@ -178,27 +185,25 @@

#### Dynamic bindings using virtual concepts
```cpp
int main(int argc, char**) {
const auto injector = di::make_injector(
auto config = [](std::string_view printer) {
return di::make_injector(
di::bind<Readable>.to<FileReader>(),
di::bind<Printable>([&](auto&& _) -> Printable {
return argc == 1
? _.to<ConsolePrinter>()
: _.to<QtPrinter>();
di::bind<Printable>([&](auto&& _) {
return printer == "QT" ?
_.to<QtPrinter>() : _.to<ConsolePrinter>();
})
);
injector.create<App>().run();
}
};
```

----

## Automatic concepts based mocks injection
## Automatic, concepts based, mocks injection

```cpp
"should print read text"_test = [] {
constexpr auto value = 42;
auto [sut, mocks] = testing::make<App>(); // creates System Under Test
auto [app, mocks] = testing::make<App>(); // creates System Under Test
// and mocks!
InSequence sequence;
Expand All @@ -207,9 +212,10 @@
EXPECT_CALL(mocks<Printable>(), print(value));
}
sut.run();
app.run();
};
```
#### It works with concepts/type_erasure and interfaces!

==============================================================================

Expand Down

0 comments on commit fc4b88c

Please sign in to comment.