Git Product home page Git Product logo

samples's Introduction

Rule of Zero PIMPL

A single-header C++ library for PIMPLs without having to implement any special member functions as described in oliora's blog.

Code Samples

Header File:

#include "spimpl.h"

class Copyable {
public:
  Copyable(int x);

  int x();

  // All five special members are compiler-generated.

private:
  class Impl;

  // Movable and copyable Smart PIMPL
  spimpl::impl_ptr<Impl> impl_;
};

Implementation File:

Copyable::Copyable(int x)
: impl_(spimpl::make_impl<Impl>(x)) {}

struct Copyable::Impl {
  Impl(int x): x(x) {}

  int x;
};

int Copyable::x() {
  return impl_->x;
}

GDB Support

For easier debugging with GDB you might want to add the following to your .gdbinit file. This makes *impl_ and impl-> behave more naturally in GDB.

python
import gdb
import gdb.xmethod
import libstdcxx.v6.xmethods
from libstdcxx.v6 import register_libstdcxx_printers

register_libstdcxx_printers (None)

class SpimplGetWorker(gdb.xmethod.XMethodWorker):
    def __init__(self, elem_type): self.elem_type = elem_type
    def get_arg_types(self): return None
    def get_result_type(self, obj): return self.elem_type.pointer()
    def __call__(self, obj):
        ptr = obj['ptr_']
        eval_string = '(*(%s*)(%s)).get()'%(ptr.type, ptr.address)
        return gdb.parse_and_eval(eval_string)

class SpimplDerefWorker(SpimplGetWorker):
    def __init__(self, elem_type): SpimplGetWorker.__init__(self, elem_type)
    def get_arg_types(self): return None
    def get_result_type(self, obj): return self.elem_type
    def __call__(self, obj):
        return SpimplGetWorker.__call__(self, obj).dereference()

class SpimplMethodsMatcher(gdb.xmethod.XMethodMatcher):
    def __init__(self):
        gdb.xmethod.XMethodMatcher.__init__(self, 'spimpl')
        self._method_dict = {
            'operator->': libstdcxx.v6.xmethods.LibStdCxxXMethod('operator->', SpimplGetWorker),
            'operator*': libstdcxx.v6.xmethods.LibStdCxxXMethod('operator*', SpimplDerefWorker),
        }
        self.methods = [self._method_dict[m] for m in self._method_dict]

    def match(self, class_type, method_name):
        if not re.match('^spimpl::impl_ptr<.*>$', class_type.tag):
            return None
        method = self._method_dict.get(method_name)
        if method is None or not method.enabled:
            return None
        return method.worker_class(class_type.template_argument(0))

gdb.xmethod.register_xmethod_matcher(None, SpimplMethodsMatcher())
end

samples's People

Contributors

oliora avatar saraedum avatar

Watchers

 avatar

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.