Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ int main()
std::cout << md["test"]["super"].to<int>() << "\n";

cppdict::add("toto/tata/titi", 2.5, md);
std::cout << "at toto/tata/titi : " << md["toto"]["tata"]["titi"].to<double>() << "\n";

double const d = md["toto"]["tata"]["titi"];
std::cout << "at toto/tata/titi : " << d << "\n";
}

```
Expand Down
36 changes: 18 additions & 18 deletions include/dict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ namespace // Visitor details
std::visit(
[key, lambdas...](auto&& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (NodeT::template is_value_v<
T> or !is_values_only_v<visit_policy_t>)
if constexpr (NodeT::template is_value_v<T>
or !is_values_only_v<visit_policy_t>)
make_visitor(lambdas...)(key, value);
},
child_node->data);
Expand Down Expand Up @@ -183,25 +183,13 @@ struct Dict
}


bool isLeaf() const noexcept
{
return !isNode() && !isEmpty();
}
bool isLeaf() const noexcept { return !isNode() && !isEmpty(); }

bool isNode() const noexcept
{
return std::holds_alternative<node_t>(data);
}
bool isNode() const noexcept { return std::holds_alternative<node_t>(data); }

bool isEmpty() const noexcept
{
return std::holds_alternative<empty_leaf_t>(data);
}
bool isEmpty() const noexcept { return std::holds_alternative<empty_leaf_t>(data); }

bool isValue() const noexcept
{
return !isNode() and !isEmpty();
}
bool isValue() const noexcept { return !isNode() and !isEmpty(); }

template<typename T, typename U = std::enable_if_t<is_any_of<T, Types...>()>>
Dict& operator=(const T& value)
Expand Down Expand Up @@ -240,6 +228,18 @@ struct Dict
throw std::runtime_error("cppdict: not a map or not default");
}

template<typename T, typename U = std::enable_if_t<is_any_of<T, Types...>()>>
operator T&() const
{
return to<T>();
}

template<typename T, typename U = std::enable_if_t<is_any_of<T, Types...>()>>
operator T&()
{
return to<T>();
}

bool contains(std::string const key) const noexcept
{
return isNode() and std::get<node_t>(data).count(key);
Expand Down
10 changes: 9 additions & 1 deletion test/basic_dict_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
#include "dict.hpp"
using Dict = cppdict::Dict<int, double, std::string>;

TEST_CASE("Can (depp)copy nodes with operator=", "[simple cppdict::Dict<int, double, std::string>]")
TEST_CASE("Can (deep)copy nodes with operator=", "[simple cppdict::Dict<int, double, std::string>]")
{
Dict dict;
dict["this"]["is"]["the"]["source"] = 3.14;
dict["destination"] = dict["this"];
REQUIRE(dict["destination"]["is"]["the"]["source"].to<double>() == 3.14);
}

TEST_CASE("Can convert directly to type")
{
Dict dict;
dict["this"]["is"]["the"]["source"] = 3.14;
double const pi = dict["this"]["is"]["the"]["source"];
REQUIRE(pi == 3.14);
}

TEST_CASE("Can add values and retrieve them", "[simple cppdict::Dict<int, double, std::string>]")
{
Dict dict;
Expand Down
Loading