Skip to content

Commit

Permalink
Merge pull request #7 from armaanhammer/develop
Browse files Browse the repository at this point in the history
Fixed static functions
  • Loading branch information
armaanhammer authored Jan 28, 2019
2 parents e65ba1f + 613d5d7 commit 496518e
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 175 deletions.
4 changes: 4 additions & 0 deletions design_log/Step_5.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ Since I have not had prior exposure to several concepts needed for this task, I
* [Brainstorming (Appendix)](Step_5_Appendix.md#brainstorming)
* [Research (Appendix)](Step_5_Appendix.md#research)
### Fix for bind issue
* [Bind issue (Appendix)](Step_5_Appendix.md#bind-issue)
### Environment
The general environment remains the same as the previous steps.
Expand Down
73 changes: 67 additions & 6 deletions design_log/Step_5_Appendix.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,76 @@ Deciding to manually add the functions in main()
// It looks like C++ is not able to implement a programmatic enumberation of
// a class's public member functions, so I need to do it manually in main().
<br>
Bind issue
---
### Hint
Received this email:
> Armaan,
>
> Hint on the dispatcher. Bind is required and not static questions.
This was not wholely unexpected. Using static member functions of `Controller` class produced results expected during testing, but did not make use of `std::bind` which was included as a hint in one of the files' headers. It seems logical that some other test conditions which I have not encountered produce unexpected results.
### Issue fix
Problem code:
```c++
// Add available command handlers in Controller class to CommandDispatcher manually
///
/// \note needs static functions
///
/// \todo maybe use std::bind instead to enable using non-static functions.
///
command_dispatcher.addCommandHandler( "help", controller.help);
command_dispatcher.addCommandHandler( "exit", controller.exit);
command_dispatcher.addCommandHandler( "sum_ints", controller.sum_ints);
command_dispatcher.addCommandHandler( "query_payload", controller.query_payload);
command_dispatcher.addCommandHandler( "mean_ints", controller.mean_ints);
```

Fixed code:

```c++
// bind controller functions to function objects to facilitate map addition
//
// std::placeholders::_1 refers to first (and only) argument for Controller member
// functions of type rapidjson::Value
using namespace placeholders;

auto help_b = std::bind( &Controller::help, controller, _1 );
auto exit_b = std::bind( &Controller::exit, controller, _1 );
auto sum_ints_b = std::bind( &Controller::sum_ints, controller, _1 );
auto query_payload_b = std::bind( &Controller::query_payload, controller, _1 );
auto mean_ints_b = std::bind( &Controller::mean_ints, controller, _1 );

// Add available command handlers in Controller class to CommandDispatcher manually
command_dispatcher.addCommandHandler( "help", help_b );
command_dispatcher.addCommandHandler( "exit", exit_b);
command_dispatcher.addCommandHandler( "sum_ints", sum_ints_b);
command_dispatcher.addCommandHandler( "query_payload", query_payload_b);
command_dispatcher.addCommandHandler( "mean_ints", mean_ints_b);
```

This allows all Controller member functions to be non-static.



<br>

Reference
---

### std::function << know this
### std::function

### std::bind << need to look up
### std::bind

* Binds parameters to functions
* Guaranteed to make a copy of argument passed in.
Expand Down Expand Up @@ -337,9 +398,9 @@ shell:user$ ./bind.out
source: https://www.youtube.com/watch?v=JtUZmkvroKg
### std::placeholders << need to look up
### std::placeholders
### std::map << know this
### std::map
key / value pair
Expand Down Expand Up @@ -369,7 +430,7 @@ source: https://www.youtube.com/watch?v=6iyzPed7FrM

Note: class template does NOT infer parameter types. Function template does.

### std::make_pair << need to look up
### std::make_pair


<br>
Expand Down
20 changes: 13 additions & 7 deletions docs/Step_5/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@
<div class="contents">
<div class="textblock"><h2>To duplicate </h2>
<ol type="1">
<li>Rapidjson headers need to be placed in the <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/rapidjson">/Step_5/rapidjson/ directory</a> for the program to build. For convenience, the headers from the <a href="https://github.com/Tencent/rapidjson/tree/66eb6067b10fd02e419f88816a8833a64eb33551/include/rapidjson">66eb606 commit</a> have been included already. If desired, it may be possible to update the headers to the current commit by replacing the entire rapidjson directory in this project with the current <a href="https://github.com/Tencent/rapidjson/tree/master/include/rapidjson">rapidjson/include/rapidjson directory</a>.</li>
<li>Rapidjson headers need to be placed in the <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/rapidjson">/Step_5/rapidjson/ directory</a> for the program to build. For convenience, the headers from the <a href="https://github.com/Tencent/rapidjson/tree/66eb6067b10fd02e419f88816a8833a64eb33551/include/rapidjson">66eb606 commit</a> have been included already. If desired, it may be possible to update the headers to the current commit by replacing the entire rapidjson directory in this project the current <a href="https://github.com/Tencent/rapidjson/tree/master/include/rapidjson">rapidjson/include/rapidjson directory</a>.</li>
</ol>
<ol type="2">
<ol type="1">
<li>In the <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/build">/src/Step_5/build/ directory</a> run <code>cmake ../</code>. The build environment will be set up in that directory using the <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/CMakeLists.txt">CMakeLists.txt file</a> in the parent directory.</li>
</ol>
<ol type="3">
<ol type="1">
<li>Enure that the bool value for <code>TEST_ALL</code> <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/main.cpp#L37">on line 37 of main.cpp</a> is set to the desired value:<ul>
<li>A value of <code>true</code> will cause all test commands to be sent to the dispatcher upon program startup.</li>
<li>A value of <code>false</code> will skip all tests and immediately fall through to a user prompt.</li>
</ul>
</li>
</ol>
<ol type="4">
<ol type="1">
<li>In the <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/build/">/src/Step_5/build/ directory</a> run <code>make</code>. Executable will build as <code>dispatcher</code>.</li>
</ol>
<ol type="5">
<ol type="1">
<li>To run executable, run <code>./dispatcher</code>.</li>
</ol>
<ol type="6">
<ol type="1">
<li>If the final command (exit_command) <a href="https://github.com/armaanhammer/lock-challenge/tree/master/src/Step_5/main.cpp#L804">on line 804 of main.cpp</a> is not commented out, the exit_command will succeed, terminating execution. If it is commented out, execution will fall through to a user prompt.</li>
</ol>
<ol type="7">
<ol type="1">
<li>At the user prompt, JSON may be entered for parsing.</li>
</ol>
<h2>Files </h2>
Expand Down Expand Up @@ -228,6 +228,12 @@ <h3>Research</h3>
<li><a href="https://github.com/armaanhammer/lock-challenge/tree/master/design_log/Step_5_Appendix.md#brainstorming">Brainstorming (Appendix)</a></li>
<li><a href="https://github.com/armaanhammer/lock-challenge/tree/master/design_log/Step_5_Appendix.md#research">Research (Appendix)</a></li>
</ul>

<h3>Fix for bind issue</h3>
<ul>
<li><a href="https://github.com/armaanhammer/lock-challenge/tree/master/design_log/Step_5_Appendix.md#parsing-code">Bind issue (Appendix)</a></li>
</ul>

<h3>Environment</h3>
<p>The general environment remains the same as the previous steps.</p>
<h4>CMAKE</h4>
Expand Down
Loading

0 comments on commit 496518e

Please sign in to comment.