Monday, November 28, 2016

Friday, March 25, 2016

C++ in modern times

I'm back in C++, and to my amaze C++ has become really productive and pleasant language to work with, I would say is as productive as JavaScript or Swift which are languages that are very important today but with the difference that it is backed by tens of years of optimisations at code generation level as well as performance. C++ is no longer this language that a lot of new people believe it as a memory leaking language, full of complex pointer arithmetic, with large template declarations which made the code verbose, etc. I would recommend that all new people using all those productive languages take a look to C++11/14 and the upcoming C++17 where the language has become quite pleasant to use, productive as JavaScript, Ruby, Swift and with amazing and unparalleled performance.

In addition to the features above, C++ believe it or not is cross-platform, you can for example in a mobile application whether it is in iOS or Android (assuming you are using MVC or MVVM), implement the Model on C++ and make it work on iOS and Android without modifications, so the soul of your app resides on fast and modern cross-platform C++ then you can glue your native UI to it using NDK on Android and Objective-C++ with wrappers on Swift on iOS, a simpler way to accomplish that is using this fabulous work from Dropbox guys

About 8 years ago I leave using C++ and move into Java not because I was a fan of Java but because the circumstances made me move into it. So before Java I worked for about 4 years on ACE (The Adaptive Communications Environment) and a lot of the stuff in that library did not exist on C++ nor in the standard library, things like tasks, IPC mechanisms like message queues, asynchronous I/O, networking objects like acceptors, connectors, time classes like chrono, etc. But now a lot of ACE is covered as part of the standard library on C++11/14 and on boost, so this is a simple example of how pleasant and simple C++ is today and how message queues can be used with a modern syntax:

#include <iostream>
#include <pplx/pplxtasks.h>
#include <boost/interprocess/ipc/message_queue.hpp>

#include <string>

using namespace std;
using namespace boost::interprocess;

int main(int argc, const char * argv[]) {
   // max elements in queue
   size_t const max_size = 100;
   // remove the queue if already exists
   message_queue::remove("the_queue");
   // create the queue
   message_queue mq(create_only, "the_queue", 100, max_size);
   
   // create a consumer Task
   auto t = pplx::create_task([]() -> int {
      // execute the Task on a separate thread
      try {
         // queue's priority
         unsigned int priority;
         
         // it will hold the size of the data received on the queue
         message_queue::size_type recvd_size;
         
         // open the queue
         message_queue mq(open_only, "the_queue");
         
         while (1) {
            
            // the queue holds messages of type string which will be hold on 
            // the "message" variable
            string message;
            message.resize(max_size);
            // read from queue
            mq.receive(&message[0], message.size(), recvd_size, priority);
            message.resize(recvd_size);
            
            // show message received
            cout << "message received: " << message << endl;
            if (message == "bye") {
               break;
            }
         }
         return 0;
      }
      catch(exception & e) {
         cout << "exception: " << e.what() << endl;
      }
      return -1;
   }).then([](int res) {
      cout << "task completed with status: " << res << endl;
   });
   
   // here on the main Thread we read a message and publish it to the queue
   string message;
   while (1) {
      cout << "message: ";
      cin >> message;
      mq.send(message.data(), message.size(), 0);
      if (message == "bye") {
         break;
      }
      cout << endl;
   }
   
   t.wait();
}

The sample code above uses PPLX which is a concurrency runtime in modern C++11 that implements the concept of Tasks and Continuations, if you are a C# developer you will find this very familiar. PPLX is contained on the fabulous C++ REST SDK library and sample code can be found here

The message_queue class allows not only inter-thread communication but inter-process communication and a lot more information about it can be found here

The following sample code shows a simple REST server using modern C++11 syntax and C++ REST SDK, a server that competes with a node.js REST server in terms of number of lines of code as well as productivity:


#include <ostream>
#include <cpprest/http_listener.h>
#include <cpprest/json.h>

using namespace std;
using namespace web;
using namespace http;
using namespace http::experimental::listener;
using namespace utility;

class RestServer {
   http_listener _listener;
   
public:
   RestServer(utility::string_t url) : _listener(url) {
      _listener.support(methods::GET, bind(&RestServer::handleGet, this, placeholders::_1));
   }
   
   pplx::task<void> open() {
      return _listener.open();
   }
   
   pplx::task<void> close() {
      return _listener.close();
   }
   
   void handleGet(http_request request) {
      json::value jsonRes;
      
      request.extract_json().
      then( [ &jsonRes ]( pplx::task<json::value> task ) {
         const auto & jsonReq = task.get();
         
         if (!jsonReq.is_null()) {
            // do whatever you need with the JSON payload...

            // now send response back ...
            jsonRes = json::value::object();
            jsonRes["response"] = json::value::string("Hello from modern C++");
         }
      }).wait();
      
      request.reply(status_codes::OK, jsonRes);
   }
   
   static void run() {
      auto rest = unique_ptr<RestServer>(new RestServer("http://localhost:8088/api/demo"));
      restServer->open().wait();
      
      cout << "Press ENTER to exit" << endl;
      
      string line;
      getline(cin, line);
      
      rest->close().wait();
   }
};


This is a version of the REST server in node.js that I took from node.js tutorial here

var express = require('express');
var app = express();
var fs = require("fs");

var user = {
   "user4" : {
      "name" : "mohit",
      "password" : "password4",
      "profession" : "teacher",
      "id": 4
   }
}

app.get('/addUser', function (req, res) {
   // First read existing users.
   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
       data = JSON.parse( data );
       data["user4"] = user["user4"];
       console.log( data );
       res.end( JSON.stringify(data));
   });
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%s:%s", host, port)


I hope this intrigues you a bit like to take a look to modern C++ and start using it on your current or next project.