Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [C++] mappa di strutture

    Salve,

    sono un pò ingarbugliato nel codice che segue...ogni spiegazione è inserita nei commenti del codice:

    Codice PHP:
    #include <windows.h>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <map>
    #include <algorithm>
    #include <boost/circular_buffer.hpp>
    using namespace std;
    using namespace boost;

    void getDateTime(char szTime);
    const 
    int numbuff 3;
    const 
    int buflen  30;

    struct buffer
    {
        
    unsigned char data[1024];
        
    int bytesRecorded;
        
    int user;
        
    buffer(const unsigned char data_, const int bytesRecorded_, const int user_) :
            
    bytesRecorded(bytesRecorded_), user(user_)
            {
                
    copy(data_data_ bytesRecorded_data);
            }
    };

    struct circular
    {
        
    circular_buffer<buffercb[numbuff];
    };

    int main()
    {
        
    /*
         * Vorrei mettere in un array associativo
         * un /int/ e una struttura /circular/ che
         * a sua volta contiene un buffer circolare
         * di "numbuff" elementi (buffers) ogni volta
         * che faccio push_back aggiungo un nuovo
         * elemento verso la pila (sovrascrivo il più
         * vecchio).
         */

        
    map<intcircularusers// globale!

        /*
         * In questa parte il "Consumer"
         * aggiunge un nuovo elemento alla mappa
         * (nuovo buffer circolare sul quale
         * il "Producer" scriverà)
         */

        
    int regkey 1;                     // Chiave nella mappa
        
    circular c;                         // buffer vuoto
        
    users.insert(make_pair(regkeyc)); // aggiunto alla mappa

        /*
         * Quando il "Consumer" vuole uscire dalla
         * mappa non dovrà fare altro che rimuoversi
         * tramite la propria regkey
         */

        
    users.erase(regkey);

        
    /*
         * Ora viene il difficile! Il "Producer"
         * deve scrivere tutti i buffer degli
         * elementi che sono presenti nella mappa.
         * Sempre seguendo la logica del buffer
         * circolare. Quindi il metodo è push back.
         */

        
    map<intcircular>::const_iterator it;

        
    // while(1) ...thread...
        // scriviamo qualcosa di diverso ogni volta nei buffers
        
    char szTime[30]; getDateTime(szTime);

        for(
    it=users.begin(); it!=users.end(); ++it)
        {
            
    //
            // Facendo push_back so che sto scrivendo
            // oppure sovrascrivendo sull'ultimo elemento
            // nella pila dei buffers
            //
            
    it->second.cb->push_back(buffer((unsigned char*)szTime301));

            
    // purtroppo ottengo questo errore:
            // error C2662: 'boost::circular_buffer<T>::push_back' : 
            // cannot convert 'this' pointer
            // from 'const boost::circular_buffer<T>' to 'boost::circular_buffer<T> &'
        
    }

        
    system("pause");
        return 
    EXIT_SUCCESS;
    }

    void getDateTime(char szTime)
    {
        
    time_t rawtime time(NULL);
        
    struct tm timeinfo;
        
    gmtime_s(&timeinfo, &rawtime);
        
    strftime(szTime30"%a, %d %b %Y %X GMT", &timeinfo);

    In pratica ho una mappa che contiene come chiave un numero int e come valore una struttura circular che a sua volta contiene una struttura buffer

    La chiave mi servirà per aggiungere e/o rimuovere la struttura circular dalla mappa. Questo è solo uno schema generale di come dovrò affrontare la questione "Producer" e "Consumer" dove il "Producer" scriverà su tutti i buffers (circolari) contenuti nella mappa ed il "Consumer" a sua volta legge questi dati (ovvero legge l'ultimo elemento del buffer circolare)

    Devo dire cmq che sono moolto in altomare...

    grazie
    Alla batteria dai retta ballA

  2. #2
    ad esempio già facendo:

    codice:
    	// Prepare buffers:
    
    	for(int i = 0; i<numbuff; i++)
    	{
    		circular c;
    		c.cb->push_back(buffer(NULL, 0, 0));
    	}
    riesco ad accedere al metodo push_back ma non ai singola campi di buffer...
    Alla batteria dai retta ballA

  3. #3
    provo a spiegarmi meglio, all'inizio usavo questa struttura:

    codice:
    struct buffer
    {
    	unsigned char data[1024];
    	int bytesRecorded;
    	int user;
    	buffer(const unsigned char * data_, const int bytesRecorded_, const int user_) :
    		bytesRecorded(bytesRecorded_), user(user_)
    		{
    			copy(data_, data_ + bytesRecorded_, data);
    		}
    };
    che usavo in questo modo: main()

    codice:
    circular_buffer<buffer> cb(numbuff);
    
    cb.push_back( buffer((unsigned char*)szTime, 30, i) );
    cb.push_back( buffer((unsigned char*)szTime, 30, i) );
    // ...etc.. (circular buffer)
    
    // Last element
    cout << endl << "Last elements:" << endl;
    cout << &cb[numbuff-1].data[0] << endl;
    fin quì tutto bene, ora ho bisogno di mettere circular_buffer<buffer> cb[numbuff] dentro ad una mappa tramite insert, make_pair (la chiave sarà un int)

    solo che ovviamente non posso fare:

    codice:
    std::map<int, circular_buffer<buffer> cb(numbuff)> users;
    per questo avevo pensato di creare un'altra piccola struttura che fungesse da wrap-up:

    codice:
    struct circular
    {
    	circular_buffer<buffer> cb[numbuff];
    };
    allora si avrei potuto fare:

    codice:
    std::map<int, circular> users;
    soltato che poi vengono i problemi che vi ho descritto sopra...
    Alla batteria dai retta ballA

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.