Created
February 25, 2015 14:05
-
-
Save bosmacs/aad31a62cdbf08732856 to your computer and use it in GitHub Desktop.
Instrumented type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "instrumented.hpp" | |
// necessary static initialization -- this needs to be done for each type we want to instrument | |
template<> size_t instrumented<short>::comparison_count = 0; | |
template<> size_t instrumented<int>::comparison_count = 0; | |
template<> size_t instrumented<float>::comparison_count = 0; | |
template<> size_t instrumented<double>::comparison_count = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INSTRUMENTED_VALUE | |
#define INSTRUMENTED_VALUE | |
#include <cstddef> | |
template<typename T> | |
struct instrumented { | |
typedef T value_type; | |
// copy assignment from value_type | |
instrumented<T>& operator=(const T& other) { | |
this->value = other; | |
return *this; | |
} | |
// implicit conversion to value type | |
operator T() const { return value; } | |
bool operator <=(const instrumented<T>& other) const { | |
comparison_count++; | |
return value <= other.value; | |
} | |
bool operator >=(const instrumented<T>& other) const { | |
comparison_count++; | |
return value >= other.value; | |
} | |
/* | |
bool operator ==(const instrumented<T>& other) const { | |
return value == other.value; | |
} | |
bool operator !=(const instrumented<T>& other) const { | |
return value != other.value; | |
} | |
*/ | |
static size_t count() { return comparison_count; } | |
static void reset() { comparison_count = 0; } | |
private: | |
value_type value; | |
static size_t comparison_count; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment