Code Monkey home page Code Monkey logo

boost_reflect's Introduction

Boost.Reflect - Simple Reflection Library

The Boost Reflect library provides a means to describe the interface of a class or struct to provide compile-time and run-time reflection.

Some of the useful features provided by the Reflect library include:

  • boost::reflect::any - a class that can contain any object that implements Interface.
  • Use visitors to create dynamic interfaces.
    • RPC client/server stubs
    • Command Line Interface
    • Scripting Interfaces
    • Asynchronous Actors

Example: Simple Command Line Calculator

An example of how to create a command line calculator can be found in the examples directory.

#include <boost/reflect/any.hpp>

struct calculator {
    calculator():result_(0){}
    double add( double v ) { return result_ += v; }
    double sub( double v ) { return result_ -= v; }
    double result()        { return result_;      }

    private:
        double result_;
};

BOOST_REFLECT_ANY( calculator, (add)(sub)(result) )

int main( int argc, char** argv ) {
  boost::reflect::any_ptr<Calculator> s( new CalculatorService() );
  printf( "Result: %f\n", s->result() );

  cli  m_cli(s);

  std::string line;
  std::string cmd;
  std::string args;

  while( true ) {
      std::cerr << "Enter Method: ";
      std::getline( std::cin, line );
      cmd = line.substr( 0, line.find('(') );
      args = line.substr( cmd.size(), line.size() );
      std::cerr << m_cli[cmd](args) << std::endl;
  }

  return 0;
}

Output

add        double(double)
sub        double(double)
result     double()const

Enter Method: add(5)
5
Enter Method: sub(3)
2
Enter Method: result()
2

Boost.CMT can be built by checking out my development repository: https://github.com/bytemaster/dev

Installation

git clone https://github.com/bytemaster/dev
cd dev
git submodule init
git submodule update
cmake .
make
make install
@endcode

boost_reflect's People

Contributors

bytemaster avatar

Watchers

James Cloos avatar LiangYue 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.