微对象的初入(6)

1.2 微对象存储-中间层(C++)

1.2.1 接口定义

  • 类型chon重定义(这一层主要给程序员的,所以用STL感觉合适)
typedef LPVOID HOB;

typedef pair<string, unsigned char>            OB_PAIR_TYPE_NULL_T;
typedef pair<string, bool>                    OB_PAIR_TYPE_BOOL_T;
typedef pair<string, int>                    OB_PAIR_TYPE_INT_T;
typedef pair<string, double>                OB_PAIR_TYPE_DOUBLE_T;
typedef pair<string, string>                OB_PAIR_TYPE_STRING_T;

typedef map<string, unsigned char>            OB_MAP_TYPE_NULL_T;
typedef map<string, bool>                    OB_MAP_TYPE_BOOL_T;
typedef map<string, int>                    OB_MAP_TYPE_INT_T;
typedef map<string, double>                    OB_MAP_TYPE_DOUBLE_T;
typedef map<string, string>                    OB_MAP_TYPE_STRING_T;

typedef pair<string, OB_MAP_TYPE_NULL_T>    OB_PAIR_TYPE_ARRAY_NULL_T;
typedef pair<string, OB_MAP_TYPE_BOOL_T>    OB_PAIR_TYPE_ARRAY_BOOL_T;
typedef pair<string, OB_MAP_TYPE_INT_T>        OB_PAIR_TYPE_ARRAY_INT_T;
typedef pair<string, OB_MAP_TYPE_DOUBLE_T>    OB_PAIR_TYPE_ARRAY_DOUBLE_T;
typedef pair<string, OB_MAP_TYPE_STRING_T>    OB_PAIR_TYPE_ARRAY_STRING_T;

typedef map<string, OB_MAP_TYPE_NULL_T>        OB_MAP_TYPE_ARRAY_NULL_T;
typedef map<string, OB_MAP_TYPE_BOOL_T>        OB_MAP_TYPE_ARRAY_BOOL_T;
typedef map<string, OB_MAP_TYPE_INT_T>        OB_MAP_TYPE_ARRAY_INT_T;
typedef map<string, OB_MAP_TYPE_DOUBLE_T>    OB_MAP_TYPE_ARRAY_DOUBLE_T;
typedef map<string, OB_MAP_TYPE_STRING_T>    OB_MAP_TYPE_ARRAY_STRING_T;


1.2.2 Class定义

  • 这块没整合好,主要是基层的继承是否暴露的考虑 (即基层不可见,可直接跨平台移植,中间层会用到许多OS SDK API的东东,如:NTFS文件系统直接映射与读写,正考虑中)
class Cobbase
{
public:
    Cobbase();
    ~Cobbase();

    BOOL ob_init();
    BOOL ob_conn(OB_STRING_T pobName);
    BOOL ob_close();

    BOOL ob_insert_null(OB_STRING_T lpName);
    BOOL ob_insert_bool(OB_STRING_T lpName, OB_BOOL_T data);
    BOOL ob_insert_int(OB_STRING_T lpName, OB_INT_T data);
    BOOL ob_insert_double(OB_STRING_T lpName, OB_DOUBLE_T data);
    BOOL ob_insert_string(OB_STRING_T lpName, OB_STRING_T data);
    BOOL ob_insert_array(OB_STRING_T lpName, OB_ARRAY_T data, enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count);
    BOOL ob_insert_array_string(OB_STRING_T lpName, OB_ARRAY_STRING_T data, OB_UINT_T count);
    
    BOOL ob_set_node_next();
    BOOL ob_set_node(OB_STRING_T pTreeName, OB_UINT_T nIndex = 0);

    BOOL ob_delete_current();
    BOOL ob_delete_node(OB_STRING_T pTreeName, OB_UINT_T nIndex);

public:
    POB_TREE_HEAD_T m_obHead;
    POB_RESULT_T m_obResult;
    POB_TREE_NODE_T m_curNode;

private:
    POB_TREE_NODE_T _find(POB_TREE_NODE_T, OB_STRING_T pTreeName);

};

