Skip to content

Commit a82e956

Browse files
author
Chase Geigle
committed
Add clone() function to TOML value classes (fixes #54).
1 parent 30727f9 commit a82e956

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

include/cpptoml.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ class base : public std::enable_shared_from_this<base>
403403
public:
404404
virtual ~base() = default;
405405

406+
virtual std::shared_ptr<base> clone() const = 0;
407+
406408
/**
407409
* Determines if the given TOML element is a value.
408410
*/
@@ -502,6 +504,8 @@ class value : public base
502504
public:
503505
static_assert(valid_value<T>::value, "invalid value type");
504506

507+
std::shared_ptr<base> clone() const override;
508+
505509
value(const make_shared_enabler&, const T& val) : value(val)
506510
{
507511
// nothing; note that users cannot actually invoke this function
@@ -613,11 +617,15 @@ class array : public base
613617
public:
614618
friend std::shared_ptr<array> make_array();
615619

620+
std::shared_ptr<base> clone() const override;
621+
616622
virtual bool is_array() const override
617623
{
618624
return true;
619625
}
620626

627+
using size_type = std::size_t;
628+
621629
/**
622630
* arrays can be iterated over
623631
*/
@@ -821,6 +829,14 @@ class array : public base
821829
values_.clear();
822830
}
823831

832+
/**
833+
* Reserve space for n values.
834+
*/
835+
void reserve(size_type n)
836+
{
837+
values_.reserve(n);
838+
}
839+
824840
private:
825841
array() = default;
826842

@@ -885,6 +901,10 @@ class table_array : public base
885901
friend std::shared_ptr<table_array> make_table_array();
886902

887903
public:
904+
std::shared_ptr<base> clone() const override;
905+
906+
using size_type = std::size_t;
907+
888908
/**
889909
* arrays can be iterated over
890910
*/
@@ -962,6 +982,14 @@ class table_array : public base
962982
array_.clear();
963983
}
964984

985+
/**
986+
* Reserve space for n tables.
987+
*/
988+
void reserve(size_type n)
989+
{
990+
array_.reserve(n);
991+
}
992+
965993
private:
966994
table_array()
967995
{
@@ -1068,6 +1096,8 @@ class table : public base
10681096
friend class table_array;
10691097
friend std::shared_ptr<table> make_table();
10701098

1099+
std::shared_ptr<base> clone() const override;
1100+
10711101
/**
10721102
* tables can be iterated over.
10731103
*/
@@ -1479,6 +1509,38 @@ inline std::shared_ptr<table> make_element<table>()
14791509
return make_table();
14801510
}
14811511

1512+
template <class T>
1513+
std::shared_ptr<base> value<T>::clone() const
1514+
{
1515+
return make_value(data_);
1516+
}
1517+
1518+
inline std::shared_ptr<base> array::clone() const
1519+
{
1520+
auto result = make_array();
1521+
result->reserve(values_.size());
1522+
for (const auto& ptr : values_)
1523+
result->values_.push_back(ptr->clone());
1524+
return result;
1525+
}
1526+
1527+
inline std::shared_ptr<base> table_array::clone() const
1528+
{
1529+
auto result = make_table_array();
1530+
result->reserve(array_.size());
1531+
for (const auto& ptr : array_)
1532+
result->array_.push_back(ptr->clone()->as_table());
1533+
return result;
1534+
}
1535+
1536+
inline std::shared_ptr<base> table::clone() const
1537+
{
1538+
auto result = make_table();
1539+
for (const auto& pr : map_)
1540+
result->insert(pr.first, pr.second->clone());
1541+
return result;
1542+
}
1543+
14821544
/**
14831545
* Exception class for all TOML parsing errors.
14841546
*/

0 commit comments

Comments
 (0)