Advertisement
Guest User

Untitled

a guest
Jul 14th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <memory>
  4.  
  5. namespace ConditionType
  6. {
  7.     enum type
  8.     {
  9.         NONE,
  10.         isTuesday,
  11.         isThursday,
  12.         isAfterEight
  13.     };
  14. }
  15.  
  16. namespace OperatorType
  17. {
  18.     enum type
  19.     {
  20.         NONE,
  21.         OR,
  22.         AND
  23.     };
  24. }
  25.  
  26. class Condition
  27. {
  28. public:
  29.     Condition(ConditionType::type condition)
  30.         : mCondition(condition)
  31.         , mOperatorType(OperatorType::NONE)
  32.         , mLCondition()
  33.         , mRCondition()
  34.     {}
  35.  
  36.     Condition(const Condition &left, OperatorType::type opType, const Condition &right)
  37.         : mCondition(ConditionType::NONE)
  38.         , mOperatorType(opType)
  39.         , mLCondition(new Condition(left))
  40.         , mRCondition(new Condition(right))
  41.     {}
  42.  
  43.     Condition(const Condition &other)
  44.         : mCondition(other.mCondition)
  45.         , mOperatorType(other.mOperatorType)
  46.         , mLCondition(other.mLCondition ? new Condition(*other.mLCondition) : nullptr)
  47.         , mRCondition(other.mRCondition ? new Condition(*other.mRCondition) : nullptr)
  48.     {
  49.     }
  50.  
  51.     Condition &Condition::operator=(const Condition &other)
  52.     {
  53.         mCondition = other.mCondition;
  54.         mOperatorType = other.mOperatorType;
  55.         mLCondition.reset(other.mLCondition ? new Condition(*other.mLCondition) : nullptr);
  56.         mRCondition.reset(other.mRCondition ? new Condition(*other.mRCondition) : nullptr);
  57.  
  58.         return *this;
  59.     }
  60.    
  61.     operator bool() const
  62.     {
  63.         return evauluate();
  64.     }
  65.  
  66.     bool evauluate() const
  67.     {
  68.         if(mOperatorType == OperatorType::NONE)
  69.             return passesValue();
  70.         else if(mOperatorType == OperatorType::AND)
  71.             return mLCondition->evauluate() && mRCondition->evauluate();
  72.         else if(mOperatorType == OperatorType::OR)
  73.             return mLCondition->evauluate() || mRCondition->evauluate();
  74.  
  75.         return false;
  76.     }
  77.  
  78.     Condition operator&&(const Condition &other) const
  79.     {
  80.         return Condition(*this, OperatorType::AND, other);
  81.     }
  82.  
  83.     Condition operator||(const Condition &other) const
  84.     {
  85.         return Condition(*this, OperatorType::OR, other);
  86.     }
  87.  
  88. private:
  89.     ConditionType::type mCondition;
  90.  
  91.     OperatorType::type mOperatorType;
  92.     std::unique_ptr<Condition> mLCondition;
  93.     std::unique_ptr<Condition> mRCondition;
  94.  
  95.     bool passesValue() const
  96.     {
  97.         switch (mCondition)
  98.         {
  99.         case ConditionType::isTuesday:
  100.             return false;
  101.         case ConditionType::isThursday:
  102.             return true;
  103.         case ConditionType::isAfterEight:
  104.             return false;
  105.         }
  106.  
  107.         return false;
  108.     }
  109. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement