Skip to content

Commit

Permalink
* Added default values for FlexMap to_double|string|integer|record th…
Browse files Browse the repository at this point in the history
…at will be returns if the variable doesn't exist. This was a feature request that can help with debugging.
  • Loading branch information
jredmondson committed Jun 21, 2018
1 parent aaca105 commit fd09798
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 49 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ script:
# now run a couple of tests
- echo "Testing basic functionality..."
- $MADARA_ROOT/bin/test_basic_reasoning
- $MADARA_ROOT/bin/test_karl_containers
- $MADARA_ROOT/bin/test_karl_exceptions
# performance test (useful to see if we've regressed in performance)
- echo "Testing reasoning throughput..."
Expand Down
76 changes: 63 additions & 13 deletions include/madara/knowledge/containers/FlexMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,44 +214,94 @@ const std::vector <double> & value)


madara::knowledge::KnowledgeRecord
madara::knowledge::containers::FlexMap::to_record (void) const
madara::knowledge::containers::FlexMap::to_record (
const KnowledgeRecord & default_value) const
{
knowledge::KnowledgeRecord result;

if (context_)
{
ContextGuard context_guard (*context_);
MADARA_GUARD_TYPE guard (mutex_);

KnowledgeUpdateSettings keep_local (true);

if (!variable_.is_valid ())
{
this->update_variable ();
}

result = context_->get (variable_, settings_);
if (context_->exists (variable_))
{
return context_->get (variable_, settings_);
}
}

return result;
return default_value;
}

madara::knowledge::KnowledgeRecord::Integer
madara::knowledge::containers::FlexMap::to_integer (void) const
madara::knowledge::containers::FlexMap::to_integer (
KnowledgeRecord::Integer default_value) const
{
return to_record ().to_integer ();
if (context_)
{
ContextGuard context_guard (*context_);
MADARA_GUARD_TYPE guard (mutex_);

if (!variable_.is_valid ())
{
this->update_variable ();
}

if (context_->exists (variable_))
{
return context_->get (variable_, settings_).to_integer ();
}
}

return default_value;
}

double
madara::knowledge::containers::FlexMap::to_double (void) const
madara::knowledge::containers::FlexMap::to_double (double default_value) const
{
return to_record ().to_double ();
if (context_)
{
ContextGuard context_guard (*context_);
MADARA_GUARD_TYPE guard (mutex_);

if (!variable_.is_valid ())
{
this->update_variable ();
}

if (context_->exists (variable_))
{
return context_->get (variable_, settings_).to_double ();
}
}

return default_value;
}

std::string
madara::knowledge::containers::FlexMap::to_string (void) const
madara::knowledge::containers::FlexMap::to_string (
const std::string & default_value) const
{
return to_record ().to_string ();
if (context_)
{
ContextGuard context_guard (*context_);
MADARA_GUARD_TYPE guard (mutex_);

if (!variable_.is_valid ())
{
this->update_variable ();
}

if (context_->exists (variable_))
{
return context_->get (variable_, settings_).to_string ();
}
}

return default_value;
}


Expand Down
18 changes: 14 additions & 4 deletions include/madara/knowledge/containers/FlexMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ namespace madara

/**
* Retrieves a copy of the record from the current location
* @param default_value if map location does not exist in KB,
* return this value instead
* @return the value of the entry. Modifications to this will
* not be reflected in the context. This is a local copy.
**/
knowledge::KnowledgeRecord to_record (void) const;
knowledge::KnowledgeRecord to_record (
const KnowledgeRecord & default_value = KnowledgeRecord(0)) const;

/**
* Fills a BufferVector container with all subkeys
Expand Down Expand Up @@ -204,21 +207,28 @@ namespace madara

/**
* Returns the value at the location as an integer
* @param default_value if map location does not exist in KB,
* return this value instead
* @return the value at the location
**/
knowledge::KnowledgeRecord::Integer to_integer (void) const;
knowledge::KnowledgeRecord::Integer to_integer (
KnowledgeRecord::Integer default_value = 0) const;

/**
* Returns the value at the location as a double
* @param default_value if map location does not exist in KB,
* return this value instead
* @return the value at the location
**/
double to_double (void) const;
double to_double (double default_value = 0.0) const;

/**
* Returns the value at the location as a string
* @param default_value if map location does not exist in KB,
* return this value instead
* @return the value at the location
**/
std::string to_string (void) const;
std::string to_string (const std::string & default_value = "") const;

/**
* Returns the size of the map
Expand Down

0 comments on commit fd09798

Please sign in to comment.