photosgallery/inc/glxid.h
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Type-safe unique id template
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #ifndef T_GLXID_H
       
    22 #define T_GLXID_H
       
    23 
       
    24 #include "e32std.h"
       
    25 #include "glxpanic.h"
       
    26 
       
    27 /** Invalid or undefined id value */
       
    28 // Use "id == KGlxIdNone" and "id != KGlxIdNone" to check for
       
    29 // id validity.
       
    30 class TGlxIdNone {};
       
    31 const TGlxIdNone KGlxIdNone = TGlxIdNone();
       
    32 
       
    33 /**
       
    34  *  TGlxId
       
    35  *
       
    36  *  Type-safe template for ids
       
    37  *  Id value of KMaxTUint is consided to be undefined.
       
    38  *  @usage Define like this:
       
    39  *            class TMyIdBase {}; 
       
    40  *            typedef TGlxId<TMyIdBase> TMyId;
       
    41  *            Now use can use TMyId in other code
       
    42  */
       
    43 template <class T> 
       
    44 class TGlxId : public T {
       
    45 public:
       
    46     inline TGlxId(); 
       
    47     inline TGlxId(const TGlxId<T>& aId);
       
    48     inline TGlxId(TUint aIdValue); 
       
    49     inline TGlxId(const TGlxIdNone&);
       
    50     inline TGlxId<T>& operator=(const TGlxId<T>& aId);
       
    51     inline TGlxId<T>& operator=(TUint aIdValue);
       
    52         
       
    53     inline TGlxId<T>& operator=(const TGlxIdNone&);
       
    54     inline TGlxId<T> operator++(int); // Postfix, keep dummy parameter as int
       
    55     inline TBool operator!=(const TGlxId<T>& aId) const;
       
    56     inline TBool operator==(const TGlxId<T>& aId) const;
       
    57     inline TBool operator!=(const TGlxIdNone&) const;
       
    58     inline TBool operator==(const TGlxIdNone&) const;
       
    59     inline TBool operator>(const TGlxId<T>& aId) const;
       
    60     inline TBool operator<(const TGlxId<T>& aId) const;
       
    61 
       
    62     inline TUint Value() const;
       
    63     inline void SetValue(TUint aIdValue); // Allows setting undefined as value (KMaxTUint)
       
    64 
       
    65 private:
       
    66     TUint iIdValue;
       
    67 };
       
    68 
       
    69 /**
       
    70  * MGlxIdProvider
       
    71  * Abstract interface class to provide ids 
       
    72  * Define as MGlxIdProvider<TMyId>
       
    73  */ 
       
    74 template <class T>
       
    75 class MGlxIdProvider {
       
    76 public:
       
    77     /**
       
    78      * getNextId
       
    79      * Returns the next unique id.
       
    80      * Returns value in the argument, since otherwise cannot have the same
       
    81      * deriving class provide multiple different types of ids
       
    82      */
       
    83     virtual void NextId(T& aId) = 0;
       
    84 };
       
    85 
       
    86 /**
       
    87  * TGlxDefaultIdProvider
       
    88  * Simple implementation of id provider
       
    89  */ 
       
    90 template <class T>
       
    91 class TGlxDefaultIdProvider : public MGlxIdProvider<T> {
       
    92 private:
       
    93     TUint iNextIdValue;
       
    94 public:
       
    95     inline TGlxDefaultIdProvider();
       
    96     virtual void NextId(T& aId);
       
    97 };
       
    98 
       
    99 #include "glxid.inl"
       
   100 
       
   101 #endif // T_GLXID_H