Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Counter

 

STLQt CreatorLubuntu

 

Counter is a class for tallying.

Technical facts

 

 

 

 

 

 

./CppCounter/CppCounter.pri

 

INCLUDEPATH += \
    ../../Classes/CppCounter

SOURCES += \
    ../../Classes/CppCounter/counter.cpp \
    ../../Classes/CppCounter/counter_test.cpp

HEADERS  += \
    ../../Classes/CppCounter/counter.h

OTHER_FILES += \
    ../../Classes/CppCounter/Licence.txt

 

 

 

 

 

./CppCounter/counter.h

 

//---------------------------------------------------------------------------
/*
Counter, an incrementing counter
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCounter.htm
//---------------------------------------------------------------------------
#ifndef COUNTER_H
#define COUNTER_H

#include <string>
#include <vector>

namespace ribi {

///Counter is a counter
struct Counter
{
  ///Counter is constructed with an initial count, which is initialized to
  ///zero by default
  explicit Counter(const int initial_count = 0) noexcept;

  ///Increments count
  void Inc() noexcept { ++m_count; }

  //Prefix
  Counter& operator++()
  {
    Inc();
    return *this;
  }

  ///Get the count
  int Get() const noexcept { return m_count; }

  static std::string GetVersion() noexcept;
  static std::vector<std::string> GetVersionHistory() noexcept;

  private:
  ///The count
  int m_count;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

bool operator==(const Counter& lhs, const Counter& rhs) noexcept;
bool operator!=(const Counter& lhs, const Counter& rhs) noexcept;

} //~namespace ribi

#endif // COUNTER_H

 

 

 

 

 

./CppCounter/counter.cpp

 

//---------------------------------------------------------------------------
/*
Counter, an incrementing counter
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCounter.htm
//---------------------------------------------------------------------------
#include "counter.h"

ribi::Counter::Counter(const int initial_count) noexcept
  : m_count(initial_count)
{
  #ifndef NDEBUG
  Test();
  #endif
}

std::string ribi::Counter::GetVersion() noexcept
{
  return "1.2";
}

std::vector<std::string> ribi::Counter::GetVersionHistory() noexcept
{
  return {
    "2011-08-20: Version 1.0: initial version",
    "2014-08-01: Version 1.1: added tests",
    "2014-04-18: Version 1.2: added operator++, operator== and operator!="
  };
}

bool ribi::operator==(const ribi::Counter& lhs, const ribi::Counter& rhs) noexcept
{
  return lhs.Get() == rhs.Get();
}

bool ribi::operator!=(const ribi::Counter& lhs, const ribi::Counter& rhs) noexcept
{
  return !(lhs == rhs);
}

 

 

 

 

 

./CppCounter/counter_test.cpp

 

//---------------------------------------------------------------------------
/*
Counter, an incrementing counter
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCounter.htm
//---------------------------------------------------------------------------
#ifndef NDEBUG

#include "counter.h"

#include <cassert>

#include "testtimer.h"
#include "trace.h"

void ribi::Counter::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
  const bool verbose{false};
  if (verbose) { TRACE("Default-construction must have value zero"); }
  {
    const Counter c;
    assert(c.Get() == 0);
  }
  if (verbose) { TRACE("Construction with value must return it"); }
  {
    const Counter c(42);
    assert(c.Get() == 42);
  }
  if (verbose) { TRACE("Increment must increment"); }
  {
    Counter c;
    const int old_value = c.Get();
    c.Inc();
    assert(c.Get() == old_value + 1);
  }
  if (verbose) { TRACE("operator++ must increment"); }
  {
    Counter c;
    const int old_value = c.Get();
    ++c;
    assert(c.Get() == old_value + 1);
  }
  if (verbose) { TRACE("operator=="); }
  {
    Counter c;
    Counter d;
    assert(c == d);
  }
  if (verbose) { TRACE("operator!="); }
  {
    Counter c;
    Counter d;
    ++c;
    assert(c != d);
  }
}
#endif

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml