Coder Social home page Coder Social logo

vc's Introduction

Boost Licence


Virtual Concepts

Concept

template<class T>
const auto Equality_comparable =
  $requires(T)(auto a, auto b) (
    bool(a == b),
    bool(a != b)
  );
template<class T>
const auto Iterator() {
  return CopyConstructible<T> &&
         CopyAssignable<T> &&
         Destructible<T> &&
         $requires(T)(auto&& t) (
           *t,
           typename decltype(t)::type
         );
  }
}
template<class T, class F, class R, class... Ts>
const auto Callable(F f) {
  return $requires(T)(auto&& t, Ts... args) (
    R((t.*f)(args...));
  )
}
template<class T, class R, class... Ts>
const auto Creatable = Callable<T, R(Ts...)>($(create));
struct Readable {
  template<class T>
  auto operator()() {
    return CopyConstructible() &&
       $requires(T)(auto&& t, std::ostream& os) (
          os = (os << t)
       ) &&
       Callable<T, void(int)>($(read));
  }
};

Implementation

struct FileReader {
  void read(int) const {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
  }
};

std::ostream& operator<<(std::ostream& os, FileReader&) {
  return os;
}

Usage

int main() {
  // constraint checking
  static_assert(Equality_comparable<int>, "");
  static_assert(Readable{}.operator()<FileReader>(), "");
  
  // template mocking
  testing::GMock<Readable> mockReadable;
  EXPECT_CALL(mockReadable, read, 42);
  mockReadable.read(42);
  
  // type erasure - dynamic dispatch
  type_erasure::any<Readable> readable = FileReader{};
  readable.read(42);
  
  // type erasure mocking
  readable = GMock<Readable>{};
  EXPECT_CALL(mock, read, 42);
  readable.read(42);
}

Dependency Injection - [Boost].DI

template<class TReader = Readable> // = 'Concept'
class App {
  TReader reader;                       // static dispatch
  type_erasure::any<Printable> printer; // dynamic dispatch
  
public:
  App(TReader reader, type_erasure::any<Printable> printer)
   : reader(reader), printer(printer)
  { }
  
  void run() {
    printer.print(reader.read());
  }
};

int main() {
  const auto injector = di::make_injector(
     di::bind<Readable>.to<FileReader>()
     di::bind<Printable>.to<ConsolePrinter>()
  );
  
  injector.create<App>().run();
}

Testing

"should print read text"_test = [] {
  constexpr auto value = 42;

  auto [sut, mocks] = testing::make<App>(); // creates System Under Test
                                            // and Mocks

  InSequence sequence;
  EXPECT_CALL(mocks<Readable>(), read).WillOnce(Return(value));
  EXPECT_CALL(mocks<Printable>(), print, value);

  sut.run();
};

Dependencies

References

vc's People

Contributors

kris-jusiak avatar krzysztof-jusiak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

blockspacer

vc's Issues

overload method support

struct a {
  void read(int) {}
};

struct b {
  void read(int, double) {}
  auto use() {
    return [](auto... t) { struct : decltype(t)... { using decltype(t)::read...; } _; return _; };
  }
};

struct m : decltype(b{}.use()(a{}, b{})) {
};

int main() {
  m _;
  _.read(42);
  _.read(1, 2.0);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.