include/utilities/counter.h
author Dmitriy Morozov <morozov@cs.duke.edu>
Thu, 13 Sep 2007 04:42:39 -0400
changeset 27 b0d6c9162de3
parent 20 7bf6aa6b0ab6
child 29 25bed9659e0f
permissions -rw-r--r--
Changed counters and added setup for new logging. Everything but tests is broken.

/*
 * Author: Dmitriy Morozov
 * Department of Computer Science, Duke University, 2005 -- 2007
 */

#ifndef __COUNTER_H__
#define __COUNTER_H__

#ifndef COUNTERS
	#define 	GetCounter(path) 		0
	#define 	Count(x)
	#define		SetFrequency(x, freq)
#else // COUNTERS

#include <map>
#include <string>
#include <iostream>

#define 	GetCounter(path) 			get_counter(path)
#define 	Count(x) 					do { x->count++; if ((x->count % x->frequency == 0)) x->print(); } while (0);
#define		SetFrequency(x, freq)		do { x->frequency = freq; } while (0);

#include <limits>


class Counter
{
	public:
		typedef 				unsigned long 							CounterType;
		typedef					std::map<std::string, Counter*>			SubCounterMap;

	public:
		CounterType				count;
		CounterType				frequency;

	public:
								Counter(const std::string& full_name = "",
										CounterType freq = std::numeric_limits<CounterType>::max()):
										count(0), frequency(freq), full_name_(full_name)
								{}
								~Counter();

		Counter*				get_child(const std::string& path, std::string::size_type pos);
		void					print();

	private:	
		SubCounterMap			subcounters_;
		std::string				full_name_;

};

static		Counter				rootCounter;

Counter*	get_counter(const char* path)
{
	return rootCounter.get_child(path, 0);
}

#include "counter.hpp"

#endif // COUNTERS


#endif // __COUNTER_H__