Changeset 27:a228b1e1e16a in xplra for src/client


Ignore:
Timestamp:
02/08/13 18:21:29 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Made the class Slot into a template

Location:
src/client/c/hu/varadiistvan/xplra
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/xplra.cc

    r25 r27  
    183183
    184184/**
    185  * Information about a connection slot in the vector of connections.
     185 * Template for handling slots.
    186186 */
     187template <class Value>
    187188struct Slot
    188189{
     
    191192     * The slots.
    192193     */
    193     static std::vector<Slot> slots;
     194    static std::vector< Slot<Value> > slots;
    194195
    195196    /**
     
    200201public:
    201202    /**
    202      * Add a new connection.
    203      */
    204     static int addConnection(Connection* connection) throw();
    205 
    206     /**
    207      * Get the connection with the given ID.
    208      */
    209     static Connection* getConnection(int connectionID) throw();
    210 
    211     /**
    212      * Clear the connection with the given ID.
    213      */
    214     static void clearConnection(int connectionID) throw();
    215 
    216     // Indicate if the slot contains a valid connection or not
     203     * Add a new value.
     204     *
     205     * @return the ID of the new value
     206     */
     207    static int addValue(Value value) throw();
     208
     209    /**
     210     * Get the value with the given ID.
     211     */
     212    static Value getValue(int valueID) throw();
     213
     214    /**
     215     * Clear the value with the given ID.
     216     */
     217    static void clearValue(int valueID) throw();
     218
     219private:
     220    /**
     221     * Indicate if the slot contains a valid value or not.
     222     */
    217223    bool valid;
    218224
    219225    union {
    220         // The connection, if it is a valid connection
    221         Connection* connection;
     226        // The value, if the slot contains a value
     227        Value value;
    222228
    223229        // The index of the next free slot
     
    226232
    227233    /**
    228      * Construct a slot with the given connection
    229      */
    230     Slot(Connection* connection) throw();
    231 
    232     /**
    233      * Set the connection and return the former next free index. It
    234      * should be called for a slot with no connection.
    235      */
    236     int setConnection(Connection* connection) throw();
    237 
    238     /**
    239      * Get the connection if this slot contains a connection.
    240      */
    241     Connection* getConnection() const throw();
    242 
    243     /**
    244      * Clear the connection and set the given next free index.
     234     * Construct a slot with the given value
     235     */
     236    Slot(Value value) throw();
     237
     238    /**
     239     * Set the value and return the former next free index. It
     240     * should be called for a slot with no value.
     241     */
     242    int setValue(Value value) throw();
     243
     244    /**
     245     * Get the value if this slot contains a value. Otherwise 0 is
     246     * returned (converted to value).
     247     */
     248    Value getValue() const throw();
     249
     250    /**
     251     * Clear the value and set the given next free index.
    245252     */
    246253    void clear(int nextFreeIndex) throw();
     
    249256//------------------------------------------------------------------------------
    250257
    251 vector<Slot> Slot::slots;
    252 
    253 //------------------------------------------------------------------------------
    254 
    255 int Slot::firstFree = -1;
    256 
    257 //------------------------------------------------------------------------------
    258 
    259 inline Slot::Slot(Connection* connection) throw() :
     258template <class Value> vector< Slot<Value> > Slot<Value>::slots;
     259
     260//------------------------------------------------------------------------------
     261
     262template <class Value> int Slot<Value>::firstFree = -1;
     263
     264//------------------------------------------------------------------------------
     265
     266template <class Value>
     267inline Slot<Value>::Slot(Value value) throw() :
    260268    valid(true)
    261269{
    262     this->connection = connection;
    263 }
    264 
    265 //------------------------------------------------------------------------------
    266 
    267 inline int Slot::setConnection(Connection* connection) throw()
     270    this->value = value;
     271}
     272
     273//------------------------------------------------------------------------------
     274
     275template <class Value>
     276inline int Slot<Value>::setValue(Value value) throw()
    268277{
    269278    assert(!valid);
    270279    int nextFreeIndex = this->nextFreeIndex;
    271     this->connection = connection;
     280    this->value = value;
    272281    valid = true;
    273282    return nextFreeIndex;
     
    276285//------------------------------------------------------------------------------
    277286
    278 inline Connection* Slot::getConnection() const throw()
    279 {
    280     return valid ? connection : 0;
    281 }
    282 
    283 //------------------------------------------------------------------------------
    284 
    285 inline void Slot::clear(int nextFreeIndex) throw()
     287template <class Value>
     288inline Value Slot<Value>::getValue() const throw()
     289{
     290    return valid ? value : static_cast<Value>(0);
     291}
     292
     293//------------------------------------------------------------------------------
     294
     295template <class Value>
     296inline void Slot<Value>::clear(int nextFreeIndex) throw()
    286297{
    287298    assert(valid);
     
    292303//------------------------------------------------------------------------------
    293304
    294 int Slot::addConnection(Connection* connection) throw()
     305template <class Value>
     306int Slot<Value>::addValue(Value value) throw()
    295307{
    296308    int id = firstFree;
    297309    if (id<0) {
    298310        id = slots.size();
    299         slots.push_back(Slot(connection));
     311        slots.push_back(Slot<Value>(value));
    300312    } else {
    301313        Slot& slot = slots[id];
    302         firstFree = slot.setConnection(connection);
     314        firstFree = slot.setValue(value);
    303315    }
    304316
     
    308320//------------------------------------------------------------------------------
    309321
    310 Connection* Slot::getConnection(int connectionID) throw()
    311 {
    312     size_t index = static_cast<size_t>(connectionID);
    313     return (index<slots.size()) ? slots[index].getConnection() : 0;
    314 }
    315 
    316 //------------------------------------------------------------------------------
    317 
    318 void Slot::clearConnection(int connectionID) throw()
    319 {
    320     size_t index = static_cast<size_t>(connectionID);
     322template <class Value>
     323Value Slot<Value>::getValue(int valueID) throw()
     324{
     325    size_t index = static_cast<size_t>(valueID);
     326    return (index<slots.size()) ?
     327        slots[index].getValue() : static_cast<Value>(0);
     328}
     329
     330//------------------------------------------------------------------------------
     331
     332template <class Value>
     333void Slot<Value>::clearValue(int valueID) throw()
     334{
     335    size_t index = static_cast<size_t>(valueID);
    321336    if (index<slots.size()) {
    322337        slots[index].clear(firstFree);
     
    326341
    327342//------------------------------------------------------------------------------
     343//------------------------------------------------------------------------------
     344
     345/**
     346 * Slot for connections.
     347 */
     348typedef Slot<Connection*> ConnectionSlot;
     349
     350//------------------------------------------------------------------------------
    328351
    329352} /* anonymous namespace */
     
    333356extern "C" int xplra_get_last_error(int connectionID, unsigned long* subCode)
    334357{
    335     Connection* connection = Slot::getConnection(connectionID);
     358    Connection* connection = ConnectionSlot::getValue(connectionID);
    336359    return (connection==0) ? -1 : connection->getLastError(subCode);
    337360}
     
    341364extern "C" const char* xplra_get_last_error_string(int connectionID)
    342365{
    343     Connection* connection = Slot::getConnection(connectionID);
     366    Connection* connection = ConnectionSlot::getValue(connectionID);
    344367    return (connection==0) ? 0 : connection->getLastErrorString();
    345368}
     
    349372extern "C" void xplra_clear_last_error(int connectionID)
    350373{
    351     Connection* connection = Slot::getConnection(connectionID);
     374    Connection* connection = ConnectionSlot::getValue(connectionID);
    352375    if (connection!=0) connection->clearLastError();
    353376}
     
    360383        auto_ptr<Connection> connection(new Connection());
    361384        connection->connect();
    362         return Slot::addConnection(connection.release());
     385        return ConnectionSlot::addValue(connection.release());
    363386    } catch(...) {
    364387        return -1;
     
    370393extern "C" int xplra_get_int(int* value, int connectionID, const char* name)
    371394{
    372     Connection* connection = Slot::getConnection(connectionID);
     395    Connection* connection = ConnectionSlot::getValue(connectionID);
    373396    if (connection==0) return -1;
    374397    try {
     
    385408extern "C" int xplra_get_float(float* value, int connectionID, const char* name)
    386409{
    387     Connection* connection = Slot::getConnection(connectionID);
     410    Connection* connection = ConnectionSlot::getValue(connectionID);
    388411    if (connection==0) return -1;
    389412    try {
     
    401424                                int connectionID, const char* name)
    402425{
    403     Connection* connection = Slot::getConnection(connectionID);
     426    Connection* connection = ConnectionSlot::getValue(connectionID);
    404427    if (connection==0) return -1;
    405428    try {
     
    417440                                         int connectionID, const char* name)
    418441{
    419     Connection* connection = Slot::getConnection(connectionID);
     442    Connection* connection = ConnectionSlot::getValue(connectionID);
    420443    if (connection==0) return -1;
    421444    try {
     
    432455                                            int connectionID, const char* name)
    433456{
    434     Connection* connection = Slot::getConnection(connectionID);
     457    Connection* connection = ConnectionSlot::getValue(connectionID);
    435458    if (connection==0) return 0;
    436459    try {
     
    447470                                       int connectionID, const char* name)
    448471{
    449     Connection* connection = Slot::getConnection(connectionID);
     472    Connection* connection = ConnectionSlot::getValue(connectionID);
    450473    if (connection==0) return -1;
    451474    try {
     
    462485                                            int connectionID, const char* name)
    463486{
    464     Connection* connection = Slot::getConnection(connectionID);
     487    Connection* connection = ConnectionSlot::getValue(connectionID);
    465488    if (connection==0) return 0;
    466489    try {
     
    477500                                        int connectionID, const char* name)
    478501{
    479     Connection* connection = Slot::getConnection(connectionID);
     502    Connection* connection = ConnectionSlot::getValue(connectionID);
    480503    if (connection==0) return -1;
    481504    try {
     
    493516                                             int connectionID, const char* name)
    494517{
    495     Connection* connection = Slot::getConnection(connectionID);
     518    Connection* connection = ConnectionSlot::getValue(connectionID);
    496519    if (connection==0) return 0;
    497520    try {
     
    507530extern "C" int xplra_set_int(int connectionID, const char* name, int value)
    508531{
    509     Connection* connection = Slot::getConnection(connectionID);
     532    Connection* connection = ConnectionSlot::getValue(connectionID);
    510533    if (connection==0) return 0;
    511534    try {
     
    522545extern "C" int xplra_set_float(int connectionID, const char* name, float value)
    523546{
    524     Connection* connection = Slot::getConnection(connectionID);
     547    Connection* connection = ConnectionSlot::getValue(connectionID);
    525548    if (connection==0) return 0;
    526549    try {
     
    538561                                double value)
    539562{
    540     Connection* connection = Slot::getConnection(connectionID);
     563    Connection* connection = ConnectionSlot::getValue(connectionID);
    541564    if (connection==0) return 0;
    542565    try {
     
    555578                                     size_t length, size_t offset)
    556579{
    557     Connection* connection = Slot::getConnection(connectionID);
     580    Connection* connection = ConnectionSlot::getValue(connectionID);
    558581    if (connection==0) return 0;
    559582    try {
     
    572595                                   size_t length, size_t offset)
    573596{
    574     Connection* connection = Slot::getConnection(connectionID);
     597    Connection* connection = ConnectionSlot::getValue(connectionID);
    575598    if (connection==0) return 0;
    576599    try {
     
    589612                                    size_t length, size_t offset)
    590613{
    591     Connection* connection = Slot::getConnection(connectionID);
     614    Connection* connection = ConnectionSlot::getValue(connectionID);
    592615    if (connection==0) return 0;
    593616    try {
     
    607630                                size_t length, size_t offset)
    608631{
    609     Connection* connection = Slot::getConnection(connectionID);
     632    Connection* connection = ConnectionSlot::getValue(connectionID);
    610633    if (connection==0) return 0;
    611634    try {
     
    622645extern "C" int xplra_disconnect(int connectionID)
    623646{
    624     Connection* connection = Slot::getConnection(connectionID);
     647    Connection* connection = ConnectionSlot::getValue(connectionID);
    625648    if (connection==0) return -1;
    626649
    627     Slot::clearConnection(connectionID);
     650    ConnectionSlot::clearValue(connectionID);
    628651    connection->disconnect();
    629652    delete connection;
  • src/client/c/hu/varadiistvan/xplra/xplra.h

    r25 r27  
    126126
    127127/*----------------------------------------------------------------------------*/
     128/* Single dataref support                                                     */
     129/*----------------------------------------------------------------------------*/
    128130
    129131/**
     
    315317
    316318/*----------------------------------------------------------------------------*/
     319/* Multi-dataref support                                                      */
     320/*----------------------------------------------------------------------------*/
     321
     322
     323
     324/*----------------------------------------------------------------------------*/
    317325
    318326/**
Note: See TracChangeset for help on using the changeset viewer.