<commit_before>// Copyright (c) 2013 Arista Networks, Inc.  All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

#ifndef EOS_BASE_MGR_H
#define EOS_BASE_MGR_H

#include <eos/base.h>

#include <map>
#include <set>

namespace eos {

/*
 * This class is not part of the public API of the SDK. You should NOT use it
 * directly. It is subclassed by the different managers that have handlers.
 *
 * Manages handler. The handler can register themselves to receive notifications
 * following two different modes:
 * - "watch all": the handler will receive every notifications.
 * - "key specific": the handler will only receive notifications for the keys it
 *   directly registered to.
 *
 * It is not possible for a handler to be in both modes. The "watch all" mode is
 * always favored over "key specific". Thus you have to manually deactivate the
 * "watch all" mode to enter the "key specific" mode.
 */
template <typename T, typename Key=int> // `int' is an arbitrary default for classes
class EOS_SDK_PRIVATE base_mgr {        // that don't use key-specific notifications
 public:
   void add_handler(T *handler) {
      // no specific ordering
      watchAllHandlers_.insert(handler);

      // The handler is now registered in the "watchAll" set. The key-specific
      // registration needs to be cleaned to avoid calling the handler twice.
      for (auto it = keySpecificHandlers_.begin(); it != keySpecificHandlers_.end();
            ++it) {
         it->second.erase(handler);
         if (it->second.empty()) {
            keySpecificHandlers_.erase(it);
         }
      }
   }

   // We don't allow a handler to be registered both as generic and key-specific.
   // We always favor generic over key-specific.
   void add_handler(Key key, T *handler) {
      if (!watchAllHandlers_.count(handler)) {
         keySpecificHandlers_[key].insert(handler);
      }
   }

   void remove_handler(T *handler) {
      watchAllHandlers_.erase(handler);
   }

   void remove_handler(Key key, T *handler) {
      auto entry = keySpecificHandlers_.find(key);
      if (entry != keySpecificHandlers_.end()) {
         entry->second.erase(handler);
         if (entry->second.empty()) {
            keySpecificHandlers_.erase(entry);
         }
      }
   }

 protected:
   // Run the function f for each registered handler, with no specific ordering
   template <typename Func> void handler_foreach(Func f) {
      for (auto i = watchAllHandlers_.begin(); i != watchAllHandlers_.end(); ++i) {
         f(*i);
      }
   }

   // Run the function f for each registered handler on the given key, with no
   // specific ordering
   template <typename Func> void handler_foreach(Key key, Func f) {
      for (auto i = watchAllHandlers_.begin(); i != watchAllHandlers_.end(); ++i) {
         f(*i);
      }

      // Since we don't allow a handler to be both key-specific and generic, we
      // don't have to check if F was already executed for a given handler.
      auto entry = keySpecificHandlers_.find(key);
      if (entry != keySpecificHandlers_.end()) {
         for (auto i = entry->second.begin(); i != entry->second.end(); ++i) {
            if (!watchAllHandlers_.count(*i)) {
               f(*i);
            }
         }
      }
   }

 private:
   std::set<T *> watchAllHandlers_;
   std::map<Key, std::set<T *> > keySpecificHandlers_;
};

}

#endif // EOS_BASE_MGR_H
<commit_msg>@1765223 base_mgr is not part of the public API of the SDK, changing its default view to protected.<commit_after>// Copyright (c) 2013 Arista Networks, Inc.  All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.

#ifndef EOS_BASE_MGR_H
#define EOS_BASE_MGR_H

#include <eos/base.h>

#include <map>
#include <set>

namespace eos {

/*
 * This class is not part of the public API of the SDK. You should NOT use it
 * directly. It is subclassed by the different managers that have handlers.
 *
 * Manages handler. The handler can register themselves to receive notifications
 * following two different modes:
 * - "watch all": the handler will receive every notifications.
 * - "key specific": the handler will only receive notifications for the keys it
 *   directly registered to.
 *
 * It is not possible for a handler to be in both modes. The "watch all" mode is
 * always favored over "key specific". Thus you have to manually deactivate the
 * "watch all" mode to enter the "key specific" mode.
 */
template <typename T, typename Key=int> // `int' is an arbitrary default for classes
class EOS_SDK_PRIVATE base_mgr {        // that don't use key-specific notifications
 protected:
   void add_handler(T *handler) {
      // no specific ordering
      watchAllHandlers_.insert(handler);

      // The handler is now registered in the "watchAll" set. The key-specific
      // registration needs to be cleaned to avoid calling the handler twice.
      for (auto it = keySpecificHandlers_.begin(); it != keySpecificHandlers_.end();
            ++it) {
         it->second.erase(handler);
         if (it->second.empty()) {
            keySpecificHandlers_.erase(it);
         }
      }
   }

   // We don't allow a handler to be registered both as generic and key-specific.
   // We always favor generic over key-specific.
   void add_handler(Key key, T *handler) {
      if (!watchAllHandlers_.count(handler)) {
         keySpecificHandlers_[key].insert(handler);
      }
   }

   void remove_handler(T *handler) {
      watchAllHandlers_.erase(handler);
   }

   void remove_handler(Key key, T *handler) {
      auto entry = keySpecificHandlers_.find(key);
      if (entry != keySpecificHandlers_.end()) {
         entry->second.erase(handler);
         if (entry->second.empty()) {
            keySpecificHandlers_.erase(entry);
         }
      }
   }

   // Run the function f for each registered handler, with no specific ordering
   template <typename Func> void handler_foreach(Func f) {
      for (auto i = watchAllHandlers_.begin(); i != watchAllHandlers_.end(); ++i) {
         f(*i);
      }
   }

   // Run the function f for each registered handler on the given key, with no
   // specific ordering
   template <typename Func> void handler_foreach(Key key, Func f) {
      for (auto i = watchAllHandlers_.begin(); i != watchAllHandlers_.end(); ++i) {
         f(*i);
      }

      // Since we don't allow a handler to be both key-specific and generic, we
      // don't have to check if F was already executed for a given handler.
      auto entry = keySpecificHandlers_.find(key);
      if (entry != keySpecificHandlers_.end()) {
         for (auto i = entry->second.begin(); i != entry->second.end(); ++i) {
            if (!watchAllHandlers_.count(*i)) {
               f(*i);
            }
         }
      }
   }

 private:
   std::set<T *> watchAllHandlers_;
   std::map<Key, std::set<T *> > keySpecificHandlers_;
};

}

#endif // EOS_BASE_MGR_H

{'fraction_non_alphanumeric': 0.1054919576872917, 'fraction_numerical': 0.00217359802927112, 'mean_word_length': 2.740921409214092, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3984855', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1951, 'n_words': 710}