NptCommon.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Common Definitions
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_COMMON_H_
00011 #define _NPT_COMMON_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptTypes.h"
00017 #include "NptResults.h"
00018 
00019 /*----------------------------------------------------------------------
00020 |   NPT_ObjectDeleter
00021 +---------------------------------------------------------------------*/
00022 template <class T>
00023 class NPT_ObjectDeleter {
00024 public:
00025     void operator()(T* object) const {
00026         delete object;
00027     }
00028 };
00029 
00030 /*----------------------------------------------------------------------
00031 |   NPT_ObjectComparator
00032 +---------------------------------------------------------------------*/
00033 template <class T>
00034 class NPT_ObjectComparator {
00035 public:
00036     NPT_ObjectComparator(T& object) : m_Object(object) {}
00037     bool operator()(const T& object) const {
00038         return object == m_Object;
00039     }
00040 private:
00041     T& m_Object;
00042 };
00043 
00044 /*----------------------------------------------------------------------
00045 |   NPT_ContainerFind
00046 +---------------------------------------------------------------------*/
00047 template <typename T, typename P>
00048 NPT_Result NPT_ContainerFind(T&                   container, 
00049                              const P&             predicate, 
00050                              typename T::Element& item, 
00051                              NPT_Ordinal          n=0) 
00052 {
00053     typename T::Iterator found = container.Find(predicate, n);
00054     if (found) {
00055         item = *found;
00056         return NPT_SUCCESS;
00057     } else {
00058         return NPT_ERROR_NO_SUCH_ITEM;
00059     }
00060 }
00061 
00062 /*----------------------------------------------------------------------
00063 |   NPT_UntilResultEquals
00064 +---------------------------------------------------------------------*/
00065 class NPT_UntilResultEquals
00066 {
00067 public:
00068     // methods
00069     NPT_UntilResultEquals(NPT_Result condition_result, 
00070                           NPT_Result return_value = NPT_SUCCESS) :
00071       m_ConditionResult(condition_result),
00072       m_ReturnValue(return_value) {}
00073     bool operator()(NPT_Result result, NPT_Result& return_value) const {
00074         if (result == m_ConditionResult) {
00075             return_value = m_ReturnValue;
00076             return true;
00077         } else {
00078             return false;
00079         }
00080     }
00081 
00082 private:
00083     // members
00084     NPT_Result m_ConditionResult;
00085     NPT_Result m_ReturnValue;
00086 };
00087 
00088 /*----------------------------------------------------------------------
00089 |   NPT_UntilResultNotEquals
00090 +---------------------------------------------------------------------*/
00091 class NPT_UntilResultNotEquals
00092 {
00093 public:
00094     // methods
00095     NPT_UntilResultNotEquals(NPT_Result condition_result) :
00096       m_ConditionResult(condition_result) {}
00097     bool operator()(NPT_Result result, NPT_Result& return_value) const {
00098         if (result != m_ConditionResult) {
00099             return_value = result;
00100             return true;
00101         } else {
00102             return false;
00103         }
00104     }
00105 
00106 private:
00107     // members
00108     NPT_Result m_ConditionResult;
00109 };
00110 
00111 /*----------------------------------------------------------------------
00112 |   NPT_PropertyValue
00113 +---------------------------------------------------------------------*/
00114 class NPT_PropertyValue
00115 {
00116  public:
00117     // typedefs
00118     typedef enum {UNKNOWN, INTEGER, STRING} Type;
00119 
00120     // methods
00121     NPT_PropertyValue() : m_Type(UNKNOWN), m_Integer(0) {}
00122     NPT_PropertyValue(NPT_Integer value) : m_Type(INTEGER), m_Integer(value) {}
00123     NPT_PropertyValue(const char* value) : m_Type(STRING),  m_String(value)  {}
00124 
00125     // members
00126     Type m_Type;
00127     union {
00128         NPT_Integer m_Integer;
00129         const char* m_String;
00130     };
00131 };
00132 
00133 #endif // _NPT_COMMON_H_
00134