diff --git a/cpp/VectorConfig.cpp b/cpp/VectorConfig.cpp index 0b802d803..4d02dbc80 100644 --- a/cpp/VectorConfig.cpp +++ b/cpp/VectorConfig.cpp @@ -149,6 +149,17 @@ const Vector& VectorConfig::get(const std::string& name) const { return it->second; } +/* ************************************************************************* */ +Vector& VectorConfig::getReference(const std::string& name) { + iterator it = values.find(name); + if (it==values.end()) { + print(); + cout << "asked for key " << name << endl; + throw(std::invalid_argument("VectorConfig::[] invalid key")); + } + return it->second; +} + /* ************************************************************************* */ void VectorConfig::print(const std::string& name) const { odprintf("VectorConfig %s\n", name.c_str()); diff --git a/cpp/VectorConfig.h b/cpp/VectorConfig.h index 5971582a4..0f0d86446 100644 --- a/cpp/VectorConfig.h +++ b/cpp/VectorConfig.h @@ -61,6 +61,9 @@ namespace gtsam { /** get a vector in the configuration by name */ const Vector& get(const std::string& name) const; + /** get a vector reference by name */ + Vector& getReference(const std::string& name); + /** operator[] syntax for get */ inline const Vector& operator[](const std::string& name) const { return get(name); diff --git a/cpp/testVectorConfig.cpp b/cpp/testVectorConfig.cpp index 5077e7a3e..0b5472f91 100644 --- a/cpp/testVectorConfig.cpp +++ b/cpp/testVectorConfig.cpp @@ -153,6 +153,15 @@ TEST( VectorConfig, operators) { CHECK(assert_equal(expected2,c-c)); } +/* ************************************************************************* */ +TEST( VectorConfig, getReference) { + VectorConfig c; c.insert("x", Vector_(2, 1.1, 2.2)); + Vector& cx = c.getReference("x"); + cx = cx*2.0; + VectorConfig expected; expected.insert("x", Vector_(2, 2.2, 4.4)); + CHECK(assert_equal(expected,c)); +} + /* ************************************************************************* */ #ifdef HAVE_BOOST_SERIALIZATION TEST( VectorConfig, serialize)