Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make operator / a friend of string_path #119

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions include/boost/property_tree/string_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ namespace boost { namespace property_tree
return detail::dump_sequence(m_value);
}

/// Concatenates two path components
friend string_path operator /(string_path p1, const string_path &p2)
{
p1 /= p2;
return p1;
}

/// Append a second path to this one.
/// @pre o's separator is the same as this one's, or o has no separators
string_path& operator /=(const string_path &o) {
Expand Down Expand Up @@ -243,36 +250,6 @@ namespace boost { namespace property_tree
typedef std::basic_string<Ch, Traits, Alloc> _string;
typedef string_path< _string, id_translator<_string> > type;
};

template <typename String, typename Translator> inline
string_path<String, Translator> operator /(
string_path<String, Translator> p1,
const string_path<String, Translator> &p2)
{
p1 /= p2;
return p1;
}

// These shouldn't be necessary, but GCC won't find the one above.
template <typename String, typename Translator> inline
string_path<String, Translator> operator /(
string_path<String, Translator> p1,
const typename String::value_type *p2)
{
p1 /= p2;
return p1;
}

template <typename String, typename Translator> inline
string_path<String, Translator> operator /(
const typename String::value_type *p1,
const string_path<String, Translator> &p2)
{
string_path<String, Translator> t(p1);
t /= p2;
return t;
}

}}

#endif
18 changes: 16 additions & 2 deletions test/test_property_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,16 +1026,30 @@ void test_path(PTREE *)
}

// Test operator /
{
path p = path(T("key1")) / path(T("key2.key3"));
BOOST_TEST(pt.get<int>(p, 0) == 1);
}
{
path p = path(T("key1.key2")) / path(T("key3"));
BOOST_TEST(pt.get<int>(p, 0) == 1);
}
Comment on lines +1029 to +1036
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it looks like we are missing a test for p1 / p2 with p1 and p2 both being paths.

@pdimov, Addressed.

{
path p = path(T("key1")) / T("key2.key3");
BOOST_TEST(pt.get<int>(p, 0) == 1);
}

// Test operator /
{
path p = T("key1.key2") / path(T("key3"));
BOOST_TEST(pt.get<int>(p, 0) == 1);
}
{
path p = path(T("key1")) / std::basic_string<CHTYPE>(T("key2.key3"));
BOOST_TEST(pt.get<int>(p, 0) == 1);
}
{
path p = std::basic_string<CHTYPE>(T("key1.key2")) / path(T("key3"));
BOOST_TEST(pt.get<int>(p, 0) == 1);
}

}

Expand Down