DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Libraries.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
8 #pragma once
9 
10 #include "stdafx.h"
11 #include "HardwareAdapter.h"
12 #include "Instrument.h"
13 #include "Module.h"
14 
15 namespace DynExp
16 {
23  template <typename ObjectTypeBasePtr>
24  using LibraryObjectFactoryPtrType = ObjectTypeBasePtr(*)(const std::thread::id, ParamsBasePtrType&&);
25 
31 
35  using LibraryStringType = const char*;
36 
43 
50  template <typename ObjectTypeBasePtr, LibraryObjectFactoryPtrType<ObjectTypeBasePtr>...>
52 
58  template <LibraryConfigFactoryPtrType...>
60 
66  template <LibraryStringParamPtrType...>
68 
73  template <typename ObjectTypeBasePtr>
74  struct LibraryEntry
75  {
86  const LibraryConfigFactoryPtrType ConfigFactoryPtr,
87  const LibraryStringType Name, const LibraryStringType Category) noexcept
88  : ObjectFactoryPtr(ObjectFactoryPtr), ConfigFactoryPtr(ConfigFactoryPtr),
89  Name(Name), Category(Category)
90  {}
91 
96  };
97 
106  template <typename ObjectTypeBasePtr,
107  typename ObjFactories,
108  typename ConfFactories,
109  typename ObjNames,
110  typename ObjCategories
111  >
112  class LibraryBase;
113 
122  template <typename ObjectTypeBasePtr,
124  LibraryConfigFactoryPtrType... ConfFactories,
125  LibraryStringParamPtrType... NameFuncs,
126  LibraryStringParamPtrType... CategoryFuncs
127  >
128  class LibraryBase<
129  ObjectTypeBasePtr,
130  LibraryObjectFactoryParamList<ObjectTypeBasePtr, ObjFactories...>,
131  LibraryConfigFactoryParamList<ConfFactories...>,
132  LibraryStringParamList<NameFuncs...>,
133  LibraryStringParamList<CategoryFuncs...>
134  >
135  {
136  protected:
142  constexpr LibraryBase() : Entries{ LibraryEntry(ObjFactories, ConfFactories, NameFuncs(), CategoryFuncs())... }
143  {
144  std::sort(Entries.begin(), Entries.end(), [](const auto& a, const auto& b) constexpr {
145  using ct = std::char_traits<char>;
146 
147  // Sort by category first...
148  const auto CategoryDiff = ct::compare(a.Category, b.Category,
149  std::min(ct::length(a.Category), ct::length(b.Category)));
150 
151  // ...and by name second if categories equal.
152  if (CategoryDiff == 0)
153  return std::lexicographical_compare(a.Name, a.Name + ct::length(a.Name),
154  b.Name, b.Name + ct::length(b.Name));
155 
156  return CategoryDiff < 0;
157  });
158  }
159 
160  ~LibraryBase() = default;
161 
162  public:
167  constexpr const auto& GetLibrary() const noexcept { return Entries; }
168 
173  auto ToVector() const
174  {
175  return std::vector<LibraryEntry<ObjectTypeBasePtr>>(Entries.cbegin(), Entries.cend());
176  }
177 
178  private:
182  std::array<LibraryEntry<ObjectTypeBasePtr>, sizeof...(ObjFactories)> Entries;
183  };
184 
191  template <typename... HardwareAdapterTypes>
193  HardwareAdapterPtrType,
194  LibraryObjectFactoryParamList<HardwareAdapterPtrType, &MakeHardwareAdapter<HardwareAdapterTypes>...>,
195  LibraryConfigFactoryParamList<&MakeHardwareAdapterConfig<HardwareAdapterTypes>...>,
196  LibraryStringParamList<&HardwareAdapterTypes::Name...>,
197  LibraryStringParamList<&HardwareAdapterTypes::Category...>
198  >
199  {};
200 
204  using HardwareAdapterLibraryVectorType = std::vector<LibraryEntry<HardwareAdapterPtrType>>;
205 
212  template <typename... InstrumentTypes>
214  InstrumentPtrType,
215  LibraryObjectFactoryParamList<InstrumentPtrType, &MakeInstrument<InstrumentTypes>...>,
216  LibraryConfigFactoryParamList<&MakeInstrumentConfig<InstrumentTypes>...>,
217  LibraryStringParamList<&InstrumentTypes::Name...>,
218  LibraryStringParamList<&InstrumentTypes::Category...>
219  >
220  {};
221 
225  using InstrumentLibraryVectorType = std::vector<LibraryEntry<InstrumentPtrType>>;
226 
233  template <typename... ModuleTypes>
234  class ModuleLibrary : public LibraryBase<
235  ModulePtrType,
236  LibraryObjectFactoryParamList<ModulePtrType, &MakeModule<ModuleTypes>...>,
237  LibraryConfigFactoryParamList<&MakeModuleConfig<ModuleTypes>...>,
238  LibraryStringParamList<&ModuleTypes::Name...>,
239  LibraryStringParamList<&ModuleTypes::Category...>
240  >
241  {};
242 
246  using ModuleLibraryVectorType = std::vector<LibraryEntry<ModulePtrType>>;
247 
257  template <typename LibraryVectorT>
258  const typename LibraryVectorT::value_type& FindInLibraryVector(const LibraryVectorT& LibraryVector,
259  const std::string& Category, const std::string& Name)
260  {
261  auto Result = std::find_if(LibraryVector.cbegin(), LibraryVector.cend(),
262  [&Category, &Name](const auto& LibEntry) {
263  return Category == std::string(LibEntry.Category) && Name == std::string(LibEntry.Name);
264  });
265 
266  if (Result == LibraryVector.cend())
267  throw Util::NotFoundException("An item (" + Object::CategoryAndNameToStr(Category, Name)
268  + ") has not been found in the given library vector.");
269 
270  return *Result;
271  }
272 }
Implementation of DynExp hardware adapter objects.
Implementation of DynExp instrument objects.
Implementation of DynExp module objects.
Represents a hardware adapter library to generate instances of classes derived from DynExp::HardwareA...
Definition: Libraries.h:199
Represents an instrument library to generate instances of classes derived from DynExp::InstrumentBase...
Definition: Libraries.h:220
Represents a module library to generate instances of classes derived from DynExp::ModuleBase.
Definition: Libraries.h:241
static std::string CategoryAndNameToStr(const std::string &Category, const std::string &Name)
Builds a string from an Object's category and name to allow the user to identify an Object's type.
Definition: Object.cpp:350
Thrown when a requested ressource does not exist.
Definition: Exception.h:236
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::vector< LibraryEntry< InstrumentPtrType > > InstrumentLibraryVectorType
Alias for the vector type containing entries of a instrument library.
Definition: Libraries.h:225
std::vector< LibraryEntry< HardwareAdapterPtrType > > HardwareAdapterLibraryVectorType
Alias for the vector type containing entries of a hardware adapter library.
Definition: Libraries.h:204
const char * LibraryStringType
String type used for library entry descriptors.
Definition: Libraries.h:35
ObjectTypeBasePtr(*)(const std::thread::id, ParamsBasePtrType &&) LibraryObjectFactoryPtrType
Alias for a factory function pointer type that instantiates library entries to create DynExp objects ...
Definition: Libraries.h:24
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition: Object.h:1807
LibraryStringType(*)() LibraryStringParamPtrType
Pointer to a DynExp object's static constexpr function returning a string literal....
Definition: Libraries.h:42
std::shared_ptr< ConfiguratorBase > ConfiguratorBasePtrType
Alias for a pointer to the configurator base class ConfiguratorBase.
Definition: Object.h:1959
ConfiguratorBasePtrType(*)() LibraryConfigFactoryPtrType
Alias for a factory function pointer type that creates a configurator for a specific library entry....
Definition: Libraries.h:30
const LibraryVectorT::value_type & FindInLibraryVector(const LibraryVectorT &LibraryVector, const std::string &Category, const std::string &Name)
Finds an entry in a library vector by category and name.
Definition: Libraries.h:258
std::vector< LibraryEntry< ModulePtrType > > ModuleLibraryVectorType
Alias for the vector type containing entries of a module library.
Definition: Libraries.h:246
Represents the base class for a library containing library entries (DynExp::LibraryEntry).
Definition: Libraries.h:112
Type consisting of function pointer types returning configurators for DynExp objects....
Definition: Libraries.h:59
Type consisting of function pointer types instantiating DynExp objects. Refer to DynExp::LibraryObjec...
Definition: Libraries.h:51
Type consisting of function pointer types returning string literals. Refer to DynExp::LibraryStringPa...
Definition: Libraries.h:67
Accumulates include statements to provide a precompiled header.
Represents an entry in the library.
Definition: Libraries.h:75
constexpr LibraryEntry(const LibraryObjectFactoryPtrType< ObjectTypeBasePtr > ObjectFactoryPtr, const LibraryConfigFactoryPtrType ConfigFactoryPtr, const LibraryStringType Name, const LibraryStringType Category) noexcept
Constructs a library entry.
Definition: Libraries.h:85
LibraryConfigFactoryPtrType ConfigFactoryPtr
Refer to LibraryEntry::LibraryEntry()
Definition: Libraries.h:93
LibraryStringType Category
Refer to LibraryEntry::LibraryEntry()
Definition: Libraries.h:95
LibraryObjectFactoryPtrType< ObjectTypeBasePtr > ObjectFactoryPtr
Refer to LibraryEntry::LibraryEntry()
Definition: Libraries.h:92
LibraryStringType Name
Refer to LibraryEntry::LibraryEntry()
Definition: Libraries.h:94