summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2022-06-02 20:41:44 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2022-06-02 20:58:32 -0400
commitd648800e83da57c7f8d554e2ed5d6802d03719da (patch)
tree4f29f09cee7d0e7923325be0d7d94c8de7d0ddb2
parent81cbe7cdad8f2c1e0d9bff4ab28b9c8f6661f8fa (diff)
downloadorcus-d648800e83da57c7f8d554e2ed5d6802d03719da.tar.gz
Add ENSURE_INTERFACE macro to remove repetitive interface checks
Also add a class doc to the exception being thrown when the check fails.
-rw-r--r--include/orcus/exception.hpp4
-rw-r--r--slickedit/cpp.vpj1
-rw-r--r--src/liborcus/Makefile.am1
-rw-r--r--src/liborcus/gnumeric_sheet_context.cpp13
-rw-r--r--src/liborcus/impl_utils.hpp18
-rw-r--r--src/liborcus/odf_styles_context.cpp25
-rw-r--r--src/liborcus/xls_xml_context.cpp25
-rw-r--r--src/liborcus/xlsx_context.cpp13
8 files changed, 51 insertions, 49 deletions
diff --git a/include/orcus/exception.hpp b/include/orcus/exception.hpp
index 2cc8e4a5..3d82535b 100644
--- a/include/orcus/exception.hpp
+++ b/include/orcus/exception.hpp
@@ -75,6 +75,10 @@ public:
virtual ~xpath_error() noexcept;
};
+/**
+ * This gets thrown when a public interface method is expected to return a
+ * non-null pointer to another interface but actually returns a null pointer.
+ */
class ORCUS_PSR_DLLPUBLIC interface_error : public general_error
{
public:
diff --git a/slickedit/cpp.vpj b/slickedit/cpp.vpj
index 98ec986a..1e494762 100644
--- a/slickedit/cpp.vpj
+++ b/slickedit/cpp.vpj
@@ -293,6 +293,7 @@
<F N="../src/liborcus/gnumeric_tokens.cpp"/>
<F N="../src/liborcus/gnumeric_tokens.hpp"/>
<F N="../src/liborcus/gnumeric_tokens.inl"/>
+ <F N="../src/liborcus/impl_utils.hpp"/>
<F N="../src/liborcus/info.cpp"/>
<F N="../src/liborcus/interface.cpp"/>
<F N="../src/liborcus/json_document_tree.cpp"/>
diff --git a/src/liborcus/Makefile.am b/src/liborcus/Makefile.am
index 8c84d232..df4d8e5c 100644
--- a/src/liborcus/Makefile.am
+++ b/src/liborcus/Makefile.am
@@ -49,6 +49,7 @@ liborcus_@ORCUS_API_VERSION@_la_SOURCES = \
formula_result.hpp \
formula_result.cpp \
global.cpp \
+ impl_utils.hpp \
info.cpp \
interface.cpp \
json_document_tree.cpp \
diff --git a/src/liborcus/gnumeric_sheet_context.cpp b/src/liborcus/gnumeric_sheet_context.cpp
index f42bd8bf..2c7a3c46 100644
--- a/src/liborcus/gnumeric_sheet_context.cpp
+++ b/src/liborcus/gnumeric_sheet_context.cpp
@@ -10,6 +10,7 @@
#include "gnumeric_token_constants.hpp"
#include "gnumeric_namespace_types.hpp"
#include "gnumeric_helper.hpp"
+#include "impl_utils.hpp"
#include <orcus/global.hpp>
#include <orcus/spreadsheet/import_interface.hpp>
@@ -522,8 +523,7 @@ void gnumeric_sheet_context::start_font(const xml_attrs_t& attrs)
return;
auto* font_style = styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
for (const auto& attr : attrs)
{
@@ -605,12 +605,10 @@ void gnumeric_sheet_context::start_style(const xml_attrs_t& attrs)
return;
auto* fill_style = styles->get_fill_style();
- if (!fill_style)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
+ ENSURE_INTERFACE(fill_style, import_fill_style);
auto* cell_protection = styles->get_cell_protection();
- if (!cell_protection)
- throw interface_error("implementer must provide a concrete instance of import_cell_protection.");
+ ENSURE_INTERFACE(cell_protection, import_cell_protection);
bool fill_set = false;
bool protection_set = false;
@@ -750,8 +748,7 @@ void gnumeric_sheet_context::end_font()
return;
auto* font_style = styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
font_style->set_color(0, front_color.red, front_color.green, front_color.blue);
font_style->set_name(chars);
diff --git a/src/liborcus/impl_utils.hpp b/src/liborcus/impl_utils.hpp
new file mode 100644
index 00000000..9b8cd2e4
--- /dev/null
+++ b/src/liborcus/impl_utils.hpp
@@ -0,0 +1,18 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#pragma once
+
+#define ENSURE_INTERFACE(PTR, NAME) \
+ do \
+ { \
+ if (!PTR) \
+ throw orcus::interface_error( \
+ "implementer must provide a concrete instance of " #NAME "."); \
+ } while (false)
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/src/liborcus/odf_styles_context.cpp b/src/liborcus/odf_styles_context.cpp
index 45d37347..81c68afc 100644
--- a/src/liborcus/odf_styles_context.cpp
+++ b/src/liborcus/odf_styles_context.cpp
@@ -9,6 +9,7 @@
#include "odf_namespace_types.hpp"
#include "odf_token_constants.hpp"
#include "odf_helper.hpp"
+#include "impl_utils.hpp"
#include <orcus/measurement.hpp>
#include <orcus/spreadsheet/import_interface.hpp>
@@ -461,8 +462,7 @@ void styles_context::start_text_properties(const xml_token_pair_t& parent, const
// Commit the font data.
auto* font_style = mp_styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
if (font_name)
font_style->set_name(*font_name);
@@ -689,8 +689,7 @@ void styles_context::start_table_cell_properties(const xml_token_pair_t& parent,
if (bg_color)
{
auto* fill_style = mp_styles->get_fill_style();
- if (!fill_style)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
+ ENSURE_INTERFACE(fill_style, import_fill_style);
fill_style->set_pattern_type(ss::fill_pattern_t::solid);
fill_style->set_fg_color(255, bg_color->red, bg_color->green, bg_color->blue);
@@ -700,8 +699,7 @@ void styles_context::start_table_cell_properties(const xml_token_pair_t& parent,
if (!border_styles.empty())
{
auto* border_style = mp_styles->get_border_style();
- if (!border_style)
- throw interface_error("implementer must provide a concrete instance of import_border_style.");
+ ENSURE_INTERFACE(border_style, import_border_style);
for (const auto& [dir, details] : border_styles)
{
@@ -716,8 +714,7 @@ void styles_context::start_table_cell_properties(const xml_token_pair_t& parent,
if (cell_protection_set)
{
auto* cell_protection = mp_styles->get_cell_protection();
- if (!cell_protection)
- throw interface_error("implementer must provide a concrete instance of import_cell_protection.");
+ ENSURE_INTERFACE(cell_protection, import_cell_protection);
if (hidden)
cell_protection->set_hidden(*hidden);
@@ -758,20 +755,16 @@ void styles_context::commit_default_styles()
return;
auto* font_style = mp_styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
auto* fill_style = mp_styles->get_fill_style();
- if (!fill_style)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
+ ENSURE_INTERFACE(fill_style, import_fill_style);
auto* border_style = mp_styles->get_border_style();
- if (!border_style)
- throw interface_error("implementer must provide a concrete instance of import_border_style.");
+ ENSURE_INTERFACE(border_style, import_border_style);
auto* cell_protection = mp_styles->get_cell_protection();
- if (!cell_protection)
- throw interface_error("implementer must provide a concrete instance of import_cell_protection.");
+ ENSURE_INTERFACE(cell_protection, import_cell_protection);
// Set default styles. Default styles must be associated with an index of 0.
// Set empty styles for all style types before importing real styles.
diff --git a/src/liborcus/xls_xml_context.cpp b/src/liborcus/xls_xml_context.cpp
index 0b76b141..64c98f75 100644
--- a/src/liborcus/xls_xml_context.cpp
+++ b/src/liborcus/xls_xml_context.cpp
@@ -9,6 +9,7 @@
#include "xls_xml_namespace_types.hpp"
#include "xls_xml_token_constants.hpp"
#include "spreadsheet_iface_util.hpp"
+#include "impl_utils.hpp"
#include <orcus/spreadsheet/import_interface.hpp>
#include <orcus/spreadsheet/import_interface_styles.hpp>
@@ -1840,8 +1841,7 @@ void xls_xml_context::commit_default_style()
return;
ss::iface::import_font_style* font_style = styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
if (font_style)
{
@@ -1860,21 +1860,15 @@ void xls_xml_context::commit_default_style()
}
ss::iface::import_fill_style* fill_style = styles->get_fill_style();
- if (!fill_style)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
-
+ ENSURE_INTERFACE(fill_style, import_fill_style);
fill_style->commit();
auto* border_style = styles->get_border_style();
- if (!border_style)
- throw interface_error("implementer must provide a concrete instance of import_border_style.");
-
+ ENSURE_INTERFACE(border_style, import_border_style);
border_style->commit();
auto* cell_protection = styles->get_cell_protection();
- if (!cell_protection)
- throw interface_error("implementer must provide a concrete instance of import_cell_protection.");
-
+ ENSURE_INTERFACE(cell_protection, import_cell_protection);
cell_protection->commit();
styles->commit_number_format();
@@ -1906,8 +1900,7 @@ void xls_xml_context::commit_styles()
for (const std::unique_ptr<style_type>& style : m_styles)
{
auto* font_style = styles->get_font_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(font_style, import_font_style);
font_style->set_bold(style->font.bold);
font_style->set_italic(style->font.italic);
@@ -1921,8 +1914,7 @@ void xls_xml_context::commit_styles()
styles->set_xf_font(font_id);
auto* fill_style = styles->get_fill_style();
- if (!font_style)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
+ ENSURE_INTERFACE(fill_style, import_fill_style);
if (style->fill.solid)
{
@@ -1942,8 +1934,7 @@ void xls_xml_context::commit_styles()
styles->set_border_count(style->borders.size());
auto* border_style = styles->get_border_style();
- if (!border_style)
- throw interface_error("implementer must provide a concrete instance of import_border_style.");
+ ENSURE_INTERFACE(border_style, import_border_style);
for (const border_style_type& b : style->borders)
{
diff --git a/src/liborcus/xlsx_context.cpp b/src/liborcus/xlsx_context.cpp
index c1d37b2f..723baf13 100644
--- a/src/liborcus/xlsx_context.cpp
+++ b/src/liborcus/xlsx_context.cpp
@@ -13,6 +13,7 @@
#include "ooxml_schemas.hpp"
#include "xlsx_helper.hpp"
#include "xml_context_global.hpp"
+#include "impl_utils.hpp"
#include <orcus/global.hpp>
#include <orcus/tokens.hpp>
@@ -569,8 +570,7 @@ void xlsx_styles_context::start_element(xmlns_id_t ns, xml_token_t name, const x
expected_elements.push_back(xml_token_pair_t(NS_ooxml_xlsx, XML_dxf));
xml_element_expected(parent, expected_elements);
mp_font = mp_styles->get_font_style();
- if (!mp_font)
- throw interface_error("implementer must provide a concrete instance of import_font_style.");
+ ENSURE_INTERFACE(mp_font, import_font_style);
break;
}
case XML_b:
@@ -682,8 +682,7 @@ void xlsx_styles_context::start_element(xmlns_id_t ns, xml_token_t name, const x
xml_element_expected(parent, expected);
mp_fill = mp_styles->get_fill_style();
- if (!mp_fill)
- throw interface_error("implementer must provide a concrete instance of import_fill_style.");
+ ENSURE_INTERFACE(mp_fill, import_fill_style);
break;
}
@@ -773,8 +772,7 @@ void xlsx_styles_context::start_element(xmlns_id_t ns, xml_token_t name, const x
start_element_border(parent, attrs);
mp_border = mp_styles->get_border_style();
- if (!mp_border)
- throw interface_error("implementer must provide a concrete instance of import_border_style.");
+ ENSURE_INTERFACE(mp_border, import_border_style);
break;
}
@@ -902,8 +900,7 @@ void xlsx_styles_context::start_element(xmlns_id_t ns, xml_token_t name, const x
xml_element_expected(parent, expected_elements);
mp_protection = mp_styles->get_cell_protection();
- if (!mp_protection)
- throw interface_error("implementer must provide a concrete instance of import_cell_protection.");
+ ENSURE_INTERFACE(mp_protection, import_cell_protection);
for (const auto& attr : attrs)
{