]> granicus.if.org Git - graphviz/commitdiff
add a GVRenderData class
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Thu, 15 Jul 2021 12:37:33 +0000 (14:37 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Sat, 7 Aug 2021 04:54:12 +0000 (06:54 +0200)
Towards https://gitlab.com/graphviz/graphviz/-/issues/2001.

lib/gvc++/CMakeLists.txt
lib/gvc++/GVRenderData.cpp [new file with mode: 0644]
lib/gvc++/GVRenderData.h [new file with mode: 0644]

index fca773ac24f770d51cdde32793a02a6fedaed262..a009510495c61b4dfcf4e7a309bc15445053809f 100644 (file)
@@ -3,6 +3,8 @@ add_library(gvc++ SHARED
   GVContext.cpp
   GVLayout.h
   GVLayout.cpp
+  GVRenderData.h
+  GVRenderData.cpp
   )
 
 set_target_properties(gvc++ PROPERTIES CXX_STANDARD 20)
@@ -34,6 +36,7 @@ install(
   FILES
   GVContext.h
   GVLayout.h
+  GVRenderData.h
   DESTINATION ${HEADER_INSTALL_DIR}
   )
 
diff --git a/lib/gvc++/GVRenderData.cpp b/lib/gvc++/GVRenderData.cpp
new file mode 100644 (file)
index 0000000..6e3d47a
--- /dev/null
@@ -0,0 +1,10 @@
+#include "GVRenderData.h"
+#include <gvc/gvc.h>
+
+namespace GVC {
+
+GVRenderData::GVRenderData(char *data, std::size_t length)
+    : m_data(data), m_length(length) {}
+GVRenderData::~GVRenderData() { gvFreeRenderData(m_data); }
+
+} // namespace GVC
diff --git a/lib/gvc++/GVRenderData.h b/lib/gvc++/GVRenderData.h
new file mode 100644 (file)
index 0000000..94cdd4d
--- /dev/null
@@ -0,0 +1,65 @@
+#pragma once
+
+#include <cstddef>
+#include <string_view>
+
+#ifdef _WIN32
+#if gvc___EXPORTS // CMake's substitution of gvc++_EXPORTS
+#define GVRENDER_API __declspec(dllexport)
+#else
+#define GVRENDER_API __declspec(dllimport)
+#endif
+#else
+#define GVRENDER_API /* nothing */
+#endif
+
+namespace GVC {
+
+class GVLayout;
+
+/**
+ * @brief The GVRenderData class represents a rendered layout in a specific text
+ * format
+ */
+
+class GVRENDER_API GVRenderData {
+public:
+  ~GVRenderData();
+
+  // delete copy for now since we cannot use default because we manage a C
+  // string using a raw pointer
+  GVRenderData(GVRenderData &) = delete;
+  GVRenderData &operator=(GVRenderData &) = delete;
+
+  // delete move for now since we cannot use default because we manage a C
+  // string using a raw pointer
+  GVRenderData(GVRenderData &&) = delete;
+  GVRenderData &operator=(GVRenderData &&) = delete;
+
+  // get the rendered string as a C string. The string is null terminated, but
+  // that is not useful for binary formats. Combine with the length method for
+  // that case.
+  char *c_str() const { return m_data; }
+
+  // get the length of the rendered string
+  std::size_t length() const { return m_length; }
+
+  // get the rendered string as a string view
+  std::string_view string_view() const {
+    return std::string_view{m_data, m_length};
+  }
+
+  friend GVLayout;
+
+private:
+  // use GVLayout::render to constuct a GVRenderData object
+  GVRenderData(char *rendered_data, std::size_t length);
+
+  // the underlying C data structure
+  char *m_data = nullptr;
+  std::size_t m_length = 0;
+};
+
+} //  namespace GVC
+
+#undef GVRENDER_API