From: Timo Heister Date: Thu, 1 Oct 2020 19:34:08 +0000 (-0400) Subject: constructor CellId from string X-Git-Tag: v9.3.0-rc1~1044^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F10996%2Fhead;p=dealii.git constructor CellId from string --- 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