mds_plat/metadata_engine_api/inc/mderange.inl
changeset 58 fe894bb075c2
parent 51 87e65c44ff3a
child 60 79f826a55db2
equal deleted inserted replaced
51:87e65c44ff3a 58:fe894bb075c2
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #ifndef MDERANGE_INL_
       
    18 #define MDERANGE_INL_
       
    19 
       
    20 /**
       
    21  * The TMdERange template is used for representing value ranges of various 
       
    22  * types. A range is composed of an upper limit, a lower limit, and type 
       
    23  * that describe how the limits are to be interpreted. 
       
    24  *
       
    25  * Value ranges are used with CMdERangePropertyCondition nodes to define 
       
    26  * a range of values that the condition matches. Basic operations such as 
       
    27  * "greater than", "equals to", and "between" are all implemented using 
       
    28  * value ranges. There are also more complex ranges that can be used in 
       
    29  * loops.
       
    30  *
       
    31  * TMdERange is the base template for many different kind of value ranges.
       
    32  * However, all ranges can be processed with the Min(), Max(), and Type() 
       
    33  * methods, regardless of any extra data defined by a specialized range type.
       
    34  */
       
    35 template <class T>
       
    36 class TMdERange
       
    37     {
       
    38 public:
       
    39 
       
    40     /* Methods. */
       
    41 
       
    42     /**
       
    43      * Returns the lower limit of the range.
       
    44      *
       
    45      * @return  The minimum value.
       
    46      */
       
    47     inline const T& Min() const
       
    48         {
       
    49         return iMin;
       
    50         }
       
    51 
       
    52     /**
       
    53      * Returns the upper limit of the range.
       
    54      *
       
    55      * @return  The maximum value.
       
    56      */
       
    57     inline const T& Max() const
       
    58         {
       
    59         return iMax;
       
    60         }
       
    61 
       
    62     /**
       
    63      * Returns the type of the range.
       
    64      *
       
    65      * @return  type
       
    66      */
       
    67     inline const TMdERangeType& Type() const
       
    68         {
       
    69         return iType;
       
    70         }
       
    71 
       
    72     /**
       
    73      * Sets the lower limit of the range.
       
    74      *
       
    75      * @param aMin  The minimum value.
       
    76      */
       
    77     inline void SetMin(const T& aMin)
       
    78         {
       
    79         iMin = aMin;
       
    80         }
       
    81 
       
    82     /**
       
    83      * Sets the upper limit of the range.
       
    84      *
       
    85      * @param aMax  The maximum value.
       
    86      */
       
    87     inline void SetMax(const T& aMax)
       
    88         {
       
    89         iMax = aMax;
       
    90         }
       
    91     
       
    92     /**
       
    93      * Sets the type of the range.
       
    94      *
       
    95      * @param aType  The new type.
       
    96      */
       
    97     inline void SetType(TMdERangeType aType)
       
    98         {
       
    99         iType = aType;
       
   100         }
       
   101 
       
   102     /**
       
   103      * Get required size of serialized buffer when this is serialized.
       
   104      *
       
   105      * @return required size of serialized buffer
       
   106      */
       
   107     TUint32 RequiredBufferSize()
       
   108     	{
       
   109      	return TMdESerializeRange::RequiredBufferSize( sizeof(T) );
       
   110     	}
       
   111 
       
   112     /**
       
   113      * Serialize own data to serialized buffer (correct position must be set 
       
   114      * before calling) and return new position of serialized buffer.
       
   115 	 *
       
   116      * @param aBuffer  serialized buffer.
       
   117      */
       
   118     void SerializeL(CMdCSerializationBuffer& aBuffer)
       
   119     	{
       
   120     	TMdESerializeRange::SerializeL( aBuffer, iType, &iMin, &iMax, sizeof(T) );
       
   121     	}
       
   122 
       
   123     /* Constructor. */
       
   124     TMdERange(const T& aMin, const T& aMax, TMdERangeType aType = EMdERangeTypeAny)
       
   125         : iType(aType), iMin(aMin), iMax(aMax)
       
   126         {
       
   127         }
       
   128 
       
   129 	/**
       
   130      * Test if value is in the range.
       
   131      *
       
   132      * @param aValue Tested value.
       
   133 	 */
       
   134     TBool InRange(const T& aValue)
       
   135         {
       
   136         switch( iType )
       
   137         	{        	
       
   138         	case EMdERangeTypeAny:
       
   139         		{
       
   140         		// always match
       
   141         		return ETrue;
       
   142         		}
       
   143         	case EMdERangeTypeEqual:
       
   144         		{
       
   145         		return ( aValue == iMin );
       
   146         		}
       
   147 			case EMdERangeTypeNotEqual:
       
   148 				{
       
   149 				return ( aValue != iMin );
       
   150 				}
       
   151 			case EMdERangeTypeLess:
       
   152 				{
       
   153 				return ( aValue < iMax );
       
   154 				}
       
   155 			case EMdERangeTypeLessOrEqual:
       
   156 				{
       
   157 				return ( aValue <= iMax );
       
   158 				}
       
   159 			case EMdERangeTypeGreater:
       
   160 				{
       
   161 				return ( aValue > iMin );
       
   162 				}
       
   163 			case EMdERangeTypeGreaterOrEqual:
       
   164 				{
       
   165 				return ( aValue >= iMin );
       
   166 				}
       
   167 			case EMdERangeTypeBetween:
       
   168 				{
       
   169 				// edges belong to range
       
   170 				return ( ( iMin <= aValue ) && ( aValue <= iMax ) );
       
   171 				}
       
   172 			case EMdERangeTypeNotBetween:
       
   173 				{
       
   174 				// edges belong to range
       
   175 				return ( ( iMin > aValue ) || ( aValue > iMax ) );
       
   176 				}
       
   177 			default:
       
   178 				{
       
   179 				// should never happen
       
   180 				return EFalse; 
       
   181 				}
       
   182         	}
       
   183 	    }
       
   184 
       
   185 private:
       
   186 
       
   187     /** Type. */
       
   188     TMdERangeType iType;
       
   189 
       
   190     /** Beginning of the range. */
       
   191     T iMin;
       
   192 
       
   193     /** End of the range. */
       
   194     T iMax;
       
   195     };
       
   196 
       
   197 
       
   198 /**
       
   199  * Value range that covers everything.
       
   200  */
       
   201 template <class T>
       
   202 class TMdEAny : public TMdERange<T>
       
   203     {
       
   204 public:
       
   205     TMdEAny()
       
   206         : TMdERange<T>(T(0), T(0), EMdERangeTypeAny) {}
       
   207     };
       
   208 
       
   209 
       
   210 /**
       
   211  * TMdEEquals defines a range that contains only a single point. Thus it
       
   212  * functions as an equivalence operator.
       
   213  */
       
   214 template <class T> 
       
   215 class TMdEEqual : public TMdERange<T>
       
   216     {
       
   217 public:
       
   218     TMdEEqual(const T& aValue) 
       
   219         : TMdERange<T>(aValue, aValue, EMdERangeTypeEqual) {}
       
   220     };
       
   221 
       
   222 /**
       
   223  * Range that contains all values except for one point.
       
   224  */
       
   225 template <class T> 
       
   226 class TMdENotEqual : public TMdERange<T>
       
   227     {
       
   228 public:
       
   229     TMdENotEqual(const T& aValue) 
       
   230         : TMdERange<T>(aValue, aValue, EMdERangeTypeNotEqual) {}
       
   231     };
       
   232 
       
   233 /**
       
   234  * Less-than range.
       
   235  */
       
   236 template <class T> 
       
   237 class TMdELess : public TMdERange<T>
       
   238     {
       
   239 public:
       
   240     TMdELess(const T& aLimit) 
       
   241         : TMdERange<T>(T(0), aLimit, EMdERangeTypeLess) {}
       
   242     };
       
   243 
       
   244 
       
   245 /**
       
   246  * Less-than-or-equal-to range.
       
   247  */
       
   248 template <class T> 
       
   249 class TMdELessEqual : public TMdERange<T>
       
   250     {
       
   251 public:
       
   252     TMdELessEqual(const T& aLimit) 
       
   253         : TMdERange<T>(T(0), aLimit, EMdERangeTypeLessOrEqual) {}
       
   254     };
       
   255 
       
   256 
       
   257 /**
       
   258  * Greater-than range.
       
   259  */
       
   260 template <class T> 
       
   261 class TMdEGreater : public TMdERange<T>
       
   262     {
       
   263 public:
       
   264     TMdEGreater(const T& aLimit) 
       
   265         : TMdERange<T>(aLimit, T(0), EMdERangeTypeGreater) {}
       
   266     };
       
   267 
       
   268 
       
   269 /**
       
   270  * Greater-than-or-equal-to range.
       
   271  */
       
   272 template <class T> 
       
   273 class TMdEGreaterEqual : public TMdERange<T>
       
   274     {
       
   275 public:
       
   276     TMdEGreaterEqual(const T& aLimit) 
       
   277         : TMdERange<T>(aLimit, T(0), EMdERangeTypeGreaterOrEqual) {}
       
   278     };
       
   279 
       
   280 
       
   281 /**
       
   282  * Between range.  The upper and lower limits are inclusive by default.
       
   283  */
       
   284 template <class T> 
       
   285 class TMdEBetween : public TMdERange<T>
       
   286     {
       
   287 public:
       
   288     TMdEBetween(const T& aMin, const T& aMax) 
       
   289         : TMdERange<T>(aMin, aMax, EMdERangeTypeBetween) {}
       
   290     };
       
   291 
       
   292 
       
   293 /**
       
   294  * Not-between range.  The upper and lower limits are inclusive by 
       
   295  * default.
       
   296  */
       
   297 template <class T> 
       
   298 class TMdENotBetween : public TMdERange<T>
       
   299     {
       
   300 public:
       
   301     TMdENotBetween(const T& aMin, const T& aMax) 
       
   302         : TMdERange<T>(aMin, aMax, EMdERangeTypeNotBetween) {}
       
   303     };
       
   304 
       
   305 
       
   306 /* Types that will be used in practice. */
       
   307 
       
   308 /** Value range of type TInt. */
       
   309 typedef TMdERange<TInt> TMdEIntRange;
       
   310 
       
   311 /** Value range of type TUint. */
       
   312 typedef TMdERange<TUint> TMdEUintRange;
       
   313 
       
   314 /** Value range of type TInt64. */
       
   315 typedef TMdERange<TInt64> TMdEInt64Range;
       
   316 
       
   317 /** Value range of type TReal. */
       
   318 typedef TMdERange<TReal> TMdERealRange;
       
   319 
       
   320 /** Value range of type TTime. */
       
   321 typedef TMdERange<TTime> TMdETimeRange;
       
   322 
       
   323 
       
   324 /** Macro for defining the real typenames. Makes four versions of each
       
   325     range type. */
       
   326 #define MDE_DEFINE_ACTUAL_RANGE_TYPES(RangeName) \
       
   327     typedef TMdE##RangeName<TInt> TMdEInt##RangeName; \
       
   328     typedef TMdE##RangeName<TUint> TMdEUint##RangeName; \
       
   329     typedef TMdE##RangeName<TInt64> TMdEInt64##RangeName; \
       
   330     typedef TMdE##RangeName<TReal> TMdEReal##RangeName; \
       
   331     typedef TMdE##RangeName<TTime> TMdETime##RangeName;
       
   332 
       
   333 MDE_DEFINE_ACTUAL_RANGE_TYPES(Any)
       
   334 MDE_DEFINE_ACTUAL_RANGE_TYPES(Equal)
       
   335 MDE_DEFINE_ACTUAL_RANGE_TYPES(NotEqual)
       
   336 MDE_DEFINE_ACTUAL_RANGE_TYPES(Less)
       
   337 MDE_DEFINE_ACTUAL_RANGE_TYPES(LessEqual)
       
   338 MDE_DEFINE_ACTUAL_RANGE_TYPES(Greater)
       
   339 MDE_DEFINE_ACTUAL_RANGE_TYPES(GreaterEqual)
       
   340 MDE_DEFINE_ACTUAL_RANGE_TYPES(Between)
       
   341 MDE_DEFINE_ACTUAL_RANGE_TYPES(NotBetween)
       
   342 
       
   343 #undef MDE_DEFINE_ACTUAL_RANGE_TYPES   
       
   344 
       
   345 #endif /*MDERANGE_INL_*/