User Tools

Site Tools


Driver examples in C, C++, Go

Very simple examples =)

C driver example

driver.c
/*
 * Copyright: Oleg Borodin <onborodin@gmail.com>
 */
 
#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
struct driver {
    char *name;
    int counter;
    void (*init)(struct driver*);
    void (*write)(struct driver*, char*);
    void (*exit)(struct driver*);
};
 
typedef struct driver driver_t;
 
//
// behavior of driver A-type
//
void my_driverA_init(driver_t *this) {
    this->counter = 0;
    printf("driver A-type [%s] init\n", this->name);
} 
 
void my_driverA_write(driver_t *this, char *data) {
    this->counter += strlen(data);
    printf("driver A-type [%s] write: %s\n", this->name, data);
} 
 
void my_driverA_exit(driver_t *this) {
    printf("driver A-type [%s] wrote %d chars\n", this->name, this->counter);
    printf("driver A-type [%s] done\n", this->name);
} 
 
//
// behavior of driver B-type
//
void my_driverB_init(driver_t *this) {
    this->counter = 0;
    printf("driver B-type [%s] init\n", this->name);
} 
 
void my_driverB_write(driver_t *this, char *data) {
    this->counter += strlen(data);
    printf("driver B-type [%s] write: %s\n", this->name, data);
} 
 
void my_driverB_exit(driver_t *this) {
    printf("driver B-type [%s] wrote %d chars\n", this->name, this->counter);
    printf("driver B-type [%s] done\n", this->name);
} 
 
 
int main() {
    // construct driver
    driver_t driverA;
    driverA.name = "my driver #1";
    driverA.init = my_driverA_init;
    driverA.write = my_driverA_write;
    driverA.exit = my_driverA_exit;
 
    driver_t driverB;
    driverB.name = "my driver #2";
    driverB.init = my_driverB_init;
    driverB.write = my_driverB_write;
    driverB.exit = my_driverB_exit;
 
 
    // make array of drivers 
    driver_t **drivers;
    int driver_count = 2;
    drivers = (driver_t **)malloc(sizeof(driver_t*) * driver_count);
 
    // assign the driver to array element
    drivers[0] = &driverA;
    drivers[1] = &driverB;
 
    // init driver
    drivers[0]->init(drivers[0]);
    drivers[1]->init(drivers[1]);
 
    // driver operation
    char* data = "qwerty";
    drivers[0]->write(drivers[0], data);
    drivers[1]->write(drivers[1], data);
 
    // driver done
    drivers[0]->exit(drivers[0]);
    drivers[1]->exit(drivers[1]);
 
    return 0;
}
//EOF

Out#1

