Giter Site home page Giter Site logo

gob_adapter's Introduction

gob_adapter

日本語

Overview

This is a library for using your own functions consisting of public functions of existing classes as if they were class functions of existing classes.
It can be used with C++11 or later.
It has the following features and limitations

Features

  • Can be encapsulated by classifying an ordinary function that uses the original class.
  • Does not modify the source code of the original class
  • "has-a" so there are no side effects on the original class (does not cause problems due to inheritance)
  • The use of arrow and dot operators is a little confusing, but it makes the difference between the original class function and a call to your own function clear.
  • Little impact from changes in the original class

Limitations

  • Not inherited, so only public functions of existing classes can be used.

How to install

Install in an appropriate way depending on your environment.

  • git clone or download zip, and extract into place
  • platformio.ini
lib_deps = https://github.com/GOB52/gob_adapter.git

How to use

#include <gob_adapter.hpp>

// Original class
class Original
{
  public:
    explicit Original(int v) : _v(v) {}
    int value() const { return _v; }
  private:
    int _v{};
};

// Extened class
// Add function to return squared value
class Extended : public goblib::plugin::Adapter<Original>
{
    using goblib::plugin::Adapter<Original>::_cls;
  public:
    using goblib::plugin::Adapter<Original>::Adapter; // for constructor

    int square() const { return _cls->value() * _cls->value(); }
};

Original org(52);
Extended  extended(org);

void foo()
{
    printf("Value:%d\nSquare:%d\n",
           extended->value(), // Call Original::value()
           extended.square()  // Call Extended::square()
           );

    Extended* ep = &extended;
    printf("Value:%d\nSquare:%d\n%d\n",
           (*ep)->value(), // Call Original::value()
           ep->square(),   // Call Extended::square()
           (*ep).square()  // Call Extended::square()
           );
}

Examples

  • simple A simple example
  • M5GFX Example of adding your own drawing functions to LovyanGFX

gob_adapter's People

Contributors

gob52 avatar

Stargazers

Jota CG avatar tobozo 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.