kodi
NptCommon.h
1 /*****************************************************************
2 |
3 | Neptune - Common Definitions
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_COMMON_H_
33 #define _NPT_COMMON_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptResults.h"
40 
41 /*----------------------------------------------------------------------
42 | NPT_ObjectDeleter
43 +---------------------------------------------------------------------*/
44 template <class T>
46 public:
47  void operator()(T* object) const {
48  delete object;
49  }
50 };
51 
52 /*----------------------------------------------------------------------
53 | NPT_ObjectComparator
54 +---------------------------------------------------------------------*/
55 template <class T>
57 public:
58  NPT_ObjectComparator(T& object) : m_Object(object) {}
59  bool operator()(const T& object) const {
60  return object == m_Object;
61  }
62 private:
63  T& m_Object;
64 };
65 
66 /*----------------------------------------------------------------------
67 | NPT_ContainerFind
68 +---------------------------------------------------------------------*/
69 template <typename T, typename P>
70 NPT_Result NPT_ContainerFind(T& container,
71  const P& predicate,
72  typename T::Element& item,
73  NPT_Ordinal n=0)
74 {
75  typename T::Iterator found = container.Find(predicate, n);
76  if (found) {
77  item = *found;
78  return NPT_SUCCESS;
79  } else {
80  return NPT_ERROR_NO_SUCH_ITEM;
81  }
82 }
83 
84 /*----------------------------------------------------------------------
85 | NPT_ContainerFind
86 +---------------------------------------------------------------------*/
87 template <typename T, typename P>
88 NPT_Result NPT_ContainerFind(T& container,
89  const P& predicate,
90  typename T::Iterator& iter,
91  NPT_Ordinal n=0)
92 {
93  iter = container.Find(predicate, n);
94  return iter?NPT_SUCCESS:NPT_ERROR_NO_SUCH_ITEM;
95 }
96 
97 /*----------------------------------------------------------------------
98 | NPT_UntilResultEquals
99 +---------------------------------------------------------------------*/
101 {
102 public:
103  // methods
104  NPT_UntilResultEquals(NPT_Result condition_result,
105  NPT_Result return_value = NPT_SUCCESS) :
106  m_ConditionResult(condition_result),
107  m_ReturnValue(return_value) {}
108  bool operator()(NPT_Result result, NPT_Result& return_value) const {
109  if (result == m_ConditionResult) {
110  return_value = m_ReturnValue;
111  return true;
112  } else {
113  return false;
114  }
115  }
116 
117 private:
118  // members
119  NPT_Result m_ConditionResult;
120  NPT_Result m_ReturnValue;
121 };
122 
123 /*----------------------------------------------------------------------
124 | NPT_UntilResultNotEquals
125 +---------------------------------------------------------------------*/
127 {
128 public:
129  // methods
130  NPT_UntilResultNotEquals(NPT_Result condition_result) :
131  m_ConditionResult(condition_result) {}
132  bool operator()(NPT_Result result, NPT_Result& return_value) const {
133  if (result != m_ConditionResult) {
134  return_value = result;
135  return true;
136  } else {
137  return false;
138  }
139  }
140 
141 private:
142  // members
143  NPT_Result m_ConditionResult;
144 };
145 
146 /*----------------------------------------------------------------------
147 | NPT_PropertyValue
148 +---------------------------------------------------------------------*/
150 {
151  public:
152  // typedefs
153  typedef enum {UNKNOWN, INTEGER, STRING} Type;
154 
155  // methods
156  NPT_PropertyValue() : m_Type(UNKNOWN), m_Integer(0) {}
157  NPT_PropertyValue(int value) : m_Type(INTEGER), m_Integer(value) {}
158  NPT_PropertyValue(const char* value) : m_Type(STRING), m_String(value) {}
159 
160  // members
161  Type m_Type;
162  union {
163  int m_Integer;
164  const char* m_String;
165  };
166 };
167 
168 #endif // _NPT_COMMON_H_
169 
Definition: NptCommon.h:100
Definition: NptCommon.h:126
Definition: NptCommon.h:149
Definition: NptCommon.h:56
Definition: NptCommon.h:45