Code Monkey home page Code Monkey logo

type_erasure's Introduction

type_erasure

This repo serves multiple purposes.

  • First, it contains several "hand-rolled" implementations of the same erased type, for reference.

  • Second, it includes the "Type Erasure" presentation given at CppCon 2014.

  • Third, it contains the emtypen tool, which generates type erasure code based on a user-supplied implementation form.

Skip to the relevant section below, depending on what you came here for. Build instructions can be found at the end.

Reference Implmentations

Check out the hand_rolled and boost_type_erasure directories for examples of various approaches to type erasure. Note that there are emtypen form and header files for most of the approaches -- everything but the manual v-table and Boost.TypeErasure approaches. All examples were written against the C++11 standard.

Presentation

If you want to look at the presentation slides live in the tubes, go to:

http://tzlaine.github.io/type_erasure

Otherwise, the CppCon 2014 presentation can be found in index.html, with the supporting files found in the presentation directory. Point your browser at index.html and get ready to rumble. I don't know why I said that last part.

emtypen

emtypen generates C++ code for one or more "erased types" (types that can hold anything that fits a given interface) from a C++ header that defines the required interface(s). It performs this magic by asking the libclang library (a wrapper around the Clang front end) what is in the given C++ header. Detailed usage instructions can be found by passing --help or --manual to emtypen.

A pre-built Windows installer is available here.

A pre-built Mac OS (Mavericks only) installer is available here.

Build Instructions

First, note that the code in this repo is written against the C++11 standard. It is known to work with Clang 3.4, GCC 4.9, and Visual Studio 2013. It may work with earier versions of Clang or GCC, but will not work with any earlier version of Visual Studio.

To build the entire project, simply install CMake 3.0 and use it to build this repo. Specific steps are given at the end.

Many of the examples have coverage tests that require headers from Boost.Test. The Boost.TypeErasure example also obviously requires Boost. Neither requires a compiled Boost library; the headers will suffice. If you want to build these items, define the CMake variable BOOST_ROOT (see below). If you do not want to build these items, it won't break anything not to define BOOST_ROOT.

emtypen requires libclang headers and libs. If you want to build emtypen, define the CMake variable CLANG_ROOT (see below). If you do not want to build emtypen, it won't break anything not to define CLANG_ROOT. At the time of this writing, there are no pre-built Clang binaries built with Visual Studio 2013, but it's now very easy to build Clang from source with Visual Studio. There's a small wrinkle, however. CMake doesn't like import libs with extensions other than .lib, and libclang's is built as libclang.imp. There's probably some obscure CMake incantation that fixes this, but it's easier just to rename libclang.imp to libclang.lib.

If you build Clang from sources, it will make your life much easier to install it somewhere, and then set CLANG_ROOT to the installed location. This will set up the directory structure expected by the emtypen build; the structure of the build products for the built-in-place Clang is different.

Here are the specific steps to build this repo with CMake. It is assumed that this repo has been cloned into a directory caled "type_erasure":

  • From a command shell, create a directory into which the build products will be generated, e.g. type_erasure/build.

  • Move into the build directory.

  • Execute cmake <path> [-DBOOST_ROOT=<path_to_boost>] [-DCLANG_ROOT=<path_to_clang>, where <path> is the relative path from your build directory to "type_erasure", e.g. ...

This will generate your build files. On UNIX-like systems this will default to generating makefiles, and on Windows this will default to generating Visual Studio solution files. You know what to do from here.

type_erasure's People

Contributors

tzlaine avatar

Stargazers

Sandalots avatar Christopher Kormanyos avatar 五十絃 avatar Thomas Bowley avatar  avatar Andrei Pastramagiu avatar  avatar Alexandre Larouche avatar Ethan Slattery avatar  avatar Subhransu avatar NotAPenguin avatar  avatar H avatar wonko_rt avatar Alastair Harrison avatar Bater.Makhabel avatar Viet Nguyen avatar  avatar Michaël avatar Andrey avatar Bret Kuhns avatar jamestillercode avatar Eric Pederson avatar  avatar Daniel Oberhoff avatar Aki avatar Jonathan Beard avatar Kittinun Vantasin avatar Barrett Adair avatar Greg Beard avatar abdul dakkak avatar Chiu-Hsiang Hsu avatar  avatar Miro Knejp avatar Izaak "Zaak" Beekman avatar Jan Pipek avatar Ondřej Čertík avatar qiao avatar Maciej Zimnoch avatar Gaetano Checinski avatar Florian Rappl avatar Prateek Raman avatar Ali Baharev avatar Vincent Esche avatar Brian Coleman avatar  avatar wood avatar Dave Oleksy avatar Luke Queenan avatar Michal Mocny avatar C.S. Lim avatar Jonas Maaskola avatar Victor Zverovich avatar JC Berleur avatar gnzlbg avatar José Manuel Barroso Galindo avatar johannes algelind avatar Bob Somers avatar Andreas Reischuck avatar Kevin Dungs avatar Matthew Chaplain avatar Gregory Petrosyan avatar Gregor Burger avatar Francisco Lopes avatar Paul Fultz II avatar Kristian Golding avatar

Watchers

Matt Calabrese avatar  avatar Jeff Garrett avatar James Cloos avatar Paul Fultz II avatar Barrett Adair avatar  avatar Anders Wang Kristensen avatar Ghyslain Leclerc avatar

Forkers

lubkoll nlyan

type_erasure's Issues

how to update emtypen?

I slightly adjusted the script according current python requirements and new clang bindings in clang subdirectory. It does not complain anymore.
Could you clarify the execution?
./emtypen.py --form form.hpp --headers headers.hpp --out-file file.hpp test_archetypes.hpp

gives:

"File... emtypen.py", line 442, in print_diagnostic
    os.write(2, '{file_}:{line}:{column} {severity}: {spelling}\n'.format(**locals()))
TypeError: a bytes-like object is required, not 'str'

thanks

Using T&& for perfect forwarding

In order for perfect forwarding to work, it is necessary that the type of value be T&&. For example, this function in hand_rolled.hpp will copy by value, where you intended perfect forwarding:

// hand_rolled.hpp
template <typename T>
printable & operator= (T value)   // should be T&& if we want perfect forwarding
{
    printable temp(std::forward<T>(value));
    ...
}

Normally, template type deduction cares nothing about the value category(lvalue or rvalue) and therefore T will not tell us anything about whether the item can be forwarding. We need to use T&& (where T is being deduced) to ask the compiler to 'encode' the value category of the expression in the referencedness of T.

Otherwise, the value will be copied by value, which will lead to problems with move-only types

(I came across this after watching your CppCon talk. Excellent talk!)

Extra: to be clear, operator= will take value by value. This is what we want to avoid as it will involve a copy. The call from operator= to the constructor of the temp object will be by reference, equivalent to move-ing from value. But the copy used to create value cannot be avoided unless you use T&&. You can try this with a type T for which the copy constructor has been =deleted. It will not be possible to call operator= with such a type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.