$ cc -o driver driver.c
$ ./driver
driver A-type [my driver #1] init
driver B-type [my driver #2] init
driver A-type [my driver #1] write: qwerty
driver B-type [my driver #2] write: qwerty
driver A-type [my driver #1] wrote 6 chars
driver A-type [my driver #1] done
driver B-type [my driver #2] wrote 6 chars
driver B-type [my driver #2] done

C++ example

driver.cpp
/*
 * Copyright: Oleg Borodin <onborodin@gmail.com>
 */
 
#include <string>
#include <iostream>
#include <vector>
 
class abstractDriver {
    public:
        std::string name;
        //abstractDriver(std::string name);
        //~abstractDriver();
        virtual void init() = 0;
        virtual void write(std::string data) = 0;
        virtual void exit() = 0; 
};
 
// first instance of abstract class  
 
class myADriver: public abstractDriver {
    private:
        int counter = 0;
    public:
        myADriver(std::string name);
 
        virtual void init();
        virtual void write(std::string data);
        virtual void exit(); 
};
 
myADriver::myADriver(std::string name) {
    this->name = name;
}
 
void myADriver::init() {
    this->counter = 0;
    std::cout << "driver [" << this->name << "] A-type init" << std::endl;
};
 
void myADriver::write(std::string data) {
    this->counter += data.length();
    std::cout << "driver [" << this->name << "] A-type  write: " << data << std::endl;
};
 
void myADriver::exit() {
    std::cout << "driver [" << this->name << "] A-type wrote: " << this->counter  << std::endl;
    std::cout << "driver [" << this->name << "] A-type done " << std::endl;
};
 
// second instance of abstract class  
 
class myBDriver: public abstractDriver {
    private:
        int counter = 0;
    public:
        myBDriver(std::string name);
        virtual void init();
        virtual void write(std::string data);
        virtual void exit(); 
};
 
myBDriver::myBDriver(std::string name) {
    this->name = name;
}
 
void myBDriver::init() {
    this->counter = 0;
    std::cout << "driver [" << this->name << "] B-type init" << std::endl;
};
 
void myBDriver::write(std::string data) {
    this->counter += data.length();
    std::cout << "driver [" << this->name << "] B-type write: " << data << std::endl;
};
 
void myBDriver::exit() {
    std::cout << "driver [" << this->name << "] B-type wrote: " << this->counter  << std::endl;
    std::cout << "driver [" << this->name << "] B-type done " << std::endl;
};
 
int main(void) {
    auto driverA = new myADriver("engine#1");
    auto driverB = new myBDriver("engine#2");
 
    // define container for parent abstract class
    std::vector<abstractDriver*> drivers;
 
    // ops, we can operate derivative classes as parent
    drivers.push_back(driverA);
    drivers.push_back(driverB);
 
    for (unsigned i=0; i < drivers.size(); i++) {
        drivers.at(i)->init();
    }
    for (unsigned i=0; i < drivers.size(); i++) {
        drivers.at(i)->write("qwerty");
    }
    for (unsigned i=0; i < drivers.size(); i++) {
        drivers.at(i)->exit();
    }
 
}
//EOF

Out#2

$ c++ -o drvier driver.cpp
$ ./driver
driver [engine#1] A-type init
driver [engine#2] B-type init
driver [engine#1] A-type  write: qwerty
driver [engine#2] B-type write: qwerty
driver [engine#1] A-type wrote: 6
driver [engine#1] A-type done 
driver [engine#2] B-type wrote: 6
driver [engine#2] B-type done 

Go driver example

driver.go
/*
 * Copyright: Oleg Borodin <onborodin@gmail.com>
 */
 
package main
 
import (
    "fmt"
)
 
type Driver interface {
    init()
    write(data string)
    exit()
}
 
type driverAType struct {
    name string
    counter int64
}
 
func (this *driverAType) init() {
    fmt.Println("a-type driver", this.name, "init")
}
 
func (this *driverAType) write(data string) {
    this.counter += int64(len(data))
    fmt.Println("a-type driver", this.name, "write", data)
}
 
func (this *driverAType) exit() {
    fmt.Println("a-type driver wrote", this.counter, "chars")
    fmt.Println("a-type driver", this.name, "done")
}
 
func NewDriverAType(name string) *driverAType {
    return &driverAType{ name: name }
}
 
 
type driverBType struct {
    name string
    counter int
}
 
func (this *driverBType) init() {
    fmt.Println("b-type driver", this.name, "init")
}
 
func (this *driverBType) write(data string) {
    this.counter += int(len(data))
    fmt.Println("b-type driver", this.name, "write", data)
}
 
func (this *driverBType) exit() {
    fmt.Println("b-type driver wrote", this.counter, "chars")
    fmt.Println("b-type driver", this.name, "done")
}
 
func NewDriverBType(name string) *driverBType {
    return &driverBType{ name: name }
}
 
func main() {
    var drivers []Driver
 
    drivers = append(drivers, NewDriverAType("driver#1"))
    drivers = append(drivers, NewDriverBType("driver#2"))
 
    for i := range drivers {
        drivers[i].init()
    }
 
    for i := range drivers {
        drivers[i].write("qwerty")
    }
 
    for i := range drivers {
        drivers[i].exit()
    }
}
//EOF

Out#3

$ go run abst1.go
a-type driver driver#1 init
b-type driver driver#2 init
a-type driver driver#1 write qwerty
b-type driver driver#2 write qwerty
a-type driver wrote 6 chars
a-type driver driver#1 done
b-type driver wrote 6 chars
b-type driver driver#2 done