From 9611db8130bb1dd1e8175ed6de46b0a1586998d7 Mon Sep 17 00:00:00 2001 From: Jose Luis Blanco Claraco Date: Thu, 3 Dec 2020 09:06:44 +0100 Subject: [PATCH] SymbolGenerator: add chr() and made constexpr-capable --- gtsam/inference/Symbol.h | 5 +++-- gtsam/inference/tests/testKey.cpp | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gtsam/inference/Symbol.h b/gtsam/inference/Symbol.h index 469082f16..42cdbb1c3 100644 --- a/gtsam/inference/Symbol.h +++ b/gtsam/inference/Symbol.h @@ -167,10 +167,11 @@ inline Key Z(std::uint64_t j) { return Symbol('z', j); } /** Generates symbol shorthands with alternative names different than the * one-letter predefined ones. */ class SymbolGenerator { - const char c_; + const unsigned char c_; public: - SymbolGenerator(const char c) : c_(c) {} + constexpr SymbolGenerator(const unsigned char c) : c_(c) {} Symbol operator()(const std::uint64_t j) const { return Symbol(c_, j); } + constexpr unsigned char chr() const { return c_; } }; /// traits diff --git a/gtsam/inference/tests/testKey.cpp b/gtsam/inference/tests/testKey.cpp index 64674c36f..98c5d36bf 100644 --- a/gtsam/inference/tests/testKey.cpp +++ b/gtsam/inference/tests/testKey.cpp @@ -59,6 +59,12 @@ TEST(Key, SymbolGenerator) { EXPECT(assert_equal(a1, ddz1)); } +/* ************************************************************************* */ +TEST(Key, SymbolGeneratorConstexpr) { + constexpr auto Z = gtsam::SymbolGenerator('x'); + EXPECT(assert_equal(Z.chr(), 'x')); +} + /* ************************************************************************* */ template Key KeyTestValue();