
A few weeks ago, Microsoft presented its new development, the Casablanca project. In order to understand what it is, you need to go back a little further into the past, to the last conference
Going Native 2012 , where in addition to discussing the new C ++ 11 standard, many more clever ideas were said. One of them was in a lecture by Herb Sutter. He noted that the biggest problem of modern C ++ is not all garbage collectors or lambda expression syntax, but the poverty of the standard library. When a novice Java or C # programmer asks his senior colleague how to send / accept an HTTP request, parse XML \ JSON, archive / unzip a ZIP file, etc. - it receives a specific, unambiguous and always working answer: “use this class from the standard library”. When a novice C ++ programmer asks the same question - he hears "well, there are no standard tools for this, you need to either write yourself or there are some libraries, but here you have to choose, test, read licenses ...". As a result, we have a wild library zoo with different levels of performance, often lousy cross-platform, a bunch of self-made bicycles, difficulties in switching between projects, because “before I used boost, and here in the project everything is on Qt”, etc. Herb Sutter suggested that the main thing to focus on is an extension of the standard library.
So, Microsoft Casablanca is the first outline of how this extension could look, what it could include and how it all could work together. No one has yet spoken about the standardization of Casablanca as an extension of the standard C ++ library, but now you can download it as a separate library (something like an alpha version) and try to learn. In addition, Microsoft promotes Casablanca as a means of creating productive services for the Azure platform.
So, what is included in Casablanca? Let me immediately show “Hello world” on Casablanca - this will be a web server that returns the text “Hello World!” To any request to it:
')
#include "stdafx.h" #include <iostream> #include "astreambuf.h" using namespace http::listener; int main(int argc, char* argv[]) { http_listener::create("http://localhost:4711/trivial", [](http_request message) { message.reply(http::status_codes::OK, "Hello World!"); }) /* Prevent Listen() from returning until user hits 'Enter' */ .listen([]() { fgetc(stdin); }).wait(); return 0; }
Beautiful, is not it?
And now a little more complete about the library. It includes the following things:
PPL tasks
This is a library for working with asynchronous operations, which, in fact, is included in the following (not yet released) version of Visual Studio, but the Casablanca developers decided to allocate it separately and enable it to be used in Visual Studio 2010. It works like this:
http::http_client client("http://localhost:80"); pplx::task<http::http_response> resp = client.request(methods::GET, "/foo.html");
Here we have created an http-client to access localhost: 80 and the task “and give me the GET method page foo.html”. The task starts to run asynchronously, starting from the moment it is created. At any time, we can ask for the task, whether it has already been completed:
bool done = resp.is_done();
and ask for the result of the execution:
http_response response = resp.get();
The second option is to immediately transfer the task to the lambda function, which will be executed upon its completion:
resp.then( [=](http_response response) { ... });
More about PPL tasks
here.HTTP clients and servers
The two examples above explain briefly both. Add to this can be except the presence of special classes for addresses, headers and other amenities. In general, working with HTTP becomes no more difficult for C # or Java, and in any case, it is much more pleasant than using WinInet, which is included with Windows now.
Access Azure Storage Services
I have already said that one of the main goals is to facilitate the creation of applications for Azure. Here’s how the data is saved to Azure Storage Services
storage_credentials creds = local_storage::get_credentials(); cloud_blob_client local(local_storage::get_blob_uri(), local_storage::get_credentials()); cloud_blob_client real("http://johndoe.blob.core.windows.net", creds); cloud_blob_container container = client.create_container(L"sampledata").get(); auto blob = container.create_block_blob(L"sample1"); blob.put(L"A first sample blob", L"text/plain");
Very concise, I think.
Json
Parsing
std::istream stream = ...; json::value value1(stream); double var = value1["var"].as_double()
Creature
json::value::element_vector e; e.push_back(json::value(false)); e.push_back(json::value::string("hehe")); json::value arr = json::value::array(e); std::ostream stream = ...; stream << arr;
Actors
The idea is taken from Erlang. Sending and processing messages as an additional layer of isolation from the outside world. It makes it easier to look at some things, avoid bugs in multi-threaded programming, and generally have a good pattern. In the example below, an Actor is created that can receive a message and write it to standard output.
class Hello : public actors::actor { public: Hello() {} actors::actor *factory() { new Hello(); } protected: void initialize(actors::ActorInitEvent e) { } virtual void execute() { std::string name; accept(name).then([=](std::string &name) { std::cout << "Hello, " << name << "!" << std::endl; Done(); }); } }; ... PID h = actor::spawn<Hello>(); h.send<std::string>("Niklas");
The library is still in development and is still very raw. Personally, I find it difficult to judge how it will take root. At first glance, everything is very cultural and logical. The architecture was designed with the idea of ​​cross-platform, the API is open, there is a desire to open the code and add a lot of useful things. On the other hand, from the “just good library” to the “extension of the standard C ++ library”, the gap is wide to the horizon. In any case, for the sake of interest in Casablanca can be viewed.
Materials on the topic