eclipse-score / eclipse-score/persistency
KVS_CPP: hide implementation of public functions
- Dominant language
- Rust
- Stars
- 2
- Forks
- 28
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 32
Description
It is need to make the constructor public for the KVS to solve some issues with the mocking of the KVS class.
But with this would expose the constructor in the public api and would make it possible to directly construct a KVS object without the KVSBuilder .
To hide the constructor the following approach can be used:
### IFoo.hpp (public)
```cpp
#pragma once
#include
class IFoo {
public:
virtual void doSomething() = 0;
virtual ~IFoo() = default;
};
```
### FooBuilder.hpp (public)
```cpp
#pragma once
#include
#include "IFoo.hpp"
class FooBuilder {
public:
FooBuilder& setOption(int value) {
option = value;
return *this;
}
std::unique_ptr build(); // returns only the interface
private:
int option = 0;
};
```
### Foo.hpp (private, internal to library)
```cpp
#pragma once
#include "IFoo.hpp"
#include
class FooImpl; // hidden implementation
class Foo : public IFoo {
public:
void doSomething() override;
~Foo();
private:
explicit Foo(int option);
std::unique_ptr impl;
friend class FooBuilder; // builder can call Foo's ctor
};
```
### Foo.cpp (private, internal to library)
```cpp
#include "Foo.hpp"
#include "FooBuilder.hpp"
#include
// ----- Hidden implementation -----
class FooImpl {
public:
explicit FooImpl(int option) : option(option) {}
void doSomethingImpl() {
std::cout << "FooImpl doing something with option = "
<< option << std::endl;
}
private:
int option;
};
// ----- Foo -----
Foo::Foo(int option) : impl(std::make_unique(option)) {}
Foo::~Foo() = default;
void Foo::doSomething() {
impl->doSomethingImpl();
}
// ----- Builder -----
std::unique_ptr FooBuilder::build() {
return std::unique_ptr(new Foo(option));
}
```
### main.cpp (user code)
```cpp
#include "FooBuilder.hpp"
int main() {
auto foo = FooBuilder()
.setOption(42)
.build();
foo->doSomething();
}
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.