VR-Forces 4.0.4 Class Documentation
include/tdbutil/testAssignUtil.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 ** Copyright (c) 2006 MaK Technologies, Inc.
00003 ** All rights reserved.
00004 *********************************************************************/
00005 /*********************************************************************
00006 ** $RCSfile: testAssignUtil.h,v $ $Revision: 1.1 $ $State: Exp $
00007 ********************************************************************/
00008 #ifndef TD_TEST_ASSIGN_UTIL
00009 #define TD_TEST_ASSIGN_UTIL
00010 
00011 //
00012 // Utility class tests if data is changed while assigning it.
00013 // If the data being tested has an operator = function, use set(old, new)
00014 // If not then use if (set.test(old.<value>(), new.<value>())) and assign the value 
00015 // yourself inside the if statement.
00016 //
00017 
00018 class TdTestAssignUtil {
00019 public: 
00020    TdTestAssignUtil(float tol = -1.0, double dTol = -1.0)
00021    {
00022       mTol = (tol < 0) ? 1.0e-6 : tol;
00023       mDTol = (dTol < 0) ? 1.0e-9 : dTol;
00024       myChanged = false;
00025    }
00026 
00027    void operator() (float& oldVal, const float & newVal)
00028    {
00029       if (fabs(oldVal - newVal) > mTol)
00030       {
00031          myChanged = true;
00032          oldVal = newVal;
00033       }
00034    }
00035    bool test (const float& oldVal, const float & newVal)
00036    {
00037       myChanged = (fabs(oldVal - newVal) > mTol);
00038       return myChanged;
00039    }
00040 
00041    void operator() (double& oldVal, const double & newVal)
00042    {
00043       if (fabs(oldVal - newVal) > mDTol)
00044       {
00045          myChanged = true;
00046          oldVal = newVal;
00047       }
00048    }
00049    bool test (const double& oldVal, const double & newVal)
00050    {
00051       myChanged = (fabs(oldVal - newVal) > mDTol);
00052       return myChanged;
00053    }
00054 
00055    template <class _T>
00056    void operator()(_T& oldVal, const _T& newVal)
00057    {
00058       // Note use of == instead of !=
00059       if (!(oldVal == newVal))
00060       {
00061          myChanged = true;
00062          oldVal = newVal;
00063       }
00064    }
00065 
00066    template <class _T>
00067    bool test(const _T& oldVal, const _T& newVal)
00068    {
00069       // Note use of == instead of !=
00070       if (!(oldVal == newVal))
00071       {
00072          myChanged = true;
00073          return true;
00074       }
00075       return false;
00076    }
00077 
00078    // Returns whether any of the preceeding assignments have changed 
00079    // a value.
00080 
00081    bool changed() const
00082    {
00083       return myChanged;
00084    }
00085 
00086    // Reset the change flag to false
00087    void reset()
00088    {
00089       myChanged = false;
00090    }
00091 
00092 protected:
00093    bool myChanged;
00094    float mTol;
00095    double mDTol;
00096 };
00097 
00098 #endif

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)