Enrico Zini: cxx11-talk-examples
C++11 talk examples
On 2014-11-27 I gave a talk about C++ and new features introduced with C++11:
these are the examples. They are all licensed under the wtfpli version
2. See cxx11-talk-notes for the talk notes.
Note that the wrapper interfaces turns errors from the underlying libraries
into exceptions, so the method calls just do what they should, without the need
of documenting special return values for error messages, and removing the need
for each library to implement yet another way of reporting errors.
Also note that all wrapper objects do RAII: you create them and they clean
after themselves when they go out of scope.
The wrapper objects also have cast operators to make them behave as the pointer
or handle that they are wrapping, so that they can be transparently passed to
the underlying libraries.
(note: I had to add
ezhash.cpp
Example usage
Simple sqlite bindings
Remarkably simple sqlite3 bindings based on lambda callbacks.
ezsqlite.h
ezsqlite.cpp
Example usage
A fast Directory object
This is a lightweight wrapper around
ezfs.cpp
Example usage
U+2063 INVISIBLE SEPARATOR to prevent noreturn
statements to be misinterpreted by the blog formatter. If you copypaste the
code and encounter issues, you may want to delete the noreturn statements and
retype them)
A gcrypt hash class
This class is a light wrapper around gcrypt's hashing
functions.
ezhash.h
#ifndef EZHASH_H
#define EZHASH_H
#include <string>
#include <gcrypt.h>
namespace ezhash
class Hash
protected:
// members can now be initialized just like this, without needing to repeat
// their default assignment in every constructor
gcry_md_hd_t handle = nullptr;
public:
Hash(int algo, unsigned int flags=0);
~Hash();
// Assign 'delete' to a method to tell the compiler not to generate it
// automatically. In this case, we make the object non-copiable.
Hash(const Hash&) = delete;
Hash(const Hash&&) = delete;
Hash& operator=(const Hash&) = delete;
// Add a buffer to the hash
void hash_buf(const std::string& buf);
// Add the contents of a file to the hash
void hash_file(int fd);
// Get a string with the hexadecimal hash
std::string read_hex(int algo=0);
/// Pretend that we are a gcry_md_hd_t handle
operator gcry_md_hd_t() return handle;
;
#endif
#include "ezhash.h"
#include <unistd.h>
#include <errno.h>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <stdexcept>
using namespace std;
namespace ezhash
namespace
// noreturn attribute, to tell the compiler that this function never returns
[ [noreturn]] void throw_gcrypt_error(gcry_error_t err)
string msg;
msg += gcry_strsource(err);
msg += "/";
msg += gcry_strerror(err);
throw runtime_error(msg);
string errno_str(int error)
char buf[256];
#if (_POSIX_C_SOURCE >= 200112L _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
strerror_r(errno, buf, 256);
string res(buf);
#else
string res(strerror_r(errno, buf, 256));
#endif
return res;
[ [noreturn]] void throw_libc_error(int error)
throw runtime_error(errno_str(error));
Hash::Hash(int algo, unsigned int flags)
gcry_error_t err = gcry_md_open(&handle, algo, flags);
if (err) throw_gcrypt_error(err);
Hash::~Hash()
gcry_md_close(handle);
void Hash::hash_buf(const std::string& buf)
gcry_md_write(handle, buf.data(), buf.size());
void Hash::hash_file(int fd)
char buf[4096];
while (true)
ssize_t res = ::read(fd, buf, 4096);
if (res < 0) ezfs::throw_libc_error();
if (res == 0) break;
gcry_md_write(handle, buf, res);
std::string Hash::read_hex(int algo)
unsigned char* res = gcry_md_read(handle, algo);
unsigned int len = gcry_md_get_algo_dlen(
algo == 0 ? gcry_md_get_algo(handle) : algo);
// Format the hash into a hex digit
stringstream hexbuf;
hexbuf << hex << setfill('0');
for (unsigned i = 0; i < len; ++i)
hexbuf << setw(2) << (unsigned)res[i];
return hexbuf.str();
ezhash::Hash sha256(GCRY_MD_SHA256);
sha256.hash_buf("ciao\n");
sha256.hash_buf("foo\n");
cout << sha256.read_hex() << endl;
#ifndef EZSQLITE_H
#define EZSQLITE_H
#include <sqlite3.h>
#include <string>
#include <functional>
#include <stdexcept>
namespace ezsqlite
/// RAII wrapper around a sqlite3 database handle
class DB
protected:
sqlite3* handle = nullptr;
public:
// Open a connection to a SQLite database
DB(const std::string& filename, int flags=SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE);
DB(const DB&) = delete;
DB(const DB&&) = delete;
DB& operator=(const DB&) = delete;
~DB();
/**
* Execute a query, optionally calling 'callback' on every result row
*
* The arguments to callback are:
* 1. number of columns
* 2. text values of the columns
* 3. names of the columns
*/
// std::function can be used to wrap any callable thing in C++
// see: http://en.cppreference.com/w/cpp/utility/functional/function
void exec(const std::string& query, std::function<bool(int, char**, char**)> callback=nullptr);
/// Pretend that we are a sqlite3 pointer
operator sqlite3*() return handle;
;
#endif
#include "ezsqlite.h"
namespace ezsqlite
DB::DB(const std::string& filename, int flags)
int res = sqlite3_open_v2(filename.c_str(), &handle, flags, nullptr);
if (res != SQLITE_OK)
// From http://www.sqlite.org/c3ref/open.html
// Whether or not an error occurs when it is opened, resources
// associated with the database connection handle should be
// released by passing it to sqlite3_close() when it is no longer
// required.
std::string errmsg(sqlite3_errmsg(handle));
sqlite3_close(handle);
throw std::runtime_error(errmsg);
DB::~DB()
sqlite3_close(handle);
namespace
// Adapter to have sqlite3_exec call a std::function
int exec_callback(void* data, int columns, char** values, char** names)
std::function<bool(int, char**, char**)> cb = *static_cast<std::function<bool(int, char**, char**)>*>(data);
return cb(columns, values, names);
void DB::exec(const std::string& query, std::function<bool(int, char**, char**)> callback)
char* errmsg;
void* cb = callback ? &callback : nullptr;
int res = sqlite3_exec(handle, query.c_str(), exec_callback, cb, &errmsg);
if (res != SQLITE_OK && errmsg)
// http://www.sqlite.org/c3ref/exec.html
//
// If the 5th parameter to sqlite3_exec() is not NULL then any error
// message is written into memory obtained from sqlite3_malloc() and
// passed back through the 5th parameter. To avoid memory leaks, the
// application should invoke sqlite3_free() on error message strings
// returned through the 5th parameter of of sqlite3_exec() after the
// error message string is no longer needed.
std::string msg(errmsg);
sqlite3_free(errmsg);
throw std::runtime_error(errmsg);
// Connect to the database
ezsqlite::DB db("erlug.sqlite");
// Make sure we have a table
db.exec(R"(
CREATE TABLE IF NOT EXISTS files (
name TEXT NOT NULL,
sha256sum TEXT NOT NULL
)
)");
// Read the list of files that we know
map<string, string> files;
db.exec("SELECT name, sha256sum FROM files", [&](int columns, char** vals, char** names)
if (columns != 2) return false;
files.insert(make_pair(vals[0], vals[1]));
return true;
);
O_PATH file descriptors for directories.
I'd love to see a library of well-maintained and thin C++ bindings around libc,
that do little more than turning errors into exceptions and making it also work
with std::string buffers.
ezfs.h
#ifndef EZFS_H
#define EZFS_H
#include <string>
#include <functional>
#include <memory>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
namespace ezfs
class Directory
protected:
int handle = -1;
public:
Directory(const std::string& pathname, int flags=0);
~Directory();
Directory(const Directory&) = delete;
Directory(const Directory&&) = delete;
Directory& operator=(const Directory&) = delete;
/// List the directory contents
void ls(std::function<void(const dirent&)> callback);
int open(const std::string& relpath, int flags, mode_t mode=0777);
;
std::string errno_str(int error=errno);
[ [noreturn]] void throw_libc_error(int error=errno);
#endif
#include "ezfs.h"
#include <stdexcept>
#include <memory>
#include <cstring>
#include <cstdlib>
#include <string>
#include <linux/limits.h>
using namespace std;
namespace ezfs
string errno_str(int error)
char buf[256];
#if (_POSIX_C_SOURCE >= 200112L _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
strerror_r(errno, buf, 256);
string res(buf);
#else
string res(strerror_r(errno, buf, 256));
#endif
return res;
[ [noreturn]] void throw_libc_error(int error)
throw runtime_error(errno_str(error));
Directory::Directory(const std::string& pathname, int flags)
handle = ::open(pathname.c_str(), O_PATH O_DIRECTORY flags);
if (handle < 0) throw_libc_error();
Directory::~Directory()
::close(handle);
void Directory::ls(std::function<void(const dirent&)> callback)
int fd = openat(handle, ".", O_DIRECTORY);
if (fd < 0) throw_libc_error();
// RAII Self-cleaning DIR object
unique_ptr<DIR, std::function<void(DIR*)>> dir(fdopendir(fd), [](DIR* dir) if (dir) closedir(dir); );
if (!dir)
// fdopendir(3): After a successful call to fdopendir(), fd is used
// internally by the implementation, and should not otherwise be used
// by the application.
//
// but if the fdopendir call was not successful, fd is not managed by
// DIR, and we still need to close it, otherwise we leak a file
// descriptor.
//
// However, close() may modify errno, so we take note of the errno set
// by fdopendir and raise the exception based on that.
int fdopendir_errno = errno;
close(fd);
throw_libc_error(fdopendir_errno);
// Size the dirent buffer properly
const unsigned len = offsetof(dirent, d_name) + PATH_MAX + 1;
unique_ptr<dirent, std::function<void(void*)>> dirbuf((dirent*)malloc(len), free);
while (true)
dirent* res;
int err = readdir_r(dir.get(), dirbuf.get(), &res);
// End of directory contents
if (err == 0)
if (res)
callback(*res);
else
break;
else
throw_libc_error(err);
int Directory::open(const std::string& relpath, int flags, mode_t mode)
int res = openat(handle, relpath.c_str(), flags, mode);
if (res < 0) throw_libc_error();
return res;
// This is almost the equivalent of running "sha256sum ."
ezfs::Directory dir(".");
dir.ls([&](const dirent& d)
if (d.d_type != DT_REG) return;
ezhash::Hash sha256(GCRY_MD_SHA256);
// I have no RAII wrapper around file handles at the moment, so
// I'll have to use a try/catch for cleaning up after errors
int fd = dir.open(d.d_name, O_RDONLY);
try
sha256.hash_file(fd);
close(fd);
catch (...)
close(fd);
throw;
cout << sha256.read_hex() << " " << d.d_name << endl;
);

