1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #ifndef POLYGRAPH__LOGANALYZERS_LEVELSTEX_H
7 #define POLYGRAPH__LOGANALYZERS_LEVELSTEX_H
8 
9 #include "xstd/String.h"
10 #include "base/StatIntvlRec.h"
11 #include "base/LevelStat.h"
12 
13 // an algorithm of extracting a particular level statistics out of
14 // interval stats record
15 class LevelStex {
16 	public:
17 		// TODO: Replace with lambdas
18 		typedef LevelStat StatIntvlRec::*StatPtr;
19 		typedef ProtoIntvlStat StatIntvlRec::*ProtoPtr;
20 		typedef const LevelStat &(ProtoIntvlStat::*LevelMethod)() const;
21 
22 	public:
LevelStex(const String & aKey,const String & aName,StatPtr aStats)23 		LevelStex(const String &aKey, const String &aName, StatPtr aStats):
24 			theKey(aKey), theName(aName), theStats(aStats) {}
LevelStex(const String & aKey,const String & aName,ProtoPtr aProto,LevelMethod aMethod)25 		LevelStex(const String &aKey, const String &aName, ProtoPtr aProto, LevelMethod aMethod):
26 			theKey(aKey), theName(aName), theStats(0), theProto(aProto), theLevelMethod(aMethod) {}
27 
key()28 		const String &key() const { return theKey; } // precise, for machine use
name()29 		const String &name() const { return theName; } // imprecise, human-readable
30 
level(const StatIntvlRec & rec)31 		const LevelStat &level(const StatIntvlRec &rec) const {
32 			return theStats ? rec.*theStats : (rec.*theProto.*theLevelMethod)();
33 		}
34 
35 	protected:
36 		String theKey;
37 		String theName;
38 
39 		// Alternative A: Extract LevelStat directly from a trace window.
40 		StatPtr theStats;
41 		// Alternative B: First extract ProtoIntvlStat from a trace window, and
42 		// then LevelStat from the extracted ProtoIntvlStat.
43 		ProtoPtr theProto;
44 		LevelMethod theLevelMethod;
45 };
46 
47 #endif
48