Headline

Implementation in the widely used, low-level Language:C.

Characteristics

This contribution demonstrates the usage of the low-level, mostly structured_programming C to solve the inherently OO_programming problems of 101companies. Companies, departments and employees are modelled as behaviorless structs with functions operating on these structs. The inheritance found in subunits is modelled using manually tagged unions.

The container type for subunits also makes use of some functional_programming features of C to make traversal of containers more algorithmic and for application of the Visitor_pattern in various places.

Due to the nature of C, many operations in this contribution are type-unsafe and require explicit allocation and deallocation of dynamic memory. Encapsulation of such operations into functions helps to prevent type errors, but such discipline is not enforced by the language. There is also significantly less syntactic sugar compared to other languages, but it also gives a view on how those features work under the hood of those other languages.

Illustration

Data Model

The "object" model for companies, departments and employees is separated into structs holding plain old data and functions that operate on them.

struct c101_Company {
    char*              name;
    struct c101_Vector subunits;
};

struct c101_Company* c101_newCompany(const char* name,
                                     size_t      subunitCount,
                                                 ...);
void                c101_freeCompany(struct c101_Company* company);

// More functions follow.
// Analogous for departments and employees.

Departments and employees are grouped together as subunits into a manually tagged union to allow for some primitive Polymorphism. The type of the subunit needs to be checked manually if it needs to be known.

struct c101_Subunit {
    bool isDepartment; // <- Union Tag
    union {
        struct c101_Employee   employee;
        struct c101_Department department;
    };
};

Graph Traversal

Many of the implemented features rely on traversal of the "object" graph. This is implemented using the Visitor_pattern, which is relatively common in C due to its ability to pass callback functions. The currently visited data type is passed via an enum and an untyped pointer (void*), which needs to be checked and cast appropriately. Arbitrary user data can also by passed via such an untyped pointer, which emulates closures.

Here is the code for Feature:Total, which passes the result parameter as user data and adds an employee's salary to it whenever it encounters one.

static int
totalVisitor(enum c101_VisitorType type, void* unit, void* out)
{
    if (type == C101_EMPLOYEE)
        *(double*) out += ((struct c101_Employee*) unit)->salary;
    return 0;
}

double
c101_total(struct c101_Company* c)
{
    double out = 0.0;
    c101_visitCompany(c, &out, totalVisitor);
    return out;
}

Relationships

Architecture

As is usual for C, interface and implementation are separated into header files (declarations) and implementation files (definitions). This is mostly due to the language's primitive import capabilities: when a file is included, its contents just get dumped into the current compilation unit, only after compilation are these separate parts linked together.

Header files (.h) are found in the include directory and implementation files (.c) are found in the src directory.

Usage

This contribution uses Technology:CMake for handling its build process and its Technology:CTest for testing. Please see the readme.md on GitHub for details.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    10 most similar pages:

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.