Skip to content
Andrey Sibiryov edited this page Apr 1, 2015 · 12 revisions

Overview

If you are planning to submit a pull request or write a plugin for Cocaine, then you should follow our codestyle. Instead of explaining it bit by bit, I'll just paste a code snippet here, which covers most of the code entities a developer will ever need.

We use spaces, tab width is 4 spaces. Commonly used soft line wrap is at 100 character mark, a hard line wrap is at 120, but it depends on the context.

C++11

We use C++11 features, but our bottom level support for compilers and toolchains is GCC 4.6 on Ubuntu Precise, so consult with C++11 support in GCC page first to find out if the feature you want to use is supported in our environments.

External dependencies

We try to stick to STL. Boost header-only libraries are allowed when necessary, but additional dependencies are very unlikely to be accepted into the project.

Code Snippet

#include "disassemble.hpp"

#include <memory>
#include <vector>

#include <boost/assert.hpp>

namespace cocaine { namespace nested {

template<class T>
class weapon_parts {
    // Type template names lack '_t' postfix, because such names don't refer
    // to a type, however typedefs (but not template aliases) should have it
    // appended to them.
    typedef weapon_parts<void> void_parts_t;
};

class plasma_rifle_t {
    std::unique_ptr<ammo_t> ammo;

public:
    template<class T>
    void
    take_aim_at(const T& target) const {
        target.prepare_to_die();
    }

    template<class... T>
    void
    burn_all_humans(T&&... humans) {
        try {
            auto human_furnace = prepare_human_furnace();
        } catch(const human_furnace_error& e) {
            std::cout << "Unable to ignite the human furnace!" << std::endl;
        }
    }

    struct part_t:
        public weapon_parts<plasma_rifle_t>
    {
        void
        sell() {
            if(get_country_code() == COUNTRY_USA) {
                return do_sell(*this);
            }

            BOOST_ASSERT_MSG(false, "selling firearms is a bad idea, man!"); 
        }
    };

    virtual
    std::vector<part_t>
    disassemble() {
        return aux::disassemble_impl<part_t>::apply(*this);
    }

    virtual
    auto
    disassemble_the_other_way_around() -> std::vector<part_t> {
        return disassemble();
    }

    plasma_rifle_t(uint32_t ammo_):
        ammo(ammo_)
    { }

    virtual
   ~plasma_rifle_t() {
        std::terminate();
    }

public:
    static inline
    int
    model_number() {
        return 271828;
    }
};

}} // namespace cocaine::nested