NCBI C++ ToolKit
serialutil.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef SERIALUTIL__HPP
2 #define SERIALUTIL__HPP
3 
4 /* $Id: serialutil.hpp 68351 2015-07-30 14:03:40Z ucko $
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: Eugene Vasilchenko
30 *
31 * File Description:
32 * !!! PUT YOUR DESCRIPTION HERE !!!
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbiobj.hpp>
37 #include <serial/serialdef.hpp>
38 
39 
40 /** @addtogroup GenClassSupport
41  *
42  * @{
43  */
44 
45 
47 
48 // helper template for various types:
49 template<typename T>
51 {
52 public:
53  typedef T TObjectType; // type of object
54 
55  // object getters:
56  static TObjectType& Get(TObjectPtr object)
57  {
58  return *static_cast<TObjectType*>(object);
59  }
60  static const TObjectType& Get(TConstObjectPtr object)
61  {
62  return *static_cast<const TObjectType*>(object);
63  }
65  {
66  return static_cast<TObjectType*>(object);
67  }
68  static const TObjectType* GetPointer(TConstObjectPtr object)
69  {
70  return static_cast<const TObjectType*>(object);
71  }
72 
73  // set of SafeCast functions which will check validity of casting by
74  // dynamic_cast<> in debug mode and will use static_cast<> in release
75  // mode for performance.
77  {
78  _ASSERT(dynamic_cast<const TObjectType*>(type));
79  return static_cast<const TObjectType*>(type);
80  }
81  static const TObjectType* SafeCast(const CObject* obj)
82  {
83  _ASSERT(dynamic_cast<const TObjectType*>(obj));
84  return static_cast<const TObjectType*>(obj);
85  }
87  {
88  _ASSERT(dynamic_cast<TObjectType*>(obj));
89  return static_cast<TObjectType*>(obj);
90  }
91 
92 private:
93  static const TObjectType* SafeCast2(TTypeInfo /*selector*/,
94  const void* ptr)
95  {
96  return SafeCast(static_cast<TTypeInfo>(ptr));
97  }
98  static const TObjectType* SafeCast2(const CObject* /*selector*/,
99  const void* ptr)
100  {
101  return SafeCast(static_cast<const CObject*>(ptr));
102  }
103  static TObjectType* SafeCast2(const CObject* /*selector*/,
104  void* ptr)
105  {
106  return SafeCast(static_cast<CObject*>(ptr));
107  }
108  static const TObjectType* SafeCast2(const void* /*selector*/,
109  const void* ptr)
110  {
111  // cannot check types not inherited from CObject or CTypeInfo
112  return static_cast<const TObjectType*>(ptr);
113  }
114  static TObjectType* SafeCast2(const void* /*selector*/,
115  void* ptr)
116  {
117  // cannot check types not inherited from CObject or CTypeInfo
118  return static_cast<TObjectType*>(ptr);
119  }
120 
121 public:
122  static const TObjectType* SafeCast(const void* ptr)
123  {
124  const T* selector = static_cast<const T*>(0);
125  return SafeCast2(selector, ptr);
126  }
127  static TObjectType* SafeCast(void* ptr)
128  {
129  const T* selector = static_cast<const T*>(0);
130  return SafeCast2(selector, ptr);
131  }
132 };
133 
134 /* @} */
135 
136 
138 
139 #endif /* SERIALUTIL__HPP */
CObject –.
Definition: ncbiobj.hpp:180
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
static int type
Definition: getdata.c:31
static TObjectType * SafeCast(CObject *obj)
Definition: serialutil.hpp:86
static TObjectType & Get(TObjectPtr object)
Definition: serialutil.hpp:56
static const TObjectType * GetPointer(TConstObjectPtr object)
Definition: serialutil.hpp:68
void * TObjectPtr
Definition: serialdef.hpp:55
const void * TConstObjectPtr
Definition: serialdef.hpp:59
static TObjectType * SafeCast2(const void *, void *ptr)
Definition: serialutil.hpp:114
static TObjectType * SafeCast2(const CObject *, void *ptr)
Definition: serialutil.hpp:103
static const TObjectType & Get(TConstObjectPtr object)
Definition: serialutil.hpp:60
static TObjectType * SafeCast(void *ptr)
Definition: serialutil.hpp:127
static const TObjectType * SafeCast(const CObject *obj)
Definition: serialutil.hpp:81
static const TObjectType * SafeCast(const void *ptr)
Definition: serialutil.hpp:122
static const TObjectType * SafeCast2(const void *, const void *ptr)
Definition: serialutil.hpp:108
static TObjectType * GetPointer(TObjectPtr object)
Definition: serialutil.hpp:64
static const TObjectType * SafeCast2(const CObject *, const void *ptr)
Definition: serialutil.hpp:98
static const TObjectType * SafeCast(TTypeInfo type)
Definition: serialutil.hpp:76
static const TObjectType * SafeCast2(TTypeInfo, const void *ptr)
Definition: serialutil.hpp:93
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Definition: type.c:6
#define _ASSERT
Modified on Wed May 29 18:43:50 2024 by modify_doxy.py rev. 669887