value.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef value_hpp
  2. #define value_hpp
  3. #include <iostream>
  4. #include <utility>
  5. #include <map>
  6. namespace filval{
  7. class GenValue{
  8. public:
  9. virtual void reset() = 0;
  10. };
  11. typedef std::map<std::string, GenValue*> ValueSet;
  12. /* class ValueSet: public std::map<std::string, GenValue*>{ */
  13. /* public: */
  14. /* GenValue*& operator[](const std::string& key){ */
  15. /* GenValue*& value = (*this)[key]; */
  16. /* if (value == NULL){ */
  17. /* std::cerr << "ERROR: key \""<<key */
  18. /* <<"\" not in valueset" << std::endl; */
  19. /* } */
  20. /* return value; */
  21. /* } */
  22. /* }; */
  23. template <typename V>
  24. class Value : public GenValue{
  25. public:
  26. virtual V& get_value() = 0;
  27. };
  28. template <typename V>
  29. class ObservedValue : public Value<V>{
  30. /* For "observed" values, there is nothing to calculate since this is
  31. * merely a wrapper around a field in the observation. A pointer to the
  32. * value is kept and it's value is read when requested.
  33. */
  34. private:
  35. V *val_ref;
  36. public:
  37. ObservedValue(V *val_ref)
  38. : val_ref(val_ref){}
  39. V& get_value(){
  40. return *val_ref;
  41. }
  42. void reset(){ }
  43. };
  44. template <typename V>
  45. class DerivedValue : public Value<V>{
  46. /* A "derived" value is the result of some sort of calculation. Since it is
  47. * desireable for the calculation to occur at most a single time for each
  48. * observation, the result of the calculation is stored in the object. be
  49. * sure that "reset" is called between processing observations to force a
  50. * re-calculation.
  51. */
  52. protected:
  53. V value; // The value to be calculated
  54. bool value_valid;
  55. virtual void update_value() = 0;
  56. public:
  57. V& get_value(){
  58. if (!value_valid){
  59. /* std::cout << "updating value!\n"; */
  60. update_value();
  61. value_valid = true;
  62. }
  63. return value;
  64. }
  65. void reset(){
  66. value_valid = false;
  67. }
  68. };
  69. template <typename T1, typename T2>
  70. class DerivedPair : public DerivedValue<std::pair<T1, T2> >{
  71. protected:
  72. std::pair<Value<T1>*, Value<T2>* > value_pair;
  73. void update_value(){
  74. this->value.first = value_pair.first->get_value();
  75. this->value.second = value_pair.second->get_value();
  76. }
  77. public:
  78. DerivedPair(ValueSet *values, const std::string &label1, const std::string &label2){
  79. ValueSet &valueSet = *values;
  80. value_pair.first = (Value<T1>*) valueSet[label1];
  81. value_pair.second = (Value<T2>*) valueSet[label2];
  82. }
  83. DerivedPair(Value<T1> *value1, Value<T2> *value2){
  84. value_pair.first = value1;
  85. value_pair.second = value2;
  86. }
  87. };
  88. template <typename V>
  89. class ConstantValue : public DerivedValue<V>{
  90. protected:
  91. V const_value;
  92. void update_value(){
  93. this->value = const_value;
  94. }
  95. public:
  96. ConstantValue(V const_value)
  97. :const_value(const_value) { }
  98. };
  99. }
  100. #endif // value_hpp