Skip to content

Commit

Permalink
Allow casting a Php::Value to a std::set
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Otto committed Oct 22, 2014
1 parent 6b2eaeb commit e5b5f1a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,40 @@ class Value : private HashParent
return result;
}

/**
* Convert the object to a set
*
* This only works for regular arrays that are indexed by a number, start
* with position 0 and have no empty spaces.
*
* return std::vector
*/
template <typename T>
std::set<T> setValue() const
{
// only works for arrays, other types return an empty set
if (!isArray()) return std::set<T>();

// allocate a result
std::set<T> result;

// how many elements are we inserting
size_t count = size();

// and fill the result set
for (size_t i = 0; i<count; i++)
{
// check if the index exists
if (!contains(i)) continue;

// get the value and add it to the vector
result.insert(get(i));
}

// done
return result;
}

/**
* Convert the object to a map with string index and Php::Value value
* @return std::map
Expand Down Expand Up @@ -669,6 +703,16 @@ class Value : private HashParent
return vectorValue<T>();
}

/**
* Convert the object to a set
* @return std::set
*/
template <typename T>
operator std::set<T>() const
{
return setValue<T>();
}

/**
* Convert the object to a map with string index and Php::Value value
* @return std::map
Expand Down

0 comments on commit e5b5f1a

Please sign in to comment.