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