Skip to content

Commit

Permalink
Update to C++11 Usage Section
Browse files Browse the repository at this point in the history
  • Loading branch information
kidproquo committed Sep 24, 2014
1 parent edd7e40 commit 9fcf9b3
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions 14.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

## Custom OpenGL (what to cover here? CustomCommand?)

## C++11 usage
## C++11 Usage
### Coding guidelines
Use of the ``auto`` keyword is encouraged for better code readability. Please refer to Herb Sutter's [AAA (Almost Always Auto)](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/) post.

Expand All @@ -38,13 +38,14 @@ _widget = new widget{};
...
//destroy
delete _widget;
```
```c++

//smart pointer
_widget = make_unique<widget>();
```

Note: make_unique is a C++14 addition. You can use Herb Sutter's version (http://herbsutter.com/gotw/_102/) for C++11:

//Note: make_unique is a C++14 addition.
//You can use Herb Sutter's version (http://herbsutter.com/gotw/_102/) for C++11:
```c++
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
Expand All @@ -53,16 +54,24 @@ std::unique_ptr<T> make_unique(Args&&... args)
```
Note that there's no need to track and delete the smart pointer version - it will be automatically deleted when its owner (the scene in this case) gets deleted.
####Obtaining raw pointers from smart pointers
When you need to interface with the engine API, you will need to supply raw pointers. For e.g. when you want to register a callback:
Quoting [Herb Sutter](http://herbsutter.com/gotw/_102/):
* Prefer to allocate objects to be managed by a shared_ptr using make_shared, and objects to be managed by a unique_ptr with make_unique.
* Although Standard C++ does not yet have make_unique, this is mostly an oversight and it will almost certainly eventually be added. In the meantime, use the version shown above, and your code will be forward-compatible with the likely direction of the C++ standard.
* Avoid using plain new or other raw unmanaged allocation directly. Instead, use a factory like make_unique that wraps the raw allocation and immediately passes it to another object that will own the resource. Often that owning object will be a smart pointer, but it can be a scope guard or any other kind of owning object whose destructor will deallocate the resource safely.
###Random
####Obtaining raw pointers from smart pointers
When you need to interface with the engine API, you will need to supply raw pointers. For e.g. when you want to register a callback, you can use the ``get()`` method:
```c++
//_game is a unique_ptr
contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, _game.get());
```


### References
1. A Tour of C++
2. Effective Modern C++
3. C++ Primer
1. Herb Sutter's [Elements of Modern C++ Style](http://herbsutter.com/elements-of-modern-c-style/)
2. Scott Meyer's [Effective Modern C++](http://shop.oreilly.com/product/0636920033707.do)


## rendering pipeline (notes about this in the wiki)

Expand Down

0 comments on commit 9fcf9b3

Please sign in to comment.