Online PImpl generation#
This page enables you to use the PImpl generator on your own code.
You can open it in an online interactive environment (mybinder), where you can edit the code below.
To launch the online tool, click on the rocket icon at the top right of this page, or use this link
The interactive environment will open in a new tab. Wait for it to be ready
Once it is launched, click on “Run Cell” to run each of the cells:
Below, you can use the PImpl generator on your own code. Just modify the C++ code and the options below.
import srcmlcpp
from srcmlcpp_tools import pimpl_my_class
from litgen.demo import litgen_demo
srcmlcpp_options = srcmlcpp.SrcmlcppOptions()
# Optional: adjust srcmlcpp options if needed
pimpl_options = pimpl_my_class.PimplOptions()
pimpl_options.pimpl_suffixes = ["PImpl"]
cpp_code = """
#include "my_class.h"
#include <string>
#include <future>
// Some doc about the class, that you want to see in the header file
class MyClassPImpl
{
//
// Some doc you also want to see in the header file
//
public:
// Construct an Instance
MyClassPImpl(const std::string& someParam)
{
// Some code you provide in the C++ file, but do not want to see in the header file
}
// Destructs the instance
// (this should not be published in the header, since the PImpl will generate its own unique_ptr destructor)
~MyClassPImpl() { /* ... */ }
// Some method
bool SomeMethod() { /* ... */ return true; }
// Some public static method
static bool SomeStaticFunction() { /* ... */ return true;}
private:
void SomePrivateMethod() { /* ... */ }
std::string mSomePrivateMember;
std::future<void> mAnoterPrivateMember;
};
"""
pimpl_result = pimpl_my_class.pimpl_my_code(cpp_code, pimpl_options, srcmlcpp_options)
if pimpl_result is None:
print("Failed generated PImpl")
else:
litgen_demo.show_cpp_code(pimpl_result.header_code, "Header Code")
litgen_demo.show_cpp_code(pimpl_result.glue_code, "Glue Code (paste this in the C++ file)")
class MyClassPImpl;
// Some doc about the class, that you want to see in the header file
class MyClass
{
public:
// Construct an Instance
MyClass(const std::string & someParam);
// Some method
bool SomeMethod();
// Some public static method
static bool SomeStaticFunction();
~MyClass();
private:
std::unique_ptr<MyClassPImpl> mPImpl;
};
MyClass::MyClass(const std::string & someParam)
: mPImpl(std::make_unique<MyClassPImpl>(someParam)) { }
bool MyClass::SomeMethod() {
return mPImpl->SomeMethod(); }
bool MyClass::SomeStaticFunction() {
return MyClassPImpl::SomeStaticFunction(); }
MyClass::~MyClass() = default;