Skip to content

Commit

Permalink
add arbitrary attributes to metadata entries
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Mar 16, 2012
1 parent 8f3b418 commit aba0061
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
8 changes: 6 additions & 2 deletions include/pdal/Metadata.hpp
Expand Up @@ -121,8 +121,8 @@ class PDAL_DLL Metadata

~Metadata();

template <class T> inline void set(T const& v) { m_variant = v; }
template <class T> inline T get() { return boost::get<T>(m_variant); }
template <class T> inline void setValue(T const& v) { m_variant = v; }
template <class T> inline T getValue() { return boost::get<T>(m_variant); }

template <class T> inline T cast() { return boost::lexical_cast<T>(m_variant); }
Metadata& operator=(Metadata const& rhs);
Expand All @@ -137,6 +137,10 @@ class PDAL_DLL Metadata
std::string const& getNamespace() const { return m_namespace; }
void setNamespace(std::string const& ns) { m_namespace = ns; }

std::vector<std::string> getAttributeNames() const;
void addAttribute(std::string const& name, std::string const value);
std::string getAttribute(std::string const& name) const;

private:
metadata::Variant m_variant;
std::string m_name;
Expand Down
28 changes: 28 additions & 0 deletions src/Metadata.cpp
Expand Up @@ -71,6 +71,34 @@ Metadata::~Metadata()

}

std::vector<std::string> Metadata::getAttributeNames() const
{
std::vector<std::string> output;
metadata::MetadataAttributeM::const_iterator i = m_attributes.begin();
while (i != m_attributes.end())
{
output.push_back(i->first);
++i;
}

return output;
}


void Metadata::addAttribute(std::string const& name, std::string const value)
{
std::pair<std::string, std::string> p(name, value);
m_attributes.insert(p);
}

std::string Metadata::getAttribute(std::string const& name) const
{
metadata::MetadataAttributeM::const_iterator p = m_attributes.find(name);
if (p != m_attributes.end())
return p->second;
else
return std::string("");
}

Metadata& Metadata::operator=(Metadata const& rhs)
{
Expand Down
22 changes: 16 additions & 6 deletions test/unit/MetadataTest.cpp
Expand Up @@ -68,16 +68,26 @@ BOOST_AUTO_TEST_CASE(test_construction)

pdal::Bounds<double> b(1.1,2.2,3.3,101.1,102.2,103.3);

m.set<boost::uint32_t>(u32);
m.setValue<boost::uint32_t>(u32);

BOOST_CHECK_EQUAL(m.get<boost::uint32_t>(), 32u);
BOOST_CHECK_THROW(m.get<boost::int32_t>(), boost::bad_get);
m.addAttribute("some_id", "some_value");
m.addAttribute("another_id", "another_value");

std::vector<std::string> names = m.getAttributeNames();
BOOST_CHECK_EQUAL(names.size(), 2);

BOOST_CHECK_EQUAL(m.getAttribute("some_id"), "some_value");
BOOST_CHECK_EQUAL(m.getAttribute("another_id"), "another_value");

BOOST_CHECK_EQUAL(m.getValue<boost::uint32_t>(), 32u);
BOOST_CHECK_THROW(m.getValue<boost::int32_t>(), boost::bad_get);

BOOST_CHECK_EQUAL(m.cast<boost::int32_t>(), 32);
m.set<pdal::metadata::ByteArray>(bytes);
BOOST_CHECK_EQUAL(m.cast<boost::int32_t>(), 32);
m.setValue<pdal::metadata::ByteArray>(bytes);

std::string base64("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiYw==");
BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(m.get<pdal::metadata::ByteArray>()), base64);
BOOST_CHECK_EQUAL( boost::lexical_cast<std::string>(m.getValue<pdal::metadata::ByteArray>()), base64);
BOOST_CHECK_THROW(m.getValue<boost::int32_t>(), boost::bad_get);

return;
}
Expand Down

0 comments on commit aba0061

Please sign in to comment.