【问题标题】:c++ serializationC++ boost 库序列化
【发布时间】:2022-12-02 21:49:35
【问题描述】:

年轻的 c++ 开发人员,我对项目的网络缓存很感兴趣。这几天我一直在尝试实现一个网络缓存,通过学习文章和教程,我写了一个没有库的原型。我的问题真的是我发现了 Boost 库。我正在尝试在我的网络缓存中实现它。专门实现这个:

https://www.boost.org/doc/libs/1_66_0/libs/multi_index/doc/examples.html#example9 "这个数据结构是用 multi_index_container 通过组合一个序列索引和一个 hashed_unique 类型的索引来实现的。"

目前我的课程看起来像这样,通过单元测试,它可以工作,但我的目标是尽可能精确地实施有问题的 lib Boost,尤其是多索引容器。

我试图改变项目的类型但它没有用,就像这样:

typedef multi_index_container<items,indexed_by<sequenced<>,hashed_unique<identity<Item> >>> item_list;

我收到一条消息:非静态数据成员“_items”的使用无效。这种错误我能理解,但我没有指定它,因为它是让我担心的全局实现

如果你发现除了我的精确问题之外还有其他错误,我也是一个接受者。

 template<typename Tkey, typename Tval>
 class CacheWeb{

  private:
   unsigned int capacity;
   std::list<std::pair<Tkey, Tval>> items;
   std::unordered_map<key, typename std::list<std::pair<Tkey, Tval>>::iterator> lookup;

   CacheWeb(const CacheWeb&) = delete;
   CacheWeb& operator=(const CacheWeb&) = delete;

   int capacityOut(){
      if( capacity == 0 || lookup.size() < capacity ) {
        return 0;
      }
      int cnt = 0;
      while(lookup.size() > capacity) {
        lookup.erase(items.back().first);
        items.pop_back();
        ++cnt;
      }
      return cnt;      
   };

  public:
   CacheWeb(int icapacity) : capacity(icapacity){};
   virtual ~CacheWeb() = default;
   

    int size(){
      return lookup.size();
    };
    bool empty(){
    return lookup.empty(); 
    };
    void clear(){
      lookup.clear(); 
      items.clear();
    };

    bool contains(const Tkey& key){
      return lookup.find(key) != lookup.end(); 
    };

    void remove(const Tkey& key){
      auto it = lookup.find(key);
      items.erase(it->second);
      lookup.erase(it); 
    };

    void put(const Tkey& key, const Tval& val){
      auto it = lookup.find(key);
      if( it != lookup.end() ) {
        it->second->second = val;
        items.splice(items.begin(), items, it->second);
        return;
      }
      items.emplace_front(key, val);
      lookup[key] = items.begin();
      capacityOut();
    };

    std::list<std::pair<Tkey, Tval>>getItems(){
      return items;
    };

    const VAL_T& get(const Tkey& key){
    
      const auto it = lookup.find(key);
      if( it == lookup.end() ) {
        throw std::invalid_argument("Key does not exist");
      }
      items.splice(items.begin(), items, it->second);
      return it->second->second;
    };

  };
}

感谢您阅读我的问题,如果您有帮助,再次感谢您。

【问题讨论】:

  • 请比“它不起作用”更具体是否有错误?那是什么错误?
  • 我试图实现这个:typedef multi_index_container<items,indexed_by<sequenced<>,hashed_unique<identity<Item> >>> item_list;但主要是我收到一条消息:非静态数据成员“_items”的使用无效。这种错误我可以理解但我不想指定它因为它是全局实现让我担心
  • edit 澄清问题,cmets 不支持足够的格式
  • 我也编辑了! :)
  • 请谈谈您运行代码时出了什么问题,以及您的期望是什么。更具体地说:对于您看到的错误尽可能准确。

标签: c++ caching serialization boost boost-multi-index


【解决方案1】:

像这样的事情可以做:

Live Coliru Demo

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/key.hpp>

template<typename Tkey, typename Tval>
class CacheWeb{
  private:
  using value_type = std::pair<Tkey, Tval>;
  