There s a lot of discussion / moaning /arguing at this time, so I thought I d post something about how LAVA got into Debian Jessie, the work involved and the lessons I ve learnt. Hopefully, it will help someone avoid the disappointment of having their package missing the migration into a future stable release. This was going to be a talk at the Minidebconf-uk in Cambridge but I decided to put this out as a permanent blog entry in the hope that it will be a useful reference for the future, not just Jessie.
Context
LAVA relies on a number of dependencies which were at the time all this started NEW to Debian as well as many others already in Debian. I d been running LAVA using packages on my own system for a few months before the packages were ready for use on the main servers (I never actually installed LAVA using the old virtualenv method on my own systems, except in a VM). I did do quite a lot of this on my own but I also had a team supporting the effort and valuing the benefits of moving to a packaged system.
At the time, LAVA was based on Ubuntu (12.04 LTS Precise Pangolin) and a new Ubuntu LTS was close (Trusty Tahr 14.04) but I started work on this in 2013. By the time my packages were ready for general usage, it was winter 2013 and much too close to get anything into Ubuntu in time for Trusty. So I started a local repo using space provided by Linaro. At the same time, I started uploading the dependencies to Debian. json-schema-validator, django-testscenarios and others arrived in April and May 2014. (Trusty was released in April). LAVA arrived in NEW in May, being accepted into unstable at the end of June. LAVA arrived in testing for the first time in July 2014.
Upstream development continued apace and a regular monthly upload, with some hotfixes in between, continued until close to the freeze.
At this point, note that although upstream is a medium sized team, the Debian packaging also has a team but all the uploads were made by me. I planned ahead. I knew that I would be going to Macau for Linaro Connect in February a critical stage in the finalisation of the packages and the migration of existing instances from the old methods. I knew that I would be on vacation from August through to the end of September 2014 including at least two weeks with absolutely no connectivity of any kind.
Right at this time, Django1.7 arrived in experimental with the intent to go into unstable and hence into Jessie. This was a headache for me, I initially sought to delay the migration until after Jessie. However, we discussed it upstream, allocated time within the busy schedule and also sought help from within Debian with the RFH tag. Rapha l Hertzog contributed patches for django1.7 support and we worked on those patches upstream, once I was back from vacation. (The final week of my vacation was a work conference, so we had everyone together at one hacking table.)
Still there was more to do, the django1.7 patches allowed the unit tests to complete but broke other parts of the lava-server package and needed subsequent tweaks and fixes.
Even with all this, the auto-removal from testing for packages affected by RC bugs in their dependencies became very important to monitor (it still is). It would be useful if some packages had less complex dependency chains (I m looking at you, uwsgi) as the auto-removal also covers build-depends. This led to some more headaches with libmatheval. I m not good with functional programming languages, I did have some exposure to Scheme when working on Gnucash upstream but it wasn t pleasant. The thought of fixing a scheme problem in the test suite of libmatheval was daunting. Again though, asking for help, I found people in the upstream team who wanted to refresh their use of scheme and were able to help out. The fix migrated into testing in October.
Just for added complications, lava-server gained a few RC bugs of it s own during that time too fixed upstream but awkward nonetheless.
Achievement unlocked
So that s how a complex package like lava-server gets into stable. With a lot of help. The main problem with top-level packages like this is the sheer weight of the dependency chain. Something seemingly unrelated (like libmatheval) can seriously derail the migrations. The package doesn t use the matheval support provided by uwsgi. The bug in matheval wasn t in the parts of matheval used by uwsgi. It wasn t in a language I am at all comfortable in fixing but it s my name on the changelog of the NMU. That happened because I asked for help. OK, when django1.7 was scheduled to arrive in Debian unstable and I knew that lava was not ready, I reacted out of fear and anxiety. However, I sought help, help was provided and that help was enough to get upstream to a point where the side-effects of the required changes could be fixed.
Maintaining a top-level package in Debian is becoming more like maintaining a core package in Debian and that is a good thing. When your package has a lot of dependencies, those dependencies become part of the maintenance workload of your package. It doesn t matter if those are install time dependencies, build dependencies or reverse dependencies. It doesn t actually matter if the issues in those packages are in languages you would personally wish to be expunged from the archive. It becomes your problem but not yours alone.
Debian has a lot of flames right now and Enrico encouraged us to look at what else is actually happening in Debian besides those arguments. Well, on top of all this with lava, I also did what I could to help the arm64 port along and I m very happy that this has been accepted into Jessie as an official release architecture. That s a much bigger story than LAVA yet LAVA was and remains instrumental in how arm64 gained the support in the kernel and various upstreams which allowed patches to be accepted and fixes to be incorporated into Debian packages.
So a roll call of helpers who may otherwise not have been recognised via changelogs, in no particular order:
Modern web applications are much more complicated than the simple Perl CGI
scripts or PHP pages of the past. They usually start with a framework and
include lots of external components both on the front-end and on the back-end.
Here's an example from the Node.js back-end of a
I m writing this blog post on the plane from Portland towards Europe (
This is my monthly summary of my free software related activities. If you re among the people who
Following Enrico's