lib:boost
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
lib:boost [2019/11/12 17:41] – Ajout de l'exécuteur root | lib:boost [2020/07/05 00:37] (Version actuelle) – [boost::asio::execution_context] : code -> gh root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ===Compilation sous Windows=== | + | ====Windows==== |
+ | ===Compilation=== | ||
<code bash> | <code bash> | ||
git clone https:// | git clone https:// | ||
Ligne 16: | Ligne 17: | ||
'' | '' | ||
- | ===Exécuteur=== | + | ====boost:: |
L' | L' | ||
* Fonctionnement entre thread, pas entre application via TCP, | * Fonctionnement entre thread, pas entre application via TCP, | ||
* Toujours en fonctionnement. En attente s'il n'a plus de travail à faire. | * Toujours en fonctionnement. En attente s'il n'a plus de travail à faire. | ||
- | Entête | + | {{gh> |
+ | |||
+ | {{gh> | ||
+ | |||
+ | {{gh> | ||
+ | ====boost:: | ||
+ | ===boost:: | ||
<code cpp> | <code cpp> | ||
- | #include <functional> | + | #include <iostream> |
- | #include <memory> | + | |
- | class PriorityScheduler | + | #include < |
- | { | + | #include <boost/range/adaptor/ |
- | | + | |
- | | + | |
- | class ExecutorType | + | |
- | { | + | |
- | | + | |
- | ExecutorType(PriorityScheduler& | + | |
- | ~ExecutorType(); | + | |
- | ExecutorType(ExecutorType&& | + | #include < |
- | ExecutorType(ExecutorType const& other) = delete; | + | |
- | ExecutorType& | + | |
- | ExecutorType& | + | |
- | // Pour boost::asio::dispatch | + | struct Data |
- | | + | { |
+ | std::string name; | ||
+ | std::string version; | ||
+ | }; | ||
- | void add(std::function<void()> f); | + | int main() |
+ | { | ||
+ | | ||
- | | + | std::cout << |
- | | + | boost:: |
- | | + | |
- | }; | + | |
+ | [](const Data& firmware) | ||
+ | { | ||
+ | return firmware.name + " / " + firmware.version; | ||
+ | } | ||
+ | ), | ||
+ | ", ") << | ||
+ | std::endl; | ||
- | | + | |
- | | + | } |
+ | </ | ||
- | ExecutorType get_executor(int pri) noexcept; | + | ====Migrer de boost vers std==== |
+ | ===Pointeur intelligent=== | ||
- | void run(); | + | Aucune différence entre : |
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
- | void stop(); | + | <WRAP center round important 60%> |
+ | '' | ||
+ | </ | ||
- | | + | * boost::ref et std::ref. |
- | class PrioritySchedulerImpl; | + | |
- | | + | |
- | }; | + | |
- | bool operator==(const PriorityScheduler:: | + | ===assign=== |
- | const PriorityScheduler:: | + | |
- | bool operator!=(const PriorityScheduler::ExecutorType& | + | Utiliser list_initializer. |
- | const PriorityScheduler::ExecutorType& | + | |
- | } | + | <code cpp> |
+ | //const std::list< | ||
+ | const std:: | ||
</ | </ | ||
- | Source : | + | ===foreach=== |
<code cpp> | <code cpp> | ||
- | #include " | + | short array_short[] = { 1, 2, 3 }; |
+ | BOOST_FOREACH( short & i, array_short ) | ||
+ | ++i; | ||
+ | </ | ||
- | # | + | <code cpp> |
- | #include < | + | short array_short[] = { 1, 2, 3 }; |
- | #include < | + | for (short & i : array_short) |
- | # | + | ++i; |
+ | </code> | ||
- | class PriorityScheduler:: | + | Et avec des containers multi valeurs |
- | : public boost::asio::execution_context | + | |
+ | <code cpp> | ||
+ | std::map< | ||
+ | int key, value; | ||
+ | BOOST_FOREACH(boost::tie(key, value), m) | ||
{ | { | ||
- | | + | // do something with key and value here |
- | void run() | + | } |
- | { | + | </code> |
- | std:: | + | |
- | for (;;) | + | |
- | { | + | |
- | condition_.wait(lock, | + | |
- | if (stopped_) return; | + | |
- | auto p(queue_.top()); | + | |
- | queue_.pop(); | + | |
- | lock.unlock(); | + | |
- | p-> | + | |
- | lock.lock(); | + | |
- | } | + | |
- | } | + | |
- | void stop() | + | <code cpp> |
- | { | + | std:: |
- | | + | int key, value; |
- | | + | // Ici, key sera en lecture seule (implicite à la structure de std::map) et value sera accessible en écriture. |
- | } | + | // En cas de présente / absence explicite de const, cela s' |
+ | for(auto & [key, value] : m) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | </ | ||
- | struct item_base | + | ===optional=== |
- | { | + | |
- | // Priorité de l' | + | |
- | int priority_; | + | |
- | // Pour trier les item ayant le même ordre. | + | |
- | std:: | + | |
- | // Fonction a exécuter dans un wrapper compatible avec | + | |
- | // boost:: | + | |
- | std:: | + | |
- | }; | + | |
- | template <class Func> | + | Il ne devrait pas y avoir de problèmes. Il faut noter que '' |
- | struct item : item_base | + | |
- | { | + | |
- | item(int pri, Func f) : function_(std::move(f)) | + | |
- | { | + | |
- | priority_ = pri; | + | |
- | execute_ = [](std::shared_ptr<item_base> | + | |
- | Func tmp(std:: | + | |
- | p.reset(); | + | |
- | tmp(); | + | |
- | }; | + | |
- | timestamp = std:: | + | |
- | } | + | |
- | Func function_; | + | '' |
- | }; | + | |
- | // Pour comparer les items et savoir quel sera la prochaine fonction à | + | ===regex=== |
- | // exécuter. | + | |
- | struct item_comp | + | |
- | { | + | |
- | bool operator()(const std:: | + | |
- | const std:: | + | |
- | { | + | |
- | if (a-> | + | |
- | return a-> | + | |
- | else | + | |
- | return a-> | + | |
- | } | + | |
- | }; | + | |
- | std::mutex mutex_; | + | Cette fonctionnalité était très peu utilisée. |
- | std:: | + | |
- | std:: | + | |
- | std:: | + | |
- | queue_; | + | |
- | std:: | + | |
- | }; | + | |
- | class PriorityScheduler::ExecutorType::ExecutorTypeImpl | + | <code cpp> |
- | { | + | boost::regex_search(name, |
- | | + | // std::regex_search ne supporte pas le 4ème paramètre. |
- | | + | std:: |
- | | + | </ |
- | { | + | |
- | } | + | |
- | ExecutorTypeImpl(ExecutorTypeImpl&& | + | Sinon, '' |
- | ExecutorTypeImpl(ExecutorTypeImpl const& other) = delete; | + | |
- | ExecutorTypeImpl& | + | |
- | ExecutorTypeImpl& | + | |
- | // Pour boost:: | + | ===variant=== |
- | PriorityScheduler& | + | |
- | // Pour boost::asio::dispatch | + | <code cpp> |
- | void on_work_started() const noexcept {} | + | boost::variant< |
+ | double d = boost::get< | ||
+ | double *d = boost:: | ||
+ | </ | ||
- | // Pour boost:: | + | devient |
- | void on_work_finished() const noexcept {} | + | |
- | // Partout Func est boost:: | + | <code cpp> |
- | | + | std:: |
- | void dispatch(Func&& | + | double d = std:: |
- | { | + | double *d = std::get_if<double>(&v); // Renvoi nullptr |
- | post(std::forward<Func>(f), a); | + | </ |
- | } | + | |
- | | + | '' |
- | PriorityScheduler& | + | |
- | int priority_; | + | |
- | template <class Func, class Alloc> | + | Attention, ''boost::variant<int, int>'' |
- | void post(Func f, const Alloc& a) const | + | |
- | { | + | |
- | // On se force à utiliser l'allocateur fournit. | + | |
- | auto p(std::allocate_shared< | + | |
- | | + | |
- | typename | + | |
- | priority_, std:: | + | |
- | // On sécurise seulement queue_. | + | |
- | { | + | |
- | std:: | + | |
- | context_.impl_-> | + | |
- | } | + | |
- | context_.impl_-> | + | |
- | } | + | |
- | // Pour boost:: | + | ===tuple=== |
- | template <class Func, class Alloc> | + | |
- | void defer(Func&& | + | |
- | { | + | |
- | post(std:: | + | |
- | } | + | |
- | }; | + | |
- | // Impl idiome | + | Non testé la migration. |
- | PriorityScheduler:: | + | |
- | : impl_(std:: | + | |
- | { | + | |
- | } | + | |
- | PriorityScheduler:: | + | ===bind=== |
- | PriorityScheduler:: | + | Les deux sont compatibles. Mais il est préférable de les remplacer par des fonctions lambda. |
- | default; | + | |
- | PriorityScheduler& | + | ===filesystem=== |
- | { | + | |
- | return impl_-> | + | |
- | } | + | |
- | void PriorityScheduler::ExecutorType:: | + | Dans les cas basiques que j'ai eu, je n'ai pas eu de différences. Mais il y a [[https:// |
- | { | + | |
- | | + | |
- | } | + | |
- | bool operator==(const PriorityScheduler::ExecutorType& | + | * '' |
- | const PriorityScheduler::ExecutorType& | + | |
- | { | + | |
- | return & | + | |
- | } | + | |
- | bool operator!=(const PriorityScheduler:: | + | <code cpp> |
- | const PriorityScheduler:: | + | #include < |
- | { | + | |
- | return &a.context() != & | + | |
- | } | + | |
- | PriorityScheduler::PriorityScheduler() | + | boost::filesystem:: |
- | : impl_(std::make_unique< | + | boost::filesystem::ifstream ifs(nextPath); |
- | { | + | </ |
- | } | + | |
- | PriorityScheduler:: | + | devient |
- | PriorityScheduler:: | + | <code cpp> |
- | int pri) noexcept | + | #include < |
- | { | + | #include < |
- | return PriorityScheduler:: | + | |
- | } | + | |
- | void PriorityScheduler::run() { impl_-> | + | std::filesystem:: |
- | + | std::ifstream ifs(nextPath.c_str()); | |
- | void PriorityScheduler::stop() { impl_-> | + | |
</ | </ | ||
- | Utilisation | + | * '' |
- | <code cpp> | + | Il devient '' |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include " | + | '' |
+ | '' | ||
- | std:: | + | * Deprecated |
- | int main() | + | '' |
- | { | + | |
- | PriorityScheduler sched; | + | |
- | PriorityScheduler:: | + | [[https://www.boost.org/ |
- | PriorityScheduler:: | + | |
- | PriorityScheduler::ExecutorType&& | + | |
- | PriorityScheduler:: | + | |
- | med.add([] { std::cout << " | + | ===thread=== |
- | low.add([] { std::cout << " | + | |
- | med.add([] { std::cout << " | + | |
- | low.add([] { std::cout << " | + | |
- | high.add([] { std::cout << " | + | |
- | high.add([] { std::cout << " | + | |
- | high.add([] { std::cout << " | + | |
- | | + | |
- | std::thread t([& | + | ===shared_mutex=== |
- | t.join(); | + | Il n'y a pas d' |
- | } | + | |
+ | Il faut alors faire une boucle où on passe son temps à faire un '' | ||
+ | |||
+ | ===scoped_array=== | ||
+ | |||
+ | Peut se remplacer sans problème par un '' | ||
+ | |||
+ | ===algorithm=== | ||
+ | * string | ||
+ | |||
+ | <code cpp> | ||
+ | std::string str; | ||
+ | return boost:: | ||
</ | </ | ||
- | Rendu : | + | devient |
- | < | + | |
- | 3 | + | < |
- | 33 | + | std::string str; |
- | 333 | + | std:: |
- | 2 | + | return std:: |
- | 22 | + | |
- | 1 | + | |
- | 11 | + | |
</ | </ | ||
+ | |||
+ | ===boost:: | ||
+ | |||
+ | '' |
lib/boost.1573576881.txt.gz · Dernière modification : de root