Skip to content

Commit 0622f4f

Browse files
yisonPylkitaskypjack
authored andcommitted
Enabled syntax hightlight in example in readme (skypjack#92)
Syntax highlighting makes reading source code easier
1 parent f4b59fb commit 0622f4f

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ As an example, a *handle* should be initialized before any other operation and c
1616

1717
## Code Example
1818

19-
```
19+
```cpp
2020
#include <uvw.hpp>
2121
#include <memory>
2222

@@ -86,7 +86,9 @@ Because of that, users have not to install it to compile and execute the tests.
8686
This means that including the `uvw.hpp` header or one of the other `uvw/*.hpp` headers is enough to use it.<br/>
8787
It's a matter of adding the following line at the top of a file:
8888
89-
#include <uvw.hpp>
89+
```cpp
90+
#include <uvw.hpp>
91+
```
9092

9193
Then pass the proper `-I` argument to the compiler to add the `src` directory to the include paths.<br/>
9294
Note that users are demanded to correctly setup include directories and libraries search paths for *libuv*.
@@ -154,49 +156,65 @@ Therefore the rule quickly becomes *feel free to make a request and forget about
154156

155157
The first thing to do to use `uvw` is to create a loop. In case the default one is enough, it's easy as doing this:
156158

157-
auto loop = uvw::Loop::getDefault();
159+
```cpp
160+
auto loop = uvw::Loop::getDefault();
161+
```
158162

159163
Note that loop objects don't require to be closed explicitly, even if they offer the `close` member function in case an user wants to do that.<br/>
160164
Loops can be started using the `run` member function. The two calls below are equivalent:
161165

162-
loop->run();
163-
loop->run<uvw::Loop::Mode::DEFAULT>
166+
```cpp
167+
loop->run();
168+
loop->run<uvw::Loop::Mode::DEFAULT>
169+
```
164170

165171
Available modes are: `DEFAULT`, `ONCE`, `NOWAIT`. Please refer to the documentation of *libuv* for further details.
166172

167173
In order to create a resource and to bind it to the given loop, just do the following:
168174

169-
auto tcp = loop.resource<uvw::TcpHandle>();
175+
```cpp
176+
auto tcp = loop.resource<uvw::TcpHandle>();
177+
```
170178

171179
The line above will create and initialize a tcp handle, then a shared pointer to that resource will be returned.<br/>
172180
Users should check if pointers have been correctly initialized: in case of errors, they won't be.<br/>
173181
Another way to create a resource is:
174182

175-
auto tcp = TcpHandle::create(loop);
176-
tcp->init();
183+
```cpp
184+
auto tcp = TcpHandle::create(loop);
185+
tcp->init();
186+
```
177187

178188
Pretty annoying indeed. Using a loop is the recommended approach.
179189

180190
The resources also accept arbitrary user-data that won't be touched in any case.<br/>
181191
Users can set and get them through the `data` member function as it follows:
182192

183-
resource->data(std::make_shared<int>(42));
184-
std::shared_ptr<void> data = resource->data();
193+
```cpp
194+
resource->data(std::make_shared<int>(42));
195+
std::shared_ptr<void> data = resource->data();
196+
```
185197
186198
Resources expect a `std::shared_pointer<void>` and return it, therefore any kind of data is welcome.<br/>
187199
Users can explicitly specify a type other than `void` when calling the `data` member function:
188200
189-
std::shared_ptr<int> data = resource->data<int>();
201+
```cpp
202+
std::shared_ptr<int> data = resource->data<int>();
203+
```
190204

191205
Remember from the previous section that a handle will keep itself alive until one invokes the `close` member function on it.<br/>
192206
To know what are the handles that are still alive and bound to a given loop, just do the following:
193207

194-
loop.walk([](uvw::BaseHandle &){ /* application code here */ });
208+
```cpp
209+
loop.walk([](uvw::BaseHandle &){ /* application code here */ });
210+
```
195211
196212
`BaseHandle` exposes a few methods and cannot be used to know the original type of the handle.<br/>
197213
Anyway, it can be used to close the handle that originated from it. As an example, all the pending handles can be closed easily as it follows:
198214
199-
loop.walk([](uvw::BaseHandle &h){ h.close(); });
215+
```cpp
216+
loop.walk([](uvw::BaseHandle &h){ h.close(); });
217+
```
200218

201219
No need to keep track of them.
202220

@@ -234,7 +252,7 @@ All the other events are specific for the given resource and documented in the A
234252

235253
The code below shows how to create a simple tcp server using `uvw`:
236254

237-
```
255+
```cpp
238256
auto loop = uvw::Loop::getDefault();
239257
auto tcp = loop.resource<uvw::TcpHandle>();
240258

0 commit comments

Comments
 (0)