Debian Codegraph @

lib/gvc++/GVLayout.cpp

raw
1 #include "config.h"
2
3 #include <cassert>
4 #include <cstddef>
5 #include <memory>
6 #include <stdexcept>
7 #include <string>
8 #include <utility>
9
10 #include "GVContext.h"
11 #include "GVLayout.h"
12 #include "GVRenderData.h"
13 #include <cgraph++/AGraph.h>
14 #include <gvc/gvc.h>
15
16 namespace GVC {
17
18 GVLayout::GVLayout(const std::shared_ptr<GVContext> &gvc,
19 const std::shared_ptr<CGraph::AGraph> &g,
20 const std::string &engine)
21 : m_gvc(gvc), m_g(g) {
22 if (gvLayoutDone(g->c_struct())) {
23 gvFreeLayout(gvc->c_struct(), g->c_struct());
24 throw std::runtime_error("Previous layout not yet destroyed");
25 }
26 const auto rc = gvLayout(gvc->c_struct(), g->c_struct(), engine.c_str());
27 if (rc) {
28 throw std::runtime_error("Layout failed");
29 }
30 }
31
32 GVLayout::GVLayout(GVContext &&gvc, CGraph::AGraph &&g,
33 const std::string &engine)
34 : GVLayout(std::make_shared<GVContext>(std::move(gvc)),
35 std::make_shared<CGraph::AGraph>(std::move(g)), engine) {}
36
37 GVLayout::GVLayout(std::shared_ptr<GVContext> gvc, CGraph::AGraph &&g,
38 const std::string &engine)
39 : GVLayout(gvc, std::make_shared<CGraph::AGraph>(std::move(g)), engine) {}
40
41 GVLayout::GVLayout(GVContext &&gvc, std::shared_ptr<CGraph::AGraph> g,
42 const std::string &engine)
43 : GVLayout(std::make_shared<GVContext>(std::move(gvc)), g, engine) {}
44
45 GVLayout::~GVLayout() {
46 // m_gvc and m_g always either both point to an object or both are null
47 assert((m_gvc != nullptr && m_g != nullptr) ||
48 (m_gvc == nullptr && m_g == nullptr));
49 if (!m_gvc || !m_g) {
50 return;
51 }
52 gvFreeLayout(m_gvc->c_struct(), m_g->c_struct());
53 }
54
55 GVRenderData GVLayout::render(const std::string &format) const {
56 char *result = nullptr;
57 size_t length = 0;
58 const auto rc = gvRenderData(m_gvc->c_struct(), m_g->c_struct(),
59 format.c_str(), &result, &length);
60 if (rc) {
61 if (result) {
62 gvFreeRenderData(result);
63 }
64 throw std::runtime_error("Rendering failed");
65 }
66
67 return GVRenderData(result, length);
68 }
69
70 } // namespace GVC