#include <iostream>
#include <memory>
#include <string>
#include "include/cppgc/allocation.h"
#include "include/cppgc/default-platform.h"
#include "include/cppgc/garbage-collected.h"
#include "include/cppgc/heap.h"
#include "include/cppgc/member.h"
#include "include/cppgc/visitor.h"
#if !CPPGC_IS_STANDALONE
#include "include/v8-initialization.h"
#endif
* This sample program shows how to set up a stand-alone cppgc heap.
*/
* Simple string rope to illustrate allocation and garbage collection below.
* The rope keeps the next parts alive via regular managed reference.
*/
class Rope final : public cppgc::GarbageCollected<Rope> {
public:
explicit Rope(std::string part, Rope* next = nullptr)
: part_(std::move(part)), next_(next) {}
void Trace(cppgc::Visitor* visitor) const { visitor->Trace(next_); }
private:
const std::string part_;
const cppgc::Member<Rope> next_;
friend std::ostream& operator<<(std::ostream& os, const Rope& rope) {
os << rope.part_;
if (rope.next_) {
os << *rope.next_;
}
return os;
}
};
int main(int argc, char* argv[]) {
auto cppgc_platform = std::make_shared<cppgc::DefaultPlatform>();
#if !CPPGC_IS_STANDALONE
v8::V8::InitializePlatform(cppgc_platform->GetV8Platform());
#endif
cppgc::InitializeProcess(cppgc_platform->GetPageAllocator());
{
std::unique_ptr<cppgc::Heap> heap = cppgc::Heap::Create(cppgc_platform);
Rope* greeting = cppgc::MakeGarbageCollected<Rope>(
heap->GetAllocationHandle(), "Hello ",
cppgc::MakeGarbageCollected<Rope>(heap->GetAllocationHandle(),
"World!"));
heap->ForceGarbageCollectionSlow("CppGC example", "Testing");
std::cout << *greeting << std::endl;
}
cppgc::ShutdownProcess();
return 0;
}