From a30a90ac92035f350221940ad63057d0f31a8786 Mon Sep 17 00:00:00 2001 From: Timo Heister Date: Thu, 1 Oct 2020 15:34:08 -0400 Subject: [PATCH] constructor CellId from string --- doc/news/changes/minor/20201001Heister | 3 ++ include/deal.II/grid/cell_id.h | 6 ++++ source/grid/cell_id.cc | 8 +++++ tests/grid/cell_id_07.cc | 44 ++++++++++++++++++++++++++ tests/grid/cell_id_07.output | 7 ++++ 5 files changed, 68 insertions(+) create mode 100644 doc/news/changes/minor/20201001Heister create mode 100644 tests/grid/cell_id_07.cc create mode 100644 tests/grid/cell_id_07.output diff --git a/doc/news/changes/minor/20201001Heister b/doc/news/changes/minor/20201001Heister new file mode 100644 index 0000000000..8fce5c281c --- /dev/null +++ b/doc/news/changes/minor/20201001Heister @@ -0,0 +1,3 @@ +New: CellId has a new constructor to create it from a std::string. +
+(Timo Heister, 2020/10/05) diff --git a/include/deal.II/grid/cell_id.h b/include/deal.II/grid/cell_id.h index 3078efc1fe..15b7425ea1 100644 --- a/include/deal.II/grid/cell_id.h +++ b/include/deal.II/grid/cell_id.h @@ -112,6 +112,12 @@ public: */ CellId(const binary_type &binary_representation); + /** + * Create a CellId from a string with the same format that is produced by + * to_string(). + */ + explicit CellId(const std::string &string_representation); + /** * Construct an invalid CellId. */ diff --git a/source/grid/cell_id.cc b/source/grid/cell_id.cc index 28c4b8206c..81ba05cdb6 100644 --- a/source/grid/cell_id.cc +++ b/source/grid/cell_id.cc @@ -97,6 +97,14 @@ CellId::CellId(const CellId::binary_type &binary_representation) +CellId::CellId(const std::string &string_representation) +{ + std::istringstream ss(string_representation); + ss >> *this; +} + + + template CellId::binary_type CellId::to_binary() const diff --git a/tests/grid/cell_id_07.cc b/tests/grid/cell_id_07.cc new file mode 100644 index 0000000000..dc780f6085 --- /dev/null +++ b/tests/grid/cell_id_07.cc @@ -0,0 +1,44 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// testing CellId from / to_string + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + + std::vector ids; + + ids.emplace_back(CellId(0, {0})); + ids.emplace_back(CellId(0, {1})); + ids.emplace_back(CellId(0, {0, 0})); + ids.emplace_back(CellId(0, {1, 2, 0})); + ids.emplace_back(CellId(23, {})); + + for (const auto &id : ids) + { + std::string s = id.to_string(); + deallog << s << '\n'; + AssertThrow(CellId(s) == id, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} diff --git a/tests/grid/cell_id_07.output b/tests/grid/cell_id_07.output new file mode 100644 index 0000000000..65da49f643 --- /dev/null +++ b/tests/grid/cell_id_07.output @@ -0,0 +1,7 @@ + +DEAL::0_1:0 +0_1:1 +0_2:00 +0_3:120 +23_0: +OK -- 2.39.5