#define OB_VALUE_NULL ((unsigned char)0)

class OBAPI_API Cobapi
{
public:
    Cobapi(void);

    BOOL Init();
    BOOL Conn(LPCTSTR pobName);
    VOID Close();

    class OBPAIR {
        OBPAIR() {}
        ~OBPAIR() {}

    };

    BOOL Insert(OB_PAIR_TYPE_NULL_T pData);
    BOOL Insert(OB_PAIR_TYPE_BOOL_T pData);
    BOOL Insert(OB_PAIR_TYPE_INT_T pData);
    BOOL Insert(OB_PAIR_TYPE_DOUBLE_T pData);
    BOOL Insert(OB_PAIR_TYPE_STRING_T pData);

    BOOL InsertArray(OB_PAIR_TYPE_ARRAY_NULL_T pData);
    BOOL InsertArray(OB_PAIR_TYPE_ARRAY_BOOL_T pData);
    BOOL InsertArray(OB_PAIR_TYPE_ARRAY_INT_T pData);
    BOOL InsertArray(OB_PAIR_TYPE_ARRAY_DOUBLE_T pData);
    BOOL InsertArray(OB_PAIR_TYPE_ARRAY_STRING_T pData);

    VOID SetCursorToNext();
    VOID SetCursor(LPCTSTR pTreeName, DWORD dwIndex = 0);

    BOOL Delete();
    BOOL DeleteAll();

    char m_csMsg[256];
    bool m_bResult;
    int m_nCode;
private:
    void Error();
    void Error(bool bResult, int nCode, char* pMsg);
    HOB m_hob;
};

 

1.2.3 Class实现

  1 #pragma region ob base class
  2 
  3 Cobbase::Cobbase()
  4 {
  5     ob_init();
  6 }
  7 
  8 
  9 Cobbase::~Cobbase()
 10 {
 11 }
 12 
 13 BOOL Cobbase::ob_init()
 14 {
 15     m_obHead = NULL;
 16     m_obResult = NULL;
 17     m_curNode = NULL;
 18 
 19     return TRUE;
 20 }
 21 
 22 BOOL Cobbase::ob_conn(OB_STRING_T pobName)
 23 {
 24     m_obHead = new_ob(&m_obResult, (PTBYTE)pobName, strlen((char*)pobName), (PTBYTE)"1.00", 4);
 25     if (m_obHead == NULL)
 26     {
 27         return FALSE;
 28     }
 29     m_curNode = m_obHead->root;
 30     return TRUE;
 31 }
 32 
 33 BOOL Cobbase::ob_close()
 34 {
 35     if (m_obHead != NULL)
 36     {
 37         delete_ob(m_obHead);
 38     }
 39     return TRUE;
 40 }
 41 
 42 BOOL Cobbase::ob_insert_null(OB_STRING_T lpName)
 43 {
 44     if (m_obHead != NULL)
 45     {
 46         POB_TREE_NODE_T pnode = NULL;
 47         bool bRet = ob_new_node_null(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), (OB_NULL_T)0);
 48         if (bRet == true)
 49         {
 50             if (m_curNode == NULL)
 51             {
 52                 m_obHead->root = pnode;
 53                 m_curNode = pnode;
 54                 m_obResult = new OB_RESULT_T();
 55                 memset(m_obResult, 0x00, sizeof(OB_RESULT_T));
 56                 m_obResult->result = true;
 57             }
 58             else
 59             {
 60                 bRet = insert_node(m_curNode, &pnode, &m_obResult);
 61                 if (bRet == true)
 62                 {
 63                     m_curNode = pnode;
 64                     return TRUE;
 65                 }
 66                 else
 67                 {
 68                     delete_node(&pnode, &m_obResult);
 69                 }
 70             }
 71         }
 72     }
 73 
 74     return FALSE;
 75 }
 76 
 77 BOOL Cobbase::ob_insert_bool(OB_STRING_T lpName, OB_BOOL_T data)
 78 {
 79     if (m_obHead != NULL)
 80     {
 81         POB_TREE_NODE_T pnode = NULL;
 82         bool bRet = ob_new_node_bool(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data);
 83         if (bRet == true)
 84         {
 85             bRet = insert_node(m_curNode, &pnode, &m_obResult);
 86             if (bRet == true)
 87             {
 88                 m_curNode = pnode;
 89                 return TRUE;
 90             }
 91             else
 92             {
 93                 delete_node(&pnode, &m_obResult);
 94             }
 95         }
 96     }
 97     return FALSE;
 98 }
 99 BOOL Cobbase::ob_insert_int(OB_STRING_T lpName, OB_INT_T data)
