Boost logo

Boost Users :

Subject: [Boost-users] boost.multi_index & IPC
From: ³Âº£·å (chenhf2010_at_[hidden])
Date: 2012-04-09 08:44:10


typedef managed_shared_memory::allocator<char>::type char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>shm_string;


struct quote
{
public:
        int market;
        int kind;
        shm_string seccode;
        shm_string secname;
        float price;
        float zf;


        quote(int market_, int kind_, const char* seccode_, const char_allocator& a, const char* secname_, const char_allocator& b, float price_, float zf_):
                market(market_),kind(kind_), seccode(seccode_, a), secname(secname_, b), price(price_), zf(zf_)
        {}
};




typedef boost::multi_index::composite_key<quote,
        BOOST_MULTI_INDEX_MEMBER(quote, int, market),
        BOOST_MULTI_INDEX_MEMBER(quote, shm_string, seccode)
>comp_key_search;
typedef boost::multi_index::composite_key_compare<
        std::greater<int>,
        std::greater<shm_string>
>comp_key_compare_search;




typedef boost::multi_index::composite_key<quote,
        BOOST_MULTI_INDEX_MEMBER(quote, float, price),
        BOOST_MULTI_INDEX_MEMBER(quote, int, market),
        BOOST_MULTI_INDEX_MEMBER(quote, shm_string, seccode)
>comp_key_sortbyprice;
typedef boost::multi_index::composite_key_compare<
        std::greater<float>,
        std::greater<int>,
        std::greater<shm_string>
>comp_key_compare_sortbyprice;






typedef boost::multi_index::multi_index_container<
        quote,
        boost::multi_index::indexed_by<
                boost::multi_index::ordered_non_unique<comp_key_search, comp_key_compare_search>,
                boost::multi_index::ordered_non_unique<comp_key_sortbyprice, comp_key_compare_sortbyprice>
>,
        managed_shared_memory::allocator<quote>::type
>quote_mic;




int main()
{
/*
         struct shm_remove
   {
      shm_remove() { shared_memory_object::remove("MySharedMemory"); }
      ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
   } remover;
*/
         managed_shared_memory segment(open_only,"MySharedMemory");


        quote_mic *quotes = segment.find_or_construct<quote_mic>
      ("My MultiIndex Container") //Container's name in shared memory
      ( quote_mic::ctor_args_list(),
      //, segment.get_allocator<quote>()); //Ctor parameters
          quote_mic::allocator_type(segment.get_segment_manager()));


        


        quote_mic::nth_index<0>::type& idx_search = quotes->get<0>();
        quote_mic::nth_index<1>::type& idx_sortbyprice = quotes->get<1>();
        


        int count = 0;
        char_allocator ca1(segment.get_allocator<char>());


        // ²éѯ¹ÉƱʵʱÐÐÇé
        shm_string seccode("600000", ca1);
        count = idx_search.count(boost::tuples::make_tuple(1, seccode));
        quote_mic::nth_index<0>::type::iterator it = idx_search.find(boost::tuples::make_tuple(1, seccode));
        for (int i=0; i<count; i++)
        {
                std::cout << it->seccode << std::endl;
                it++;
        }








        std::vector<quote> result;
        BOOST_FOREACH(const quote& q, idx_sortbyprice)
        {
                int kind = q.kind;
                if (kind == 1 || kind == 11)
                {
// std::cout << "Êг¡" << q.market << ",Àà±ð" << q.kind << ", " << q.secname << ",×îмÛ" << q.price << std::endl;


                        result.push_back(q); this statement will compile error, why? how to write?
                }
                else
                {
// std::cout << "¹ýÂË£ºÊг¡" << q.market << ",Àà±ð" << q.kind << ", " << q.secname << ",×îмÛ" << q.price << std::endl;
                }
        }




        return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net