1 #ifndef PATHHOOK__HPP
2 #define PATHHOOK__HPP
3 
4 /*  $Id: pathhook.hpp 540952 2017-07-12 15:58:51Z gouriano $
5 * ===========================================================================
6 *
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's official duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Andrei Gourianov
30 *
31 * File Description:
32 *   Helper classes to set serialization hooks by stack path
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbiobj.hpp>
37 
38 
39 /** @addtogroup ObjStreamSupport
40  *
41  * @{
42  */
43 
44 BEGIN_NCBI_SCOPE
45 
46 class CItemInfo;
47 class CObjectStack;
48 class CTypeInfo;
49 
50 class NCBI_XSERIAL_EXPORT CPathHook
51     : protected multimap<CObjectStack*, pair<string, CRef<CObject> > >
52 {
53 public:
54     CPathHook(void);
55     ~CPathHook(void);
56 
IsEmpty(void) const57     bool     IsEmpty(void) const {return m_Empty;}
58     bool     SetHook(CObjectStack* stk, const string& path, CObject* hook);
59     CObject* GetHook(CObjectStack& stk) const;
60 
61     static bool Match(const string& mask, const string& path);
62 
Erase(CObjectStack * stk)63     void  Erase(CObjectStack* stk) {
64         erase(stk);
65     }
66 private:
67     CObject* x_Get(CObjectStack& stk, const string& path) const;
68     bool m_Empty;
69     bool m_Regular;
70     bool m_All;
71     bool m_Wildcard;
72 };
73 
74 
75 class NCBI_XSERIAL_EXPORT CStreamPathHookBase
76     : protected map<string,CRef<CObject> >
77 {
78 public:
79     CStreamPathHookBase(void);
80     ~CStreamPathHookBase(void);
81 
IsEmpty(void) const82     bool     IsEmpty(void) const {return m_Empty;}
83     bool     SetHook(const string& path, CObject* hook);
84     CObject* GetHook(CObjectStack& stk) const;
85 
86     static CTypeInfo* FindType(const CObjectStack& stk);
87     static CItemInfo* FindItem(const CObjectStack& stk);
88 private:
89     CObject* x_Get(const string& path) const;
90     bool m_Empty;
91     bool m_Regular;
92     bool m_All;
93     bool m_Member;
94     bool m_Wildcard;
95 };
96 
97 
98 template <typename TInfo, typename THook>
99 class CStreamPathHook : public CStreamPathHookBase
100 {
101 public:
SetHook(const string & path,THook hook)102     void SetHook(const string& path, THook hook)
103     {
104         CStreamPathHookBase::SetHook(path,hook);
105     }
GetHook(CObjectStack & stk) const106     THook GetHook(CObjectStack& stk) const
107     {
108         return static_cast<THook>(CStreamPathHookBase::GetHook(stk));
109     }
FindItem(const CObjectStack & stk)110     static TInfo FindItem(const CObjectStack& stk)
111     {
112         return dynamic_cast<TInfo>(CStreamPathHookBase::FindItem(stk));
113     }
114 };
115 
116 
117 template <typename THook>
118 class CStreamObjectPathHook : public CStreamPathHookBase
119 {
120 public:
SetHook(const string & path,THook hook)121     void SetHook(const string& path, THook hook)
122     {
123         CStreamPathHookBase::SetHook(path,hook);
124     }
GetHook(CObjectStack & stk) const125     THook GetHook(CObjectStack& stk) const
126     {
127         return static_cast<THook>(CStreamPathHookBase::GetHook(stk));
128     }
129 };
130 
131 END_NCBI_SCOPE
132 
133 #endif  /* PATHHOOK__HPP */
134 
135 
136 /* @} */
137