]> granicus.if.org Git - icinga2/blobdiff - lib/livestatus/maxaggregator.cpp
Merge branch 'support/2.8'
[icinga2] / lib / livestatus / maxaggregator.cpp
index bb4650acdde65898bb363e9e59e1f9bfd63f00ef..a2265430bf0c22ebaf3bf6260d187f788dc6ce54 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
 
 using namespace icinga;
 
-MaxAggregator::MaxAggregator(const String& attr)
-    : m_Max(0), m_MaxAttr(attr)
+MaxAggregator::MaxAggregator(String attr)
+       : m_MaxAttr(std::move(attr))
 { }
 
-void MaxAggregator::Apply(const Table::Ptr& table, const Value& row)
+MaxAggregatorState *MaxAggregator::EnsureState(AggregatorState **state)
+{
+       if (!*state)
+               *state = new MaxAggregatorState();
+
+       return static_cast<MaxAggregatorState *>(*state);
+}
+
+void MaxAggregator::Apply(const Table::Ptr& table, const Value& row, AggregatorState **state)
 {
        Column column = table->GetColumn(m_MaxAttr);
 
        Value value = column.ExtractValue(row);
 
-       if (value > m_Max)
-               m_Max = value;
+       MaxAggregatorState *pstate = EnsureState(state);
+
+       if (value > pstate->Max)
+               pstate->Max = value;
 }
 
-double MaxAggregator::GetResult(void) const
+double MaxAggregator::GetResultAndFreeState(AggregatorState *state) const
 {
-       return m_Max;
+       MaxAggregatorState *pstate = EnsureState(&state);
+       double result = pstate->Max;
+       delete pstate;
+
+       return result;
 }