UniSet  2.24.2
UHelpers.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 #ifndef UHelpers_H_
18 #define UHelpers_H_
19 // --------------------------------------------------------------------------
20 #include "UniSetTypes.h"
21 #include "Exceptions.h"
22 #include "Configuration.h"
23 // --------------------------------------------------------------------------
24 namespace uniset
25 {
26  // Шаблон для "универсальной инициализации объекта (процесса)".
27  // Использование:
28  // auto m = make_object<MyClass>("ObjectId","secname",...);
29  // --
30  // Где MyClass должен содержать конструктор MyClass( const ObjetctId id, xmlNode* cnode, ...any args.. );
31  // ---------------
32  // Если secname задан, то ищется: <secname name="ObjectId" ....>
33  // Если secname не задан, то: <idname name="idname" ...>
34  //----------------
35  template<typename T, typename... _Args>
36  std::shared_ptr<T> make_object( const std::string& idname, const std::string& secname, _Args&& ... __args )
37  {
38  auto conf = uniset::uniset_conf();
39  uniset::ObjectId id = conf->getObjectID(idname);
40 
41  if( id == uniset::DefaultObjectId )
42  throw uniset::SystemError("(make_object<" + std::string(typeid(T).name()) + ">): Not found ID for '" + idname + "'");
43 
44  auto xml = conf->getConfXML();
45  std::string s( (secname.empty() ? idname : secname) );
46  xmlNode* cnode = conf->findNode(xml->getFirstNode(), s, idname);
47 
48  if( cnode == 0 )
49  throw uniset::SystemError("(make_object<" + std::string(typeid(T).name()) + ">): Not found xmlnode <" + s + " name='" + idname + "' ... >");
50 
51  std::shared_ptr<T> obj = std::make_shared<T>(id, cnode, std::forward<_Args>(__args)...);
52 
53  if (obj == nullptr)
54  throw uniset::SystemError("(make_object<T> == nullptr" + std::string(typeid(T).name()));
55 
56  return obj;
57  }
58  // -----------------------------------------------------------------------------
59  // версия с указанием начального xml-узла, с которого ведётся поиск xmlNode
60  // а ID берётся из поля name="" у найденного xmlnode.
61  template<typename T, typename... _Args>
62  std::shared_ptr<T> make_object_x( xmlNode* root, const std::string& secname, _Args&& ... __args )
63  {
64  auto conf = uniset::uniset_conf();
65  auto xml = conf->getConfXML();
66  xmlNode* cnode = conf->findNode(root, secname, "");
67 
68  if( cnode == 0 )
69  throw uniset::SystemError("(make_object_x<" + std::string(typeid(T).name()) + ">): Not found xmlnode <" + secname + " ... >");
70 
71  std::string idname = conf->getProp(cnode, "name");
72  uniset::ObjectId id = conf->getObjectID(idname);
73 
74  if( id == uniset::DefaultObjectId )
75  throw uniset::SystemError("(make_object_x<" + std::string(typeid(T).name()) + ">): Not found ID for '" + idname + "'");
76 
77  return std::make_shared<T>(id, cnode, std::forward<_Args>(__args)...);
78 
79  }
80  // -----------------------------------------------------------------------------
81  // Просто обёртка для удобства вывода сообщений об ошибке в лог "объекта"..
82  // "по задумке" позволяет не загромождать код..
83  // T - тип создаваемого объекта
84  // M - (master) - класс который создаёт объект (подразумевается, что он UniSetManager)
85  // Использование
86  // auto m = make_child_object<MyClass,MyMasterClass>(master, "ObjectId","secname",...);
87  template<typename T, typename M, typename... _Args>
88  std::shared_ptr<T> make_child_object( M* m, const std::string& idname, const std::string& secname, _Args&& ... __args )
89  {
90  try
91  {
92  m->log()->info() << m->getName() << "(" << __FUNCTION__ << "): " << "create " << idname << "..." << std::endl;
93  auto o = uniset::make_object<T>(idname, secname, std::forward<_Args>(__args)...);
94  m->add(o);
95  m->logAgregator()->add(o->logAgregator());
96  return o;
97  }
98  catch( const uniset::Exception& ex )
99  {
100  m->log()->crit() << m->getName() << "(" << __FUNCTION__ << "): " << "(create " << idname << "): " << ex << std::endl;
101  throw;
102  }
103  }
104  // -----------------------------------------------------------------------------
105  // Версия использующая make_object_x<>
106  template<typename T, typename M, typename... _Args>
107  std::shared_ptr<T> make_child_object_x( M* m, xmlNode* root, const std::string& secname, _Args&& ... __args )
108  {
109  try
110  {
111  auto o = uniset::make_object_x<T>(root, secname, std::forward<_Args>(__args)...);
112  m->add(o);
113  m->logAgregator()->add(o->logAgregator());
114  return o;
115  }
116  catch( const uniset::Exception& ex )
117  {
118  m->log()->crit() << m->getName() << "(" << __FUNCTION__ << "): " << "(create " << std::string(typeid(T).name()) << "): " << ex << std::endl;
119  throw;
120  }
121  }
122  // -----------------------------------------------------------------------------------------
123 } // endof namespace uniset
124 // -----------------------------------------------------------------------------------------
125 #endif // UHelpers_H_
Definition: Exceptions.h:46
Definition: Exceptions.h:90
Definition: CommonEventLoop.h:15
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
std::shared_ptr< Configuration > uniset_conf() noexcept
Definition: Configuration.cc:90
long ObjectId
Definition: UniSetTypes_i.idl:30