100 {
101     if (m_obHead != NULL)
102     {
103         POB_TREE_NODE_T pnode = NULL;
104         bool bRet = ob_new_node_integer(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data);
105         if (bRet == true)
106         {
107             bRet = insert_node(m_curNode, &pnode, &m_obResult);
108             if (bRet == true)
109             {
110                 m_curNode = pnode;
111                 return TRUE;
112             }
113             else
114             {
115                 delete_node(&pnode, &m_obResult);
116             }
117         }
118     }
119     return FALSE;
120 }
121 BOOL Cobbase::ob_insert_double(OB_STRING_T lpName, OB_DOUBLE_T data)
122 {
123     if (m_obHead != NULL)
124     {
125         POB_TREE_NODE_T pnode = NULL;
126         bool bRet = ob_new_node_double(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data);
127         if (bRet == true)
128         {
129             bRet = insert_node(m_curNode, &pnode, &m_obResult);
130             if (bRet == true)
131             {
132                 m_curNode = pnode;
133                 return TRUE;
134             }
135             else
136             {
137                 delete_node(&pnode, &m_obResult);
138             }
139         }
140     }
141     return FALSE;
142 }
143 BOOL Cobbase::ob_insert_string(OB_STRING_T lpName, OB_STRING_T data)
144 {
145     if (m_obHead != NULL)
146     {
147         POB_TREE_NODE_T pnode = NULL;
148         bool bRet = ob_new_node_string(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data, _tcslen((LPTSTR)data));
149         if (bRet == true)
150         {
151             bRet = insert_node(m_curNode, &pnode, &m_obResult);
152             if (bRet == true)
153             {
154                 m_curNode = pnode;
155                 return TRUE;
156             }
157             else
158             {
159                 delete_node(&pnode, &m_obResult);
160             }
161         }
162     }
163     return FALSE;
164 }
165 
166 BOOL Cobbase::ob_insert_array(OB_STRING_T lpName, OB_ARRAY_T data, enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count)
167 {
168     if (m_obHead != NULL)
169     {
170         POB_TREE_NODE_T pnode = NULL;
171         bool bRet = ob_new_node_array(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data, type,count);
172         if (bRet == true)
173         {
174             bRet = insert_node(m_curNode, &pnode, &m_obResult);
175             if (bRet == true)
176             {
177                 m_curNode = pnode;
178                 return TRUE;
179             }
180             else
181             {
182                 delete_node(&pnode, &m_obResult);
183             }
184         }
185     }
186     return FALSE;
187 }
188 
189 BOOL Cobbase::ob_insert_array_string(OB_STRING_T lpName, OB_ARRAY_STRING_T data, OB_UINT_T count)
190 {
191     if (m_obHead != NULL)
192     {
193         POB_TREE_NODE_T pnode = NULL;
194         bool bRet = ob_new_node_array_string(&pnode, &m_obResult, lpName, _tcslen((LPTSTR)lpName), data, count);
195         if (bRet == true)
196         {
197             bRet = insert_node(m_curNode, &pnode, &m_obResult);
198             if (bRet == true)
199             {
200                 m_curNode = pnode;
201                 return TRUE;
202             }
203             else
204             {
205                 delete_node(&pnode, &m_obResult);
206             }
207         }
208     }
209     return FALSE;
210 }
211 
212 BOOL Cobbase::ob_set_node_next()
213 {
214     if (m_curNode != NULL && m_curNode->next != NULL)
215     {
216         m_curNode = m_curNode->next;
217         return TRUE;
218     }
219     return FALSE;
220 }
221 
222 // strTree="ROOT\ChildLevel1\ChildLevel2\ChildLevel3"
223 POB_TREE_NODE_T Cobbase::_find(POB_TREE_NODE_T pnode, OB_STRING_T pTreeName)
224 {
225     TCHAR *cur = NULL;
226     TCHAR *pos = NULL;
227     TCHAR *old = (LPTSTR)pTreeName;
228 
229     cur = _tcstok_s((LPTSTR)pTreeName, _T("\\"), &pos);
230 
231     while (cur != NULL)
232     {
233         if (cur - old - 1 == pnode->obj->len &&
234             memcmp(pnode->obj->name, old, cur - old - 1))
235         {
236             if (pnode->child != NULL && pnode->child->obj != NULL)
237             {
238                 pnode = pnode->child;
239             }
240             else
241             {
242                 pnode = pnode->next;
243             }
244         }
245         cur = old;
246         cur = _tcstok_s(NULL, _T("\\"), &pos);
247     }
248     return pnode;
249 }
250 
251 BOOL Cobbase::ob_set_node(OB_STRING_T pTreeName, OB_UINT_T nIndex)
252 {
253     if (m_obHead != NULL && m_obHead->root != NULL && m_obHead->root->obj != NULL)
254     {
255         POB_TREE_NODE_T pNode = m_obHead->root;
256 
257         POB_TREE_NODE_T pFound = _find(pNode, pTreeName);
258         if (pFound != NULL)
259         {
260             m_curNode = pFound;
261             return TRUE;
262         }
263     }
264     return FALSE;
265 }
266 
267 
268 BOOL Cobbase::ob_delete_current()
269 {
270     if (m_curNode != NULL)
271     {
272         return delete_node(&m_curNode, &m_obResult);
273     }
274     return FALSE;
275 }
276 
277 BOOL Cobbase::ob_delete_node(OB_STRING_T pTreeName, OB_UINT_T nIndex)
278 {
279     BOOL bRet = ob_set_node(pTreeName, nIndex);
280     if (bRet == TRUE)
281     {
282         return delete_node(&m_curNode, &m_obResult);
283     }
284     return FALSE;
285 }
286 
287 
288 #pragma endregion ob base class
289 
290 
291 
292 using namespace std;
293 
294 #pragma region C++ Interface
295 
296 template<typename K, typename V>
297 typename V* ToArray(const map<K, V>& mp)
298 {
299     typename V* arr = (typename V*)malloc(sizeof(typename V));
300     int i = 0;
301     for (auto it = mp.begin(); it != mp.end(); it++, i++)
302     {
303         arr[i] = (typename V)(it->second);
304     }
305     return arr;
306     /////////////////////////
307     //for_each(mp.begin(), mp.end(), [](const pair<const typename K,  typename V> & val) {
308     //    if (typeid(typename V).name()[0] == 'b')
309     //    {
310     //        vector<V>* vec = new vector<V>();
311     //        vec->push_back(val.second);
312     //    }
313     //    return new vector<V>(0);
314     //});
315     ///////////////////////////////////////////
316     //vector<V>* vec = new vector<V>();
317 
318     //for (auto it = mp.begin(); it != mp.end(); it++)
319     //{
320     //    //(pair<typename K, typename V>)it->first;
321     //    vec->push_back(it->second);
322     //}
323     //return vec;
324 }
325 
326 
327 Cobapi::Cobapi()
328 {
329     return;
330 }
331 BOOL Cobapi::Init()
332 {
333     m_hob = NULL;
334     memset(m_csMsg, 0x00, sizeof(m_csMsg));
335     m_bResult = FALSE;
336     m_nCode = 0;
337     return TRUE;
338 }
339 
340 BOOL Cobapi::Conn(LPCTSTR pobName)
341 {
342     Cobbase* pob = new Cobbase();
343     if (pob != NULL)
344     {
345         pob->ob_init();
346         BOOL bRet = pob->ob_conn((OB_STRING_T)pobName);
347         if (bRet == TRUE)
348         {
349             m_hob = pob;
350             return TRUE;
351         }
352         else
353         {
354             Error();
355             delete pob;
356         }
357     }
358     Error(false, 2, "new Cobbase Failed.");
359 
360     return FALSE;
361 }
362 
363 VOID Cobapi::Close()
364 {
365     if (m_hob != NULL)
366     {
367         Cobbase* pob = (Cobbase*)m_hob;
368         pob->ob_close();
369     }
370 }
371 
372 void Cobapi::Error(bool bResult, int nCode, char* pMsg)
373 {
374     strcpy_s(m_csMsg, (char*)pMsg);
375     m_bResult = bResult;
376     m_nCode = nCode;
377 }
378 
379 void Cobapi::Error()
380 {
381     if (m_hob != NULL)
382     {
383         Cobbase* pob = (Cobbase*)m_hob;
384         if (pob->m_obResult != NULL && pob->m_obResult->msg != NULL)
385         {
386             strcpy_s(m_csMsg, (char*)pob->m_obResult->msg);
387         }
388         m_bResult = pob->m_obResult->result;
389         m_nCode = pob->m_obResult->code;
390     }
391 }
392 
393 BOOL Cobapi::Insert(OB_PAIR_TYPE_NULL_T pData)
394 {
395     BOOL bRet = FALSE;
396     if (m_hob != NULL)
397     {
398         Cobbase* pob = (Cobbase*)m_hob;
399         bRet = pob->ob_insert_null((OB_STRING_T)pData.first.data());
400         Error();
401     }
402     return bRet;
403 }
404 BOOL Cobapi::Insert(OB_PAIR_TYPE_BOOL_T pData)
405 {
406     BOOL bRet = FALSE;
407     if (m_hob != NULL)
408     {
409         Cobbase* pob = (Cobbase*)m_hob;
410         bRet = pob->ob_insert_bool((OB_STRING_T)pData.first.data(), (OB_BOOL_T)pData.second);
411         Error();
412     }
413     return bRet;
414 }
415 BOOL Cobapi::Insert(OB_PAIR_TYPE_INT_T pData)
416 {
417     BOOL bRet = FALSE;
418     if (m_hob != NULL)
419     {
420         Cobbase* pob = (Cobbase*)m_hob;
421         bRet = pob->ob_insert_int((OB_STRING_T)pData.first.data(), (OB_INT_T)pData.second);
422         Error();
423     }
424     return bRet;
425 }
426 BOOL Cobapi::Insert(OB_PAIR_TYPE_DOUBLE_T pData)
427 {
428     BOOL bRet = FALSE;
429     if (m_hob != NULL)
430     {
431         Cobbase* pob = (Cobbase*)m_hob;
432         bRet = pob->ob_insert_double((OB_STRING_T)pData.first.data(), (OB_DOUBLE_T)pData.second);
433         Error();
434     }
435     return bRet;
436 }
437 
438 BOOL Cobapi::Insert(OB_PAIR_TYPE_STRING_T pData)
439 {
440     BOOL bRet = FALSE;
441     if (m_hob != NULL)
442     {
443         Cobbase* pob = (Cobbase*)m_hob;
444         bRet = pob->ob_insert_string((OB_STRING_T)pData.first.data(), (OB_STRING_T)pData.second.data());
445         Error();
446     }
447     return bRet;
448 }
449 
450 BOOL Cobapi::InsertArray(OB_PAIR_TYPE_ARRAY_NULL_T pData)
451 {
452     BOOL bRet = FALSE;
453     if (m_hob != NULL && pData.second.size() > 0)
454     {
455         Cobbase* pob = (Cobbase*)m_hob;
456         OB_MAP_TYPE_NULL_T value = pData.second;
457         OB_NULL_T* pv = (OB_NULL_T*)ToArray(value);
458         bRet = pob->ob_insert_array((OB_STRING_T)pData.first.data(), pv, ENUM_OB_ENDOBJ_NULL, value.size());
459         Error();
460     }
461     return bRet;
462 }
463 
464 BOOL Cobapi::InsertArray(OB_PAIR_TYPE_ARRAY_BOOL_T pData)
465 {
466     BOOL bRet = FALSE;
467     if (m_hob != NULL && pData.second.size() > 0)
468     {
469         Cobbase* pob = (Cobbase*)m_hob;
470         OB_MAP_TYPE_BOOL_T value = pData.second;
471         OB_BOOL_T* pv = (OB_BOOL_T*)ToArray(value);
472         bRet = pob->ob_insert_array((OB_STRING_T)pData.first.data(), pv, ENUM_OB_ENDOBJ_BOOL, value.size());
473         Error();
474     }
475     return bRet;
476 }
477 BOOL Cobapi::InsertArray(OB_PAIR_TYPE_ARRAY_INT_T pData)
478 {
479     BOOL bRet = FALSE;
480     if (m_hob != NULL && pData.second.size() > 0)
481     {
482         Cobbase* pob = (Cobbase*)m_hob;
483         OB_MAP_TYPE_INT_T value = pData.second;
484         OB_INT_T* pv = (OB_INT_T*)ToArray(value);
485         bRet = pob->ob_insert_array((OB_STRING_T)pData.first.data(), pv, ENUM_OB_ENDOBJ_INTEGER, value.size());
486         Error();
487     }
488     return bRet;
489 }
490 BOOL Cobapi::InsertArray(OB_PAIR_TYPE_ARRAY_DOUBLE_T pData)
491 {
492     BOOL bRet = FALSE;
493     if (m_hob != NULL && pData.second.size() > 0)
494     {
495         Cobbase* pob = (Cobbase*)m_hob;
496         OB_MAP_TYPE_DOUBLE_T value = pData.second;
497         OB_DOUBLE_T* pv = (OB_DOUBLE_T*)ToArray(value);
498         bRet = pob->ob_insert_array((OB_STRING_T)pData.first.data(), pv, ENUM_OB_ENDOBJ_DOUBLE, value.size());
499         Error();
500     }
501     return bRet;
502 }
503 BOOL Cobapi::InsertArray(OB_PAIR_TYPE_ARRAY_STRING_T pData)
504 {
505     BOOL bRet = FALSE;
506     if (m_hob != NULL && pData.second.size() > 0)
507     {
508         Cobbase* pob = (Cobbase*)m_hob;
509         OB_MAP_TYPE_STRING_T value = pData.second;
510         OB_STRING_T* pv = (OB_STRING_T*)ToArray(value);
511         bRet = pob->ob_insert_array_string((OB_STRING_T)pData.first.data(), pv, value.size());
512         Error();
513     }
514     return bRet;
515 }
516 
517 VOID Cobapi::SetCursorToNext()
518 {
519     if (m_hob != NULL)
520     {
521         Cobbase* pob = (Cobbase*)m_hob;
522         pob->ob_set_node_next();
523     }
524 }
525 
526 VOID Cobapi::SetCursor(LPCTSTR pTreeName, DWORD dwIndex)
527 {
528     if (m_hob != NULL)
529     {
530         Cobbase* pob = (Cobbase*)m_hob;
531         pob->ob_set_node((OB_STRING_T)pTreeName, (OB_UINT_T)dwIndex);
532     }
533 }
534 
535 BOOL Cobapi::Delete()
536 {
537     if (m_hob != NULL)
538     {
539         Cobbase* pob = (Cobbase*)m_hob;
540         pob->ob_delete_current();
541         return FALSE;
542     }
543     return FALSE;
544 }
545 
546 BOOL Cobapi::DeleteAll()
547 {
548     if (m_hob != NULL)
549     {
550         Cobbase* pob = (Cobbase*)m_hob;
551         pob->ob_delete_current();
552         return FALSE;
553     }
554     return FALSE;
555 }
556 
557 
558 #pragma endregion C++ Interface

 

上一篇:ollvm 使用——“Cannot open /dev/random”错误的解决方法


下一篇:2021-03-18