UniSet  2.6.0
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 "Configuration.h"
22 // --------------------------------------------------------------------------
23 namespace uniset
24 {
25  // Шаблон для "универсальной инициализации объекта(процесса)".
26  // Использование:
27  // auto m = make_object<MyClass>("ObjectId","secname",...);
28  // --
29  // Где MyClass должен содержать конструктор MyClass( const ObjetctId id, xmlNode* cnode, ...any args.. );
30  // ---------------
31  // Если secname задан, то ищется: <secname name="ObjectId" ....>
32  // Если secname не задан, то: <idname name="idname" ...>
33  //----------------
34  template<typename T, typename... _Args>
35  std::shared_ptr<T> make_object( const std::string& idname, const std::string& secname, _Args&& ... __args )
36  {
37  auto conf = uniset::uniset_conf();
38  uniset::ObjectId id = conf->getObjectID(idname);
39 
40  if( id == uniset::DefaultObjectId )
41  throw uniset::SystemError("(make_object<" + string(typeid(T).name()) + ">): Not found ID for '" + idname + "'");
42 
43  auto xml = conf->getConfXML();
44  std::string s( (secname.empty() ? idname : secname) );
45  xmlNode* cnode = conf->findNode(xml->getFirstNode(), s, idname);
46 
47  if( cnode == 0 )
48  throw uniset::SystemError("(make_object<" + string(typeid(T).name()) + ">): Not found xmlnode <" + s + " name='" + idname + "' ... >");
49 
50  std::shared_ptr<T> obj = std::make_shared<T>(id, cnode, std::forward<_Args>(__args)...);
51 
52  if (obj == nullptr)
53  throw uniset::SystemError("(make_object<T> == nullptr" + string(typeid(T).name()));
54 
55  return obj;
56  }
57  // -----------------------------------------------------------------------------
58  // версия с указанием начального xml-узла, с которого ведётся поиск xmlNode
59  // а ID берётся из поля name="" у найденного xmlnode.
60  template<typename T, typename... _Args>
61  std::shared_ptr<T> make_object_x( xmlNode* root, const std::string& secname, _Args&& ... __args )
62  {
63  auto conf = uniset::uniset_conf();
64  auto xml = conf->getConfXML();
65  xmlNode* cnode = conf->findNode(root, secname, "");
66 
67  if( cnode == 0 )
68  throw uniset::SystemError("(make_object_x<" + string(typeid(T).name()) + ">): Not found xmlnode <" + secname + " ... >");
69 
70  string idname = conf->getProp(cnode, "name");
71  uniset::ObjectId id = conf->getObjectID(idname);
72 
73  if( id == uniset::DefaultObjectId )
74  throw uniset::SystemError("(make_object_x<" + string(typeid(T).name()) + ">): Not found ID for '" + idname + "'");
75 
76  return std::make_shared<T>(id, cnode, std::forward<_Args>(__args)...);
77 
78  }
79  // -----------------------------------------------------------------------------
80  // Просто обёртка для удобства вывода сообщений об ошибке в лог "объекта"..
81  // "по задумке" позволяет не загромаждать код..
82  // T - тип создаваемого объекта
83  // M - (master) - класс который создаёт объект (подразумевается, что он UniSetManager)
84  // Использование
85  // auto m = make_child_object<MyClass,MyMasterClass>(master, "ObjectId","secname",...);
86  template<typename T, typename M, typename... _Args>
87  std::shared_ptr<T> make_child_object( M* m, const std::string& idname, const std::string& secname, _Args&& ... __args )
88  {
89  try
90  {
91  m->log()->info() << m->getName() << "(" << __FUNCTION__ << "): " << "create " << idname << "..." << std::endl;
92  auto o = uniset::make_object<T>(idname, secname, std::forward<_Args>(__args)...);
93  m->add(o);
94  m->logAgregator()->add(o->logAgregator());
95  return std::move(o);
96  }
97  catch( const uniset::Exception& ex )
98  {
99  m->log()->crit() << m->getName() << "(" << __FUNCTION__ << "): " << "(create " << idname << "): " << ex << std::endl;
100  throw;
101  }
102  }
103  // -----------------------------------------------------------------------------
104  // Версия использующая make_object_x<>
105  template<typename T, typename M, typename... _Args>
106  std::shared_ptr<T> make_child_object_x( M* m, xmlNode* root, const std::string& secname, _Args&& ... __args )
107  {
108  try
109  {
110  auto o = uniset::make_object_x<T>(root, secname, std::forward<_Args>(__args)...);
111  m->add(o);
112  m->logAgregator()->add(o->logAgregator());
113  return std::move(o);
114  }
115  catch( const uniset::Exception& ex )
116  {
117  m->log()->crit() << m->getName() << "(" << __FUNCTION__ << "): " << "(create " << string(typeid(T).name()) << "): " << ex << std::endl;
118  throw;
119  }
120  }
121  // -----------------------------------------------------------------------------------------
122 } // endof namespace uniset
123 // -----------------------------------------------------------------------------------------
124 #endif // UHelpers_H_
Definition: CallbackTimer.h:29
Definition: Exceptions.h:44
std::shared_ptr< Configuration > uniset_conf() noexcept
Definition: Configuration.cc:89
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:56
Definition: Exceptions.h:89
long ObjectId
Definition: UniSetTypes_i.idl:30