Class Registry

Synopsis

#include <src/easylogging++.h>

template <typename T_Ptr, typename T_Key = const char*>
class Registry : public AbstractRegistry<T_Ptr, std::unordered_map<T_Key, T_Ptr*>>

Description

A pointer registry mechanism to manage memory and provide search functionalities. (non-predicate version)

@detail NOTE: This is thread-unsafe implementation (although it contains lock function, it does not use these functions) of AbstractRegistry<T_Ptr, Container>. Any implementation of this class should be explicitly (by using lock functions)

Inheritance

Ancestors: AbstractRegistry

Methods

Registry overloadCopy constructor that is useful for base classes. Try to avoid this constructor, use move constructor.
~Registry
getGets pointer from repository. If none found, nullptr is returned.
operator=Assignment operator that unregisters all the existing registries and deeply copies each of repo element.
registerNewRegisters new registry to repository.
unregisterUnregisters single entry mapped to specified unique key.
unregisterAllUnregisters all the pointers from current repository.

Source

Lines 1373-1443 in src/easylogging++.h.

template <typename T_Ptr, typename T_Key = const char*>
class Registry : public AbstractRegistry<T_Ptr, std::unordered_map<T_Key, T_Ptr*>> {
 public:
  typedef typename Registry<T_Ptr, T_Key>::iterator iterator;
  typedef typename Registry<T_Ptr, T_Key>::const_iterator const_iterator;

  Registry(void) {}

  /// @brief Copy constructor that is useful for base classes. Try to avoid this constructor, use move constructor.
  Registry(const Registry& sr) : AbstractRegistry<T_Ptr, std::vector<T_Ptr*>>() {
    if (this == &sr) {
      return;
    }
    this->reinitDeepCopy(sr);
  }

  /// @brief Assignment operator that unregisters all the existing registries and deeply copies each of repo element
  /// @see unregisterAll()
  /// @see deepCopy(const AbstractRegistry&)
  Registry& operator=(const Registry& sr) {
    if (this == &sr) {
      return *this;
    }
    this->reinitDeepCopy(sr);
    return *this;
  }

  virtual ~Registry(void) {
    unregisterAll();
  }

 protected:
  virtual void unregisterAll(void) ELPP_FINAL {
    if (!this->empty()) {
      for (auto&& curr : this->list()) {
        base::utils::safeDelete(curr.second);
      }
      this->list().clear();
    }
  }

/// @brief Registers new registry to repository.
  virtual void registerNew(const T_Key& uniqKey, T_Ptr* ptr) ELPP_FINAL {
    unregister(uniqKey);
    this->list().insert(std::make_pair(uniqKey, ptr));
  }

/// @brief Unregisters single entry mapped to specified unique key
  void unregister(const T_Key& uniqKey) {
    T_Ptr* existing = get(uniqKey);
    if (existing != nullptr) {
      this->list().erase(uniqKey);
      base::utils::safeDelete(existing);
    }
  }

/// @brief Gets pointer from repository. If none found, nullptr is returned.
  T_Ptr* get(const T_Key& uniqKey) {
    iterator it = this->list().find(uniqKey);
    return it == this->list().end()
           ? nullptr
           : it->second;
  }

 private:
  virtual void deepCopy(const AbstractRegistry<T_Ptr, std::unordered_map<T_Key, T_Ptr*>>& sr) ELPP_FINAL {
    for (const_iterator it = sr.cbegin(); it != sr.cend(); ++it) {
      registerNew(it->first, new T_Ptr(*it->second));
    }
  }
};





Add Discussion

Log in to comment