https://github.com/erikzenker/clangactiveobjectgenerator
Clang active object generator is a clang plugin for C++ code which generates an active object from a pure virtual class. An active object is a design pattern which decouples method execution from method invocation for objects that each reside in their own thread of control.
https://github.com/erikzenker/clangactiveobjectgenerator
activeobject clang-plugin cpp cpp14
Last synced: about 1 year ago
JSON representation
Clang active object generator is a clang plugin for C++ code which generates an active object from a pure virtual class. An active object is a design pattern which decouples method execution from method invocation for objects that each reside in their own thread of control.
- Host: GitHub
- URL: https://github.com/erikzenker/clangactiveobjectgenerator
- Owner: erikzenker
- Created: 2018-09-15T20:03:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-16T13:44:09.000Z (over 7 years ago)
- Last Synced: 2025-03-21T15:10:56.895Z (about 1 year ago)
- Topics: activeobject, clang-plugin, cpp, cpp14
- Language: C++
- Size: 34.2 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Clang Active Object Generator
=============================
**Clang active object generator** is a clang plugin for C++ code which generates an active object
from a pure virtual class. An [active object](https://en.wikipedia.org/wiki/Active_object) is a design
pattern which decouples method execution from method invocation for objects that each reside
in their own thread of control.

Usage
=====
1. Install the clang active object generator (see [Build Plugin](#build-plugin))
1. You need a header file with a pure virtual class inside e.g.: IPureVirtualClass.hpp.
```c++
#pragma once
class IPureVirtualClass {
public:
virtual ~IPureVirtualClass() = default;
virtual void foo(int a) = 0;
};
```
2. Call the generator script with the header file name and the interface name. The script will
print a generated interface implementation using the active object pattern to standard out.
```bash
clang-active-object-generator.sh IPureVirtualClass.hpp IPureVirtualClass \
> IPureVirtualClassActiveObject.hpp
```
3. The generator script will create an active object header file for you e.g.:
```c++
#pragma once
/*
* This file was generated by the ClangActiveObjectGenerator
*/
#include "IPureVirtualClass.hpp"
#include
#include
class IPureVirtualClassActiveObject : public IPureVirtualClass {
public:
IPureVirtualClassActiveObject(const std::shared_ptr& impl, boost::asio::io_service& ioService)
: m_impl(impl)
, m_ioService(ioService)
{
}
public: // IPureVirtualClass
void foo(int a) override {
m_ioService.post(
[this, a](){
m_impl.lock()->foo(a);
});
}
private:
std::weak_ptr m_impl;
boost::asio::io_service& m_ioService;
};
#ifndef MAKE_ACTIVE_OBJECT
#define MAKE_ACTIVE_OBJECT
template
class MakeActiveObject {};
template
std::unique_ptr
make_active_object(const std::shared_ptr& impl, const std::shared_ptr& executor)
{
return MakeActiveObject{}(impl, executor);
}
#endif
template
class MakeActiveObject {
public:
std::unique_ptr operator()(const std::shared_ptr& impl, const std::shared_ptr& executor){
return std::make_unique>(impl, executor);
}
};
```
4. Finally, include the generated active object header into your sources and call methods on the active object
```c++
#include "IPureVirtualClass.hpp" // <-- original interface header
#include "IPureVirtualClassActiveObject.hpp" // <-- generated active object header
#include
#include
#include
class Impl : public IPureVirtualClass {
void foo(int a){
std::cout << a << std::endl;
}
}
int main()
{
// Thread loop in which the active object method calls will be executed
auto ioService = std::make_shared();
// The implementation of the interface
auto impl = std::make_shared();
// Generates the active object
auto implActiveObject = make_active_object(impl, ioService);
// Puts the method call into the thread loop
implActiveObject->foo(42);
// Executes the method call (prints 42)
std::thread t0([ioService]() { ioService->run(); });
t0.join();
return 0;
}
```
Build Plugin
=============
```bash
mkdir build; cd build
cmake ..
cmake --build . --target install
```
Build Example
=============
Build the plugin in advance
```bash
mkdir build; cd build
cmake ..
cmake --build . --target clang_active_object_generator_example
./example/clang_active_object_generator_example
```
Dependencies
============
* C++14
* llvm 6.0.1-4
* clang 6.0.1-2
License
=======
MIT
Author
======
Written by Erik Zenker (erikzenker (at) hotmail.com)