ADTF  3.18.2
class_factory.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <stdexcept>
10 
11 namespace adtf
12 {
13 namespace ucom
14 {
15 namespace ant
16 {
17 
24 template <typename ...Classes>
25 class class_factory : public catwo::object<IClassFactory>
26 {
27  private:
28  template<typename ...InnerClasses>
29  struct dispatch
30  {
31  static tResult CreateInstance(const char* /* strCID */,
32  iobject_ptr<IObject>& /* oObject */,
33  const char* /* strNameOfObject */)
34  {
35  RETURN_ERROR(ERR_NO_CLASS);
36  }
37 
38  static void AddClassInfo(iobject_enum<const IClassInfo>& /* lstOfClasses */)
39  {
40  }
41  };
42 
43  template<typename Class, typename ...InnerClasses>
44  struct dispatch<Class, InnerClasses...>
45  {
46  static tResult CreateInstance(const char* strCID,
47  iobject_ptr<IObject>& oObject,
48  const tChar* strNameOfObject = "")
49  {
50  if (adtf_util::cString(strCID).IsEqual(get_class_id<Class>()))
51  {
52  try
53  {
54  object_ptr<IObject> pCreatedObject(make_object_ptr<Class>());
55  if (!pCreatedObject)
56  {
57  RETURN_ERROR(ERR_MEMORY);
58  }
59  oObject.Reset(pCreatedObject);
60  if (oObject.Get() == nullptr)
61  {
62  RETURN_ERROR(ERR_INVALID_INTERFACE);
63  }
64 
66  }
67  catch (const tResult& oError)
68  {
69  return oError;
70  }
71  catch (const std::exception& oError)
72  {
73  RETURN_ERROR_DESC(ERR_FAILED, "%s", oError.what());
74  }
75  catch (...)
76  {
78  RETURN_ERROR(ERR_UNKNOWN);
79  }
80  }
81  return dispatch<InnerClasses...>::CreateInstance(strCID, oObject, strNameOfObject);
82  }
83 
84  static void AddClassInfo(iobject_enum<const IClassInfo>& lstOfClasses)
85  {
86  //use copy constructor
87  object_ptr<const IClassInfo> pInfo = get_class_info<Class>();
88  lstOfClasses.PushObject(pInfo);
90  }
91  };
92 
93  private:
94  class_factory(const class_factory&) = delete;
95  class_factory(class_factory&&) = delete;
96  class_factory& operator=(const class_factory&) = delete;
97  class_factory& operator=(class_factory&&) = delete;
98 
99  public:
104  {
105  }
106 
110  virtual ~class_factory()
111  {
112  }
113 
114 
115  public: // implements IClassFactory
125  virtual tResult CreateInstance(const char* strCID,
126  iobject_ptr<IObject>& oObject,
127  const tChar* strNameOfObject = "") const
128  {
129  RETURN_IF_POINTER_NULL(strCID);
130 
131  object_ptr<IObject> pCreatedObject = nullptr;
132 
133  adtf_util::cString strClassToCreate(strCID);
134 
135  if (strClassToCreate.IsEmpty())
136  {
137  RETURN_ERROR_DESC(ERR_INVALID_ARG, "CID is empty");
138  }
139 
140  return dispatch<Classes...>::CreateInstance(strCID, oObject, strNameOfObject);
141  }
142 
150  {
153  }
154 };
155 
156 }//ns ant
157 
159 using ant::class_factory;
160 
161 }//ns ucom
162 }//ns adtf
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
#define RETURN_ERROR_DESC(_code,...)
Same as RETURN_ERROR(_error) using a printf like parameter list for detailed error description.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
#define RETURN_IF_POINTER_NULL(_ptr)
Return ERR_POINTER if _ptr is nullptr, which requires the calling function's return type to be tResul...
The class class_factory provides a template based implementation of the IClassFactory interface,...
Definition: class_factory.h:26
virtual tResult CreateInstance(const char *strCID, iobject_ptr< IObject > &oObject, const tChar *strNameOfObject="") const
Creates a new instance.
class_factory()
The Constructor create the class factory for exactly one object.
tResult GetClasses(iobject_enum< const IClassInfo > &lstOfClasses) const
Enumerates all classes supported by the class factory and pushes it to lstOfClasses This method shoul...
virtual ~class_factory()
standard DTOR for the class factory.
Interface definition for a container of objects.
Definition: object_list.h:22
virtual tResult PushObject(const value_type &oObject)=0
Pushes a object of value_type to the container.
virtual tResult Reset(const iobject_ptr< T > &i_oOther)=0
Reset this object_ptr<> with the content of another iobject_ptr<>
virtual T * Get() const =0
Get raw pointer to shared object.
Base object pointer to realize binary compatible reference counting in interface methods.
Object pointer implementation used for reference counting on objects of type IObject.
Definition: object_ptr.h:163
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:379
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
Namespace for entire ADTF SDK.