  unsigned int capacity;
  boost::multi_index_container<
    value_type,
    boost::multi_index::indexed_by<
      boost::multi_index::sequenced<>,
      boost::multi_index::hashed_unique<boost::multi_index::key<&value_type::first>>
    >
  > container; 
  
  CacheWeb(const CacheWeb&) = delete;
  CacheWeb& operator=(const CacheWeb&) = delete;

  int capacityOut(){
      if( capacity == 0 || container.size() < capacity ) {
        return 0;
      }
      int cnt = 0;
      while(container.size() > capacity) {
        container.pop_back();
        ++cnt;
      }
      return cnt;      
  };

  public:
  CacheWeb(int icapacity) : capacity(icapacity){};
  virtual ~CacheWeb() = default;
   

  int size(){
    return container.size();
  };
  bool empty(){
    return container.empty(); 
  };
  void clear(){
    container.clear(); 
  };

  bool contains(const Tkey& key){
    const auto& lookup = container.template get<1>();
    return lookup.find(key) != container.template get<1>().end(); 
  };

  void remove(const Tkey& key){
    container.erase(key);
  };

  void put(const Tkey& key, const Tval& val){
    auto& lookup = container.template get<1>();
    auto it = lookup.find(key);
    if( it != lookup.end() ) {
      lookup.modify(it,[&](value_type& x){ x.second = val; });
    }
    else{
      it=lookup.emplace(key, val).first;
    }
    container.relocate(container.begin(),container.template project<0>(it));
    capacityOut();
  };

  std::list<std::pair<Tkey, Tval>>getItems(){
    return {container.begin(), container.end()};
  };

  const Tval& get(const Tkey& key){
    const auto& lookup = container.template get<1>();
    const auto it = lookup.find(key);
    if( it == lookup.end() ) {
      throw std::invalid_argument("Key does not exist");
    }
    return it->second;
  }
};
  
#include <iostream>

int main()
{
  CacheWeb<int,int> c(10);
  for(int i=0;i<11;++i)c.put(i,i);
  
  for(const auto& x:c.getItems()){
    std::cout<<"("<<x.first<<","<<x.second<<")";
  }
  std::cout<<"
";
  
  for(int i=1;i<11;++i){
    std::cout<<i<<"->"<<c.get(i)<<" ";
  }
  std::cout<<"
";
}

输出

(10,10)(9,9)(8,8)(7,7)(6,6)(5,5)(4,4)(3,3)(2,2)(1,1)
1->1 2->2 3->3 4->4 5->5 6->6 7->7 8->8 9->9 10->10 

【讨论】:

  • 您好,感谢您的回复!在测试代​​码后,我将不得不分享与两个错误相关的两个问题:“类模板‘sequenced’的模板参数太少”和“命名空间‘boost::multi_index’clang(no_member) 中没有名为‘key’的成员”。对于第一个错误,我认为有必要添加参数 Tkey ?对于查看 Boost 文档的第二个错误,我没有找到任何关于“key”的信息。 2 另一个问题,为什么 get<1>(); ?我试图从 Boost 文档中找到答案,但同样没有找到任何东西,_container 之后的 .template,我没有找到对此的解释。
  • 请发布一个额外的问题,包括一些显示问题的合理完整的代码 sn-p。
  • 你好 !抱歉,是我的错!我在下面提出了另一个问题。
【解决方案2】:

不幸的是,我的以下问题已经消失,但幸运的是我能够更好地理解我的错误和疑虑!我只是回来找你解释两个概念。

第一个:.template

从我发现的内容来看,我的容器上指定的模板关键字正在使用模板,如果我错了请纠正我。

来源:Where and why do I have to put the "template" and "typename" keywords?

第二个:项目<0>(它)

在lib中查找定义,我特别看到它需要一个迭代器作为参数,但我不理解 project<0> (与 get<1> 相同)

ps:我找到了一些像这样的信息 https://theboostcpplibraries.com/boost.variant 并且也在stackoverflow中发布,但我有点困惑

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2011-12-28
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多