{
gmx::TextWriter writer(fp);
writer.wrapperSettings().setIndent(indent);
- ir->params->writeUsing(&writer);
+ gmx::dumpKeyValueTree(&writer, *ir->params);
}
pr_grp_opts(fp, indent, "grpopts", &(ir->opts), bMDPformat);
<Object Name="Adjusted">
<Bool Name="a">true</Bool>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = true
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<Real Name="a">1.5</Real>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = 1.5
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<String Name="a">foo</String>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = foo
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<String Name="a">foo</String>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = foo
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<Real Name="a">1.5</Real>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = 1.5
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<Int64 Name="a">2</Int64>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = 2
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<Int Name="a">2</Int>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = 2
+]]></String>
</ReferenceData>
<Object Name="Adjusted">
<String Name="a">s</String>
</Object>
+ <String Name="Dumped"><![CDATA[
+a = s
+]]></String>
</ReferenceData>
#include "gromacs/options/optionsection.h"
#include "gromacs/options/repeatingsection.h"
#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/inmemoryserializer.h"
#include "gromacs/utility/keyvaluetree.h"
#include "gromacs/utility/keyvaluetreebuilder.h"
+#include "gromacs/utility/keyvaluetreeserializer.h"
+#include "gromacs/utility/stringstream.h"
+#include "gromacs/utility/textwriter.h"
#include "testutils/refdata.h"
#include "testutils/testasserts.h"
gmx::test::TestReferenceChecker checker(refdata.rootChecker());
gmx::KeyValueTreeObject tree(builder_.build());
checker.checkKeyValueTreeObject(tree, "Input");
+ // Check that adjustment works.
ASSERT_NO_THROW_GMX(tree = gmx::adjustKeyValueTreeFromOptions(tree, options_));
checker.checkKeyValueTreeObject(tree, "Adjusted");
+ // Check that assignment works.
+ ASSERT_NO_THROW_GMX(gmx::assignOptionsFromKeyValueTree(&options_, tree, nullptr));
+ {
+ gmx::StringOutputStream stream;
+ gmx::TextWriter writer(&stream);
+ ASSERT_NO_THROW_GMX(gmx::dumpKeyValueTree(&writer, tree));
+ checker.checkTextBlock(stream.toString(), "Dumped");
+ }
}
gmx::Options options_;
#include "gromacs/utility/compare.h"
#include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/strconvert.h"
#include "gromacs/utility/stringutil.h"
#include "gromacs/utility/textwriter.h"
return splitDelimitedString(path.substr(1), '/');
}
-//! Helper function to format a simple KeyValueTreeValue.
-std::string formatSingleValue(const KeyValueTreeValue &value)
-{
- if (value.isType<float>())
- {
- return formatString("%g", value.cast<float>());
- }
- else if (value.isType<double>())
- {
- return formatString("%g", value.cast<double>());
- }
- GMX_RELEASE_ASSERT(false, "Unknown value type");
- return std::string();
-}
-
} // namespace
/********************************************************************
return true;
}
-void KeyValueTreeObject::writeUsing(TextWriter *writer) const
+/********************************************************************
+ * Key value tree dump
+ */
+
+//! \cond libapi
+void dumpKeyValueTree(TextWriter *writer, const KeyValueTreeObject &tree)
{
- for (const auto &prop : properties())
+ for (const auto &prop : tree.properties())
{
const auto &value = prop.value();
if (value.isObject())
writer->writeLine(":");
int oldIndent = writer->wrapperSettings().indent();
writer->wrapperSettings().setIndent(oldIndent + 2);
- value.asObject().writeUsing(writer);
+ dumpKeyValueTree(writer, value.asObject());
writer->wrapperSettings().setIndent(oldIndent);
}
else
GMX_RELEASE_ASSERT(!elem.isObject() && !elem.isArray(),
"Arrays of objects not currently implemented");
writer->writeString(" ");
- writer->writeString(formatSingleValue(elem));
+ writer->writeString(simpleValueToString(elem));
}
writer->writeString(" ]");
}
else
{
- writer->writeString(formatSingleValue(value));
+ writer->writeString(simpleValueToString(value));
}
writer->writeLine();
}
}
}
+//! \endcond
/********************************************************************
* Key value tree comparison
{
return "present";
}
- return formatSingleValue(value);
+ return simpleValueToString(value);
}
KeyValueTreePath currentPath_;
*/
bool hasDistinctProperties(const KeyValueTreeObject &obj) const;
- /*! \brief
- * Writes a string representation of the object with given writer.
- *
- * The output format is designed to be readable by humans; if some
- * particular machine-readable format is needed, that should be
- * implemented outside the generic key-value tree code.
- */
- void writeUsing(TextWriter *writer) const;
-
private:
//! Keeps the properties by key.
std::map<std::string, KeyValueTreeValue> valueMap_;
}
//! \cond libapi
+/*! \brief
+ * Writes a human-readable representation of the tree with given writer.
+ *
+ * The output format is designed to be readable by humans; if some
+ * particular machine-readable format is needed, that should be
+ * implemented outside the generic key-value tree code.
+ *
+ * \ingroup module_utility
+ */
+void dumpKeyValueTree(TextWriter *writer, const KeyValueTreeObject &tree);
+
/*! \brief
* Compares two KeyValueTrees and prints any differences.
*
const KeyValueTreeObject &tree2,
real ftol,
real abstol);
+
+//! Helper function to format a simple KeyValueTreeValue.
+static inline std::string
+simpleValueToString(const KeyValueTreeValue &value)
+{
+ return simpleValueToString(value.asVariant());
+}
+
//! \endcond
} // namespace gmx
--- /dev/null
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2017, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can 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.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \internal \file
+ * \brief
+ * Implements functionality from variant.h.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \ingroup module_utility
+ */
+#include "gmxpre.h"
+
+#include "variant.h"
+
+#include <string>
+
+#include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/strconvert.h"
+
+namespace gmx
+{
+
+//! \cond libapi
+std::string simpleValueToString(const Variant &value)
+{
+ if (value.isType<bool>())
+ {
+ // TODO: Consider if this would be better as yes/no instead of
+ // true/false.
+ return toString(value.cast<bool>());
+ }
+ else if (value.isType<float>())
+ {
+ return toString(value.cast<float>());
+ }
+ else if (value.isType<double>())
+ {
+ return toString(value.cast<double>());
+ }
+ else if (value.isType<int>())
+ {
+ return toString(value.cast<int>());
+ }
+ else if (value.isType<gmx_int64_t>())
+ {
+ return toString(value.cast<gmx_int64_t>());
+ }
+ else if (value.isType<std::string>())
+ {
+ return value.cast<std::string>();
+ }
+ GMX_RELEASE_ASSERT(false, "Unknown value type");
+ return std::string();
+}
+//! \endcond
+
+} // namespace gmx
/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2016, by the GROMACS development team, led by
+ * Copyright (c) 2016,2017, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
#define GMX_UTILITY_VARIANT_H
#include <memory>
+#include <string>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
std::unique_ptr<IContent> content_;
};
+//! \cond libapi
+/*! \brief
+ * Converts a Variant value to a string.
+ *
+ * As the name suggests, only some types of "simple" values (such as int) are
+ * supported. Asserts for unsupported types.
+ *
+ * \ingroup module_utility
+ */
+std::string simpleValueToString(const Variant &value);
+//! \endcond
+
} // namespace gmx
#endif