Skip to content

melsicon/cpp-sample

Repository files navigation

Melsicon C++ Sample

Experiment in setting up a C++ build pipeline.

edef4254dcd1c609713e25d8ac67f9c04dba5308f31c408d13

Purpose

This code is a simple FizzBuzz implementation, to play around with a C++17 build pipeline.

Running

Prerequisites

You need Bazelisk installed, with HomeBrew just use brew install bazelbuild/tap/bazelisk.

Tests

To run all tests, use

bazel test //test/...

Main App

Run the app with

bazel run //main:cpp-sample

or, for the alternate strategy (same result):

bazel run //main:cpp-sample-alt

Dependency Inversion Principle

This code demonstrates the application of the dependency inversion principle. Naively, one would simply make the main programm dependent on the concrete implementation of the transformation strategy. Here, we simply let the main programm own the interface, making it independent of the implementation and therefore the strategy exchangable.

Dependencies of //main:main
bazel query 'filter("^//", kind("source file", deps(//main:main)))'
//main:fizzbuzz.h
//main:main.cc
//main:main.h

Then, to put everything together, cpp-sample.cc or cpp-sample-alt.cc pull in the concrete implementaion making a runnable application:

Dependencies of //main:cpp-sample
bazel query 'filter("^//", kind("source file", deps(//main:cpp-sample)))'
//lib:fizzbuzz.cc
//lib:fizzbuzz.h
//main:cpp-sample.cc
//main:fizzbuzz.h
//main:main.cc
//main:main.h
Dependencies of //main:cpp-sample-alt
bazel query 'filter("^//", kind("source file", deps(//main:cpp-sample-alt)))'
//lib:fizzbuzz-alt.cc
//lib:fizzbuzz-alt.h
//main:cpp-sample-alt.cc
//main:fizzbuzz.h
//main:main.cc
//main:main.h

The test depends on both implementations, testing them via the shared interface:

Dependencies of //test:fizzbuzz-test
bazel query 'filter("^//", kind("source file", deps(//test:fizzbuzz-test)))'
//lib:fizzbuzz-alt.cc
//lib:fizzbuzz-alt.h
//lib:fizzbuzz.cc
//lib:fizzbuzz.h
//main:fizzbuzz.h
//test:fizzbuzz-test.cc

License

Copyright 2021 melsicon GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this material except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.