Skip to content

Commit

Permalink
Added a copy constructor to the Data class
Browse files Browse the repository at this point in the history
  • Loading branch information
Toon Schoenmakers committed Sep 8, 2014
1 parent d19d9b4 commit 13dab1c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 9 additions & 3 deletions include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Data
/**
* All managed values that should be cleaned up upon destruction
*/
std::list<std::unique_ptr<Value>> _managed_values;
std::list<std::shared_ptr<Value>> _managed_values;

/**
* All modifiers
Expand All @@ -52,9 +52,14 @@ class Data
Data(const Variant::Value &value);

/**
* Deleted copy constructor
* Copy constructor
*/
Data(const Data &that) = delete;
Data(const Data &that)
: _variables(that._variables),
_managed_values(that._managed_values),
_modifiers(that._modifiers)
{
}

/**
* Move constructor
Expand Down Expand Up @@ -90,6 +95,7 @@ class Data
* @return Data Same object for chaining
*/
Data &assignManaged(const std::string &name, Value *value);
Data &assignManaged(const std::string &name, std::shared_ptr<Value> &value);

/**
* Assign a callback
Expand Down
24 changes: 24 additions & 0 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ Data &Data::assignValue(const std::string &name, Value *value)
*/
Data &Data::assignManaged(const std::string &name, Value *value)
{
// assigning nullptr's doesn't make sense..
if (!value) return *this;

// append variable
_variables[name] = value;

Expand All @@ -158,6 +161,27 @@ Data &Data::assignManaged(const std::string &name, Value *value)
return *this;
}

/**
* Assign data that is managed by a unique pointer and keep managing it
* @param name Name of the variable
* @param value A unique pointer to a VariantValue
* @return Data Same object for chaining
*/
Data &Data::assignManaged(const std::string &name, std::shared_ptr<Value> &value)
{
// assigning empty shared pointer doesn't make sense
if (!value) return *this;

// append variable
_variables[name] = value.get();

// make it managed
_managed_values.emplace_back(value);

// allow chaining
return *this;
}

/**
* Assign a callback
* @param name Name of the variable
Expand Down

0 comments on commit 13dab1c

Please sign in to comment.