C++ 容器,它将存储在其中一定时间段内推入的任何元素

C++ 容器,它将存储在其中一定时间段内推入的任何元素,c++,boost,C++,Boost,我需要有一个容器,将存储任何元素推它10分钟。我怎么做?我是否应该使用类似于boost::deadline\u timer的东西来达到这样的目的 我已尝试编写以下代码: #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/date_time.hpp> #include <boost/function.hpp> #include <boost/noncopyab

我需要有一个容器,将存储任何元素推它10分钟。我怎么做?我是否应该使用类似于
boost::deadline\u timer
的东西来达到这样的目的

我已尝试编写以下代码:

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/thread.hpp>

#include <map>
#include <utility>

namespace koicxx {

template <typename T>
class temp_storage : private boost::noncopyable
{
  typedef boost::shared_ptr<boost::asio::deadline_timer>                    shared_timer_t;
  typedef std::map<T, shared_timer_t>                                       timer_map_t;
  typedef std::pair<T, shared_timer_t>                                      timer_pair_t;
  typedef boost::function<void(const T&, const boost::system::error_code&)> callback_t;

public:
  temp_storage(boost::asio::io_service& io_service) :
    _io_service(io_service) {}

  bool add(const T& element, const boost::asio::deadline_timer::duration_type& timeout, callback_t callback = callback_t())
  {
    boost::lock_guard<boost::mutex> lock(_sync);

    const std::pair<timer_map_t::iterator, bool>& res =
      _internal_storage.insert(
        timer_pair_t(
          element
          , shared_timer_t(new boost::asio::deadline_timer(_io_service, timeout))
        ));

    if (!res.second)
    {
        return false;
    }

    const timer_map_t::iterator& itr = res.first;

    if (callback)
    {
      itr->second->async_wait(
        boost::bind(
          callback
          , itr->first
          , boost::asio::placeholders::error
        ));
    }

    itr->second->async_wait(
      boost::bind(
        &temp_storage::remove_callback
        , this
        , itr->first
        , boost::asio::placeholders::error
      ));

    return true;
  }

  bool remove(const T& element)
  {
    boost::lock_guard<boost::mutex> lock(_sync);

    const timer_map_t::iterator& itr = _internal_storage.find(element);
    if (itr == _internal_storage.end())
    {
        return false;
    }
    itr->second->cancel();

    _internal_storage.erase(itr);

    return true;
  }

  bool contains(const T& element)
  {
    boost::lock_guard<boost::mutex> lock(_sync);

    return _internal_storage.find(element) != _internal_storage.end();
  }

  void clear()
  {
    boost::lock_guard<boost::mutex> lock(_sync);

    for (timer_map_t::value_type& i : _internal_storage)
    {
        i.second->cancel();
    }

    _internal_storage.clear();
  }

private:
  void remove_callback(const T& element, const boost::system::error_code& e)
  {
    if (e == boost::asio::error::operation_aborted)
    {
        return;
    }
    remove(element);
  }

  boost::asio::io_service&  _io_service;
  timer_map_t               _internal_storage;
  boost::mutex              _sync;
};

} // namespace koicxx

int main()
{
  boost::asio::io_service io_service;
  io_service.run();
  koicxx::temp_storage<int> some_storage(io_service);
  some_storage.add(0, boost::posix_time::seconds(2));
  some_storage.add(1, boost::posix_time::seconds(3));
  some_storage.add(2, boost::posix_time::seconds(5));

  while (true)
  {
    if (some_storage.contains(0))
    {
      std::cout << 0 << ' ';
    }
    if (some_storage.contains(1))
    {
      std::cout << 1 << ' ';
    }
    if (some_storage.contains(2))
    {
      std::cout << 2 << ' ';
    }
    std::cout << '\n';

    boost::this_thread::sleep_for(boost::chrono::seconds(1));
  }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间koicxx{
模板
类临时存储:私有boost::不可复制
{
typedef boost::shared_ptr shared_timer_t;
typedef std::map timer\u map\t;
typedef std::对定时器对;
typedef boost::函数回调\u t;
公众:
临时存储(boost::asio::io_服务和io_服务):
_io_服务(io_服务){}
bool add(const T&element,const boost::asio::deadline\u timer::duration\u type&timeout,callback\u T callback=callback\u T())
{
boost::lock\u guard lock(\u sync);
const std::pair&res=
_内部存储.insert(
定时器对(
要素
,共享计时器(新的boost::asio::deadline\u计时器(\u io\u服务,超时))
));
如果(!res.second)
{
返回false;
}
常量计时器映射:迭代器&itr=res.first;
如果(回调)
{
itr->second->async\u wait(
boost::bind(
回拨
,itr->first
,boost::asio::占位符::错误
));
}
itr->second->async\u wait(
boost::bind(
&临时存储::删除回调
这
,itr->first
,boost::asio::占位符::错误
));
返回true;
}
bool移除(常量T和元素)
{
boost::lock\u guard lock(\u sync);
const timer\u map\u t::iterator&itr=\u internal\u storage.find(元素);
如果(itr==\u内部存储.end())
{
返回false;
}
itr->second->cancel();
_内部存储器擦除(itr);
返回true;
}
布尔包含(常量T和元素)
{
boost::lock\u guard lock(\u sync);
return _internal_storage.find(element)!=_internal_storage.end();
}
无效清除()
{
boost::lock\u guard lock(\u sync);
用于(计时器映射t::值类型&i:\内部存储)
{
i、 第二->取消();
}
_内部存储。清除();
}
私人:
void remove_回调(常量T&element,常量boost::system::error_code&e)
{
如果(e==boost::asio::error::operation\u中止)
{
返回;
}
移除(元素);
}
boost::asio::io_服务和io_服务;
定时器映射内部存储器;
boost::互斥同步;
};
}//名称空间koicxx
int main()
{
boost::asio::io_服务io_服务;
io_service.run();
koicxx::临时存储部分存储(io服务);
添加(0,boost::posix_time::seconds(2));
添加(1,boost::posix_time::seconds(3));
添加(2,boost::posix_time::seconds(5));
while(true)
{
if(某些存储包含(0))
{

也许这可以帮助你

#include <vector>
#include <memory>
#include <chrono>
#include <algorithm>
#include <string>

using timepoint = std::chrono::time_point<std::chrono::system_clock>;

//type erasure code
struct Holder{
    virtual timepoint getCreationTime() const = 0;
    virtual ~Holder(){}
};

template <class T>
struct HolderHelper : Holder{
    HolderHelper(std::unique_ptr<T> &&up, timepoint t = timepoint::clock::now()) :
        object(std::move(up)), creationtime(t){}
    std::unique_ptr<T> object;
    timepoint creationtime;
    timepoint getCreationTime() const final override{
        return creationtime;
    }
};

//convenience wrapper
//it can be made more convenient if you limit yourself to movable objects
template <class T>
std::unique_ptr<Holder> make_timed_holder(std::unique_ptr<T> &&up){
    return std::unique_ptr<Holder>(new HolderHelper<T>(std::move(up)));
}

template <class T>
std::unique_ptr<Holder> make_timed_holder(std::unique_ptr<T> &&up, timepoint t){
    return std::unique_ptr<Holder>(new HolderHelper<T>(std::move(up), t));
}

//usage
int main(){
    //using a vector as a container, can be any other container
    std::vector<std::unique_ptr<Holder>> v;

    //inserting different objects
    v.push_back(make_timed_holder(std::make_unique<int>(42)));
    v.push_back(make_timed_holder(std::make_unique<std::string>("Hello")));
    v.push_back(make_timed_holder(std::make_unique<std::vector<std::vector<std::string>>>()));

    //removing all objects that have run out of time
    /*  for a sequence container the outdated objects are always in the front,
        so you can make the search faster
        For a set you can use the creation time as a key and make it search quicker that way
    */
    auto now = timepoint::clock::now();
    v.erase(std::remove_if(begin(v), end(v), [&](std::unique_ptr<Holder> &u){
        return u->getCreationTime() + std::chrono::minutes(10) < now;
    }), end(v));
}
#包括
#包括
#包括

#包括。

这有帮助吗?->我能想出一些解决方案。你尝试了什么,什么不起作用?@William Andrew Montgomery我已经更新了帖子。你能帮我吗?