Skip to content

Commit

Permalink
Merge pull request #119 from ashtum/add-operator-/-for-string_path-an…
Browse files Browse the repository at this point in the history
…d-string

Make operator / a friend of string_path
  • Loading branch information
pdimov committed Dec 5, 2023
2 parents ddaf922 + 6b16249 commit 8080ecd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
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);
}
{
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

0 comments on commit 8080ecd

Please sign in to comment.