Should safe pointers be used in strategy pattern? - c++

Given a typical strategy pattern
class Strategy
{
public:
virtual int execute() const = 0;
}
class StrategyA : public Strategy
{
public:
int execute() const override;
}
class StrategyB : public Strategy
{
public:
int execute() const override;
}
I believe the 'pre-C++11' way to implement a context class would be something like
class ContextRaw
{
public:
ContextRaw(Strategy* the_strategy);
~ContextRaw(); // Should this delete the_strategy_?
int execute() const;
private:
Strategy* the_strategy_;
}
To me, in this design it's not clear if Context should take responsibility for Strategy, and unless there is clear documentation stating otherwise, bad things might happen if it does
void trouble()
{
StrategyA a_concrete_strategy;
ContextRaw a_context(&a_concrete_strategy); // Oops, Context may try to delete stack variable
}
void more_trouble()
{
Strategy* a_concrete_strategy = new StrategyA;
ContextRaw* a_context = new ContextRaw(a_concrete_strategy);
ContextRaw* another_context = new ContextRaw(a_concrete_strategy);
delete a_context;
std::cout << another_context.execute() << std::endl; // Oops, the_strategy is deleted
}
In light of safe-pointers, should it now be preferable to inject a safe pointer, and have Context take ownership of the Strategy?
class ContextUnique
{
public:
ContextUnique() = delete;
ContextUnique(std::unique_ptr<Strategy> the_strategy);
~ContextUnique();
int execute() const;
private:
std::unique_ptr<Strategy> the_strategy_;
}
or if Strategy can be shared amongst different Context?
class ContextShared
{
public:
ContextShared() = delete;
ContextShared(std::shared_ptr<Strategy> the_strategy);
~ContextShared();
int execute() const;
private:
std::shared_ptr<Strategy> the_strategy_;
}
This design of course introduces problems of it's own, in particular only dynamically allocated Strategy's can be injected into Context.

The design is up to the implementator.
Notice that in your examples you reference different ways of screwing up with Strategy pattern using non C++11 pointers.
To answer directly to your question :
Yes you should use smart pointers in strategy pattern.
To further answer the question :
You should use smart pointers whenever possible.
The reason is that smart pointers practically are self documenting in terms of memory ownership policy, so you get rid of some the drawbacks of "If no good documentation".
Considering the prototype that you expose for your Context class , you can tell users what are your expectations :
unique_ptr if you expect the user to pass memory ownership to you
shared_ptr if you expect the same strategy implementation to be used on multiple owners
weak_ptr if you want the user to handle memory management
What is safer, it's up to you. However , you are able to tell users that the Context can share it's concrete strategy with other Contexts OR that there is 1 Concrete strategy per Context.
As a design approach, I would suggest to go with 1 Strategy / Context (so unique_ptr) since your concrete strategies might end up having some internal variables that are unique / context , and things will get complicated from there on.

You're doing it wrong.
In the light of std::function, everything you've just written is completely obsolete and you should just use std::function<int()> and some lambdas.

It strongly depends on what's the real purpose of Strategy objects whether they should be shared between various Context objects or owned by them.
At least when you use the shared or unique ptr, you clearly define your intentions. You should use a "raw" pointer only if you are going to "view" some other objects (you don't share nor own it - and you are sure that the pointing object won't outlive the pointed one).

Related

C++: How can I use different implementation of methods with the same data class?

Background:
Various modules of the program I'm involved with deal with the same combination of objects that are grouped together in an aggregating structure. There are well-known invariants imposed on that combination of objects, and those invariants are respected by all modules to the fullest extent. Each module is developed by a dedicated team, and each team needs their custom domain-specific methods to deal with that combination of objects.
Example:
To give you a tangible idea, imagine a sequence container class. The core of the container is the same across all users: it consists of data members for the storage, size/capacity and the allocator. But the set of methods, the contract and the body of those methods may vary a lot. One module may implement std-style operations, another module may implement all operations as nothrow methods, yet another module may insist on using their private checked iterators; some performance-critical module takes pain to ban all copy operations, while yet another module is all for making copies... Such requirements are well justified in each particular domain of any given module.
Speculations:
So, providing a single non-redundant set of methods which would satisfy the needs of all the client teams is impossible - requirements of some teams are mutually exclusive. Providing only those methods that are commonly required by all modules is rather useless, because the only common part is, probably, the destructor. Throwing together all possible implementations of all methods is not good either: poor maintainability and stability, confusingly bloated interface, lots of name clashes, lots of cross-module dependencies.
The question:
What options do I have to let several independent implementations operate on the same set of data members?
Things I tried:
Solutions I can see thus far aren't exactly nice, and I ain't entirely happy with any of them. I'll list them in answers, three approaches one by one.
A possible less-than-perfect solution to my own question:
2. Have a set of classes that operate on an external instance of core data by reference.
struct CoreData
{
int m_x;
~CoreData();
};
class TeamA
{
public:
// Allocates a new instance and owns it.
TeamA();
// Attaches an external instance without assuming ownership.
TeamA(CoreData& ext);
// Release the core, if must.
~TeamA();
void push_back(Whatever args);
Iter begin();
CoreData& GetCore();
private:
CoreData* m_core;
bool m_doIOwnThatCore;
};
class TeamB
{
public:
TeamB();
TeamB(CoreData& ext);
~TeamB();
int push_back(Whatever args);
CoreData& GetCore();
private:
CoreData* m_core;
bool m_doIOwnThatCore;
};
//--------------------- Usage:
void ServiceOfTeamA::CallServiceOfTeamB(ServiceOfTeamB* srv)
{
TeamA d;
srv->Process(d.GetCore());
d.begin();
}
void ServiceOfTeamB::Process(CoreData* core)
{
TeamB d(core);
d.push_back(567);
}
- What I don't like about this approach is that it imposes slight pessimization in terms of memory usage and performance. Also the syntax makes objects of type TeamA and TeamB look like values, while actually they have reference semantic.
+ Good news is that this approach allows somewhat better C++ syntax for the calls (but still, there's that ugly GetCore()), and meets RAII.
A possible less-than-perfect solution to my own question:
3. Throw the code on the mercy of de-facto defined behavior of reinterpret_cast.
// This is a standard layout class.
// It is the only class with data members;
// derived classes never append new data members.
class CoreData
{
public:
// Could be either explicit conversion function
// or implicit conversion operator.
template <typename Final>
// requires <LayoutCompatibleWithCore Final>
Final& As()
{
return reinterpret_cast<Final&>(*this);
}
protected:
~CoreData();
int m_x;
};
// No extra data members appended. No extra invariants imposed.
// This class is also a standard layout type,
// fully layout-compatible with CoreData.
class TeamA : public CoreData
{
public:
void push_back(Whatever args);
Iter begin();
};
class TeamB : public CoreData
{
public:
bool push_back(Whatever args);
X* begin();
};
//--------------------- Usage:
void ServiceOfTeamA::CallServiceOfTeamB(ServiceOfTeamB* srv)
{
TeamA d;
srv->Process(&d);
d.begin();
}
void ServiceOfTeamB::Process(CoreData* core)
{
TeamB& d = core->As<TeamB>();
d.push_back(567);
}
- However, such tricks are outlawed by the standard. So I have to decline this approach, too.
+ If it was legal, it would have offered the best syntax of the three, the syntax clearly showing the reference semantic, with RAII and no pessimization.
P.S. Invalidity of this approach puts me down. The whole point of layout compatibility seems to give the ability to communicate data between ABI-compatible processes or shared components. Too bad it doesn't allow communicating the data between parts of the same application...
A possible less-than-perfect solution to my own question:
1. Don't bother with methods and have freestanding functions instead.
struct CoreData
{
int m_x;
~CoreData();
};
void TeamA_init(CoreData& data);
void TeamA_push_back(CoreData& data, Whatever args);
Iter TeamA_begin(CoreData& data);
bool TeamB_init(CoreData& data, Other args);
bool TeamB_push_back(CoreData& data, Whatever args);
X* TeamB_begin(CoreData& data);
//--------------------- Usage:
void ServiceOfTeamA::CallServiceOfTeamB(ServiceOfTeamB* srv)
{
CoreData d;
TeamA_init(d);
srv->Process(&d);
TeamA_begin(d);
}
void ServiceOfTeamB::Process(CoreData* d)
{
TeamB_push_back(*d, 567);
}
- What I don't like about this approach is unfriendly syntax, no RAII and all data members being public. That's C, not C++.
+ On the bright side, this approach offers unlimited customization possibilities. No restrictions on choosing the right function for the task. No memory overhead, no runtime overhead (that is technically, the compiler have the same inlining and optimization opportunities as it would have would those free functions be methods).

Is this an idiomatic use of reference class members?

I have the following class definitions:
class Policy {
public:
virtual DigraphNode getBestMove(DigraphNode &node) const = 0;
};
class NoPolicy : public Policy{
virtual DigraphNode getBestMove(DigraphNode &node) const
{return DigraphNode();}
};
class Agent {
public:
static NoPolicy noPolicy;
Agent() : policy(noPolicy) {}
void setPolicy(Policy &p) {policy = p;}
Policy &getPolicy() {return policy;}
private:
Policy &policy; // pointer so polymorphism should work
};
Agent::policy may store an object of any descendant class of Policy. To avoid using pointers, I made this member a reference. However, then I need to initialize it in the constructor, which forced me to define an artificial policy NoPolicy as you see in the code. This works, but seems to be a bit artificial.
To complete the picture, I ended up having the following usage in the client code:
Policy &policy = Agent::noPolicy;
switch(agentTypes[a]) {
case NON_COMPLIANT:
policy = HeuristicBasedPolicy<MultiHeuristic>(nearExitH);
break;
etc.
I would very much appreciate if someone can suggest a more elegant way. Thanks!
Remark: The comment in the code is out-dated. Agent::policy is a reference, of course, not a pointer.
----------------------- AN EDIT (based on the feedback) ----------------------------------
Based on the feedback that I got (thanks!), I have modified the code. The class definitions are:
class Policy {
public:
virtual DigraphNode getBestMove(DigraphNode &node) const = 0;
};
class Agent {
public:
Agent() {policy = nullptr;}
void setPolicy(Policy *p) {policy = p;}
Policy &getPolicy() {return *policy;}
private:
Policy *policy; // pointer so polimorphism should work
};
The usage is:
vector<Policy *> agentPolicies; // need this only to free memory!
while (node->getDepth()) {
int a = node->getDepth() - 1;
Policy *myPolicy;
switch(agentTypes[a]) {
case NON_COMPLIANT:
myPolicy = new HeuristicBasedPolicy<MultiHeuristic>(nearExitH);
break;
...
}
agents[a].setPolicy(myPolicy);
agentPolicies.push_back(myPolicy);
}
...
for (int i = 0; i < agentPolicies.size(); i++)
delete(agentPolicies[i]);
Please let me know if this was the intention of the people who commented and replied.
Also, the array in the first line of the client code is needed only to keep pointers to later release them. Is this a normal state of affairs to have an array just for the bookkeeping of memory management? Thanks!
As you've seen, the reference has the problem that it must be
set in the constructor. And then can never be changed. If this
is what you want, and the class shouldn't be assignable
(probably the case in something called Agent), then
a reference is a valid solution. If the class needs to be
assignable, or you might need to change the policy after it is
constructed, then a pointer is the usual solution.
More generally, I've seen coding guidelines which forbid
reference members, because they make it impossible to make the
object assignable. Personally, I don't agree with them, because
a lot of classes have identity, and shouldn't support copy and
assignment. But it's probably fairly idiomatic to
systematically use pointers as class members, even when
a reference would work.
Having said that: using a no-op class is also fairly idiomatic
in cases like this. It's often easier to have a class invariant
policy != nullptr, and just do policy->something(), rather
than having to check for nullptr all over the place. (You may
still want the pointer, however, because you may want to change
the policy, particularly if it is a NullPolicy, after the
object has been constructed.) If you do this, you can (and
probably should) have functions like getPolicy return
a reference, and functions like setPolicy take a reference.
Also, classes like Policy are probably immutable (especially
if you share instances of them), so you should be using pointers
to const and references to const.
And finally: why do you want to avoid using pointers? Well
written C++, at least object oriented C++, will be full of
pointers.

return value vs return reference in C++ [duplicate]

Effective C++ by Scott Meyers tells in Chapter 5, Item 28 to avoid returning "handles" (pointers, references or iterators) to object internals and it definitely makes a good point.
I.e. don't do this:
class Family
{
public:
Mother& GetMother() const;
}
because it destroys encapsulation and allows to alter private object members.
Don't even do this:
class Family
{
public:
const Mother& GetMother() const;
}
because it can lead to "dangling handles", meaning that you keep a reference to a member of an object that is already destroyed.
Now, my question is, are there any good alternatives? Imagine Mother is heavy! If I now return a copy of Mother instead of a reference, GetMother is becoming a rather costly operation.
How do you handle such cases?
First, let me re-iterate: the biggest issue is not one of lifetime, but one of encapsulation.
Encapsulation does not only mean that nobody can modify an internal without you being aware of it, encapsulation means that nobody knows how things are implemented within your class, so that you can change the class internals at will as long as you keep the interface identical.
Now, whether the reference you return is const or not does not matter: you accidentally expose the fact that you have a Mother object inside of your Family class, and now you just cannot get rid of it (even if you have a better representation) because all your clients might depend on it and would have to change their code...
The simplest solution is to return by value:
class Family {
public:
Mother mother() { return _mother; }
void mother(Mother m) { _mother = m; }
private:
Mother _mother;
};
Because in the next iteration I can remove _mother without breaking the interface:
class Family {
public:
Mother mother() { return Mother(_motherName, _motherBirthDate); }
void mother(Mother m) {
_motherName = m.name();
_motherBirthDate = m.birthDate();
}
private:
Name _motherName;
BirthDate _motherBirthDate;
};
See how I managed to completely remodel the internals without changing the interface one iota ? Easy Peasy.
Note: obviously this transformation is for effect only...
Obviously, this encapsulation comes at the cost of some performance, there is a tension here, it's your judgement call whether encapsulation or performance should be preferred each time you write a getter.
Possible solutions depend on actual design of your classes and what do you consider "object internals".
Mother is just implementation detail of Family and could be completely hidden from Family user
Family is considered as composition of other public objects
In first case you shall completely encapsulate subobject and provide access to it only via Family function members (possibly duplicating Mother public interface):
class Family
{
std::string GetMotherName() const { return mommy.GetName(); }
unsigned GetMotherAge() const { return mommy.GetAge(); }
...
private:
Mother mommy;
...
};
Well, it can be boring if Mother interface is quite large, but possibly this is design problem (good interfaces shall have 3-5-7 members) and this will make you revisit and redesign it in some better way.
In second case you still need to return entire object. There are two problems:
Encapsulation breakdown (end-user code will depend on Mother definition)
Ownership problem (dangling pointers/references)
To adress problem 1 use interface instead of specific class, to adress problem 2 use shared or weak ownership:
class IMother
{
virtual std::string GetName() const = 0;
...
};
class Mother: public IMother
{
// Implementation of IMother and other stuff
...
};
class Family
{
std::shared_ptr<IMother> GetMother() const { return mommy; }
std::weak_ptr<IMother> GetMotherWeakPtr() const { return mommy; }
...
private:
std::shared_ptr<Mother> mommy;
...
};
If a read-only view is what you're after, and for some reason you need to avoid dangling handles, then you can consider returning a shared_ptr<const Mother>.
That way, the Mother object can out-live the Family object. Which must also store it by shared_ptr, of course.
Part of the consideration is whether you're going to create reference loops by using too many shared_ptrs. If you are, then you can consider weak_ptr and you can also consider just accepting the possibility of dangling handles but writing the client code to avoid it. For example, nobody worries too much about the fact that std::vector::at returns a reference that becomes stale when the vector is destroyed. But then, containers are the extreme example of a class that intentionally exposes the objects it "owns".
This goes back to a fundamental OO principle:
Tell objects what to do rather than doing it for them.
You need Mother to do something useful? Ask the Family object to do it for you. Hand it any external dependencies wrapped up in a nice interface (Class in c++) through the parameters of the method on the Family object.
because it can lead to "dangling handles", meaning that you keep a
reference to a member of an object that is already destroyed.
Your user could also de-reference null or something equally stupid, but they're not going to, and nor are they going to do this as long as the lifetime is clear and well-defined. There's nothing wrong with this.
It's just a matter of semantics. In your case, Mother is not Family internals, not its implementation details. Mother class instance can be referenced in a Family, as well as in many other entities. Moreover, Mother instance lifetime may even not correlate with Family lifetime.
So better design would be to store in Family a shared_ptr<Mother>, and expose it in Family interface without worries.

What is the purpose of this code?

I am struggling to understand why the initialization of pprocessor, below, is written like this:
class X
{
...
private:
boost::scoped_ptr<f_process> pprocessor_;
};
X:X()
: pprocessor_( f_process_factory<t_process>().make() ) //why the factory with template
{...}
instead of just writing
X:X()
: pprocessor_( new t_process() )
{...}
Other relevant code is:
class f_process {
...
};
class t_process : public f_process {
...
};
//
class f_process_factory_base {
public:
f_process_factory_base() { }
virtual ~f_process_factory_base() = 0 { }
virtual f_process* make() = 0;
};
template < typename processClass >
class f_process_factory : public f_process_factory_base {
public:
f_process_factory() { }
virtual ~f_process_factory() { }
virtual processClass* make() { return new processClass(); }
};
The guy who wrote the code is very clever so perhaps there is a good reason for it.
(I can't contact him to ask)
As it is, it seems kinda pointless, but I can think of a few possible uses that aren't shown here, but may be useful in the future:
Memory management: It's possible that at some point in the future the original author anticipated needing a different allocation scheme for t_process. For example, he may want to reuse old objects or allocate new ones from an arena.
Tracking creation: There may be stats collected by the f_process_factory objects when they're created. Maybe the factory can keep some static state.
Binding constructor parameters: Perhaps a specialization of the f_process_factory for t_process at some point in the future needs to pass constructor parameters to the t_process creator, but X doesn't want to know about them.
Preparing for dependency injection: It might be possible to specialize these factories to return mocks, instead of real t_process. That could be achieved in a few ways, but not exactly as written.
Specialized object creation: (This is really just the general case for the previous two), there may be specializations of t_process that get created in different circumstances - for example, it might create different t_process types based on environmental variables or operating system. This would require specializations of the factory.
If it were me, and none of these sound plausible, I'd probably rip it out, as it seems like gratuitous design pattern usage.
This look like he is using the factory design pattern to create new instances of t_process. This will allow you to delegate the responsibility of creating different types of t_process away from class X
Well, in this case it doesn't make much sense, unless the author expects the default factory's definition will be updated sometime in the future. It would make sense, though, if the factory object were passed in as a parameter; a factory gives you more flexibility in constructing an object, but if you instantiate the factory at the same place that you use it, then it really doesn't provide an advantage. So, you're right.

Private members vs temporary variables in C++

Suppose you have the following code:
int main(int argc, char** argv) {
Foo f;
while (true) {
f.doSomething();
}
}
Which of the following two implementations of Foo are preferred?
Solution 1:
class Foo {
private:
void doIt(Bar& data);
public:
void doSomething() {
Bar _data;
doIt(_data);
}
};
Solution 2:
class Foo {
private:
Bar _data;
void doIt(Bar& data);
public:
void doSomething() {
doIt(_data);
}
};
In plain english: if I have a class with a method that gets called very often, and this method defines a considerable amount of temporary data (either one object of a complex class, or a large number of simple objects), should I declare this data as private members of the class?
On the one hand, this would save the time spent on constructing, initializing and destructing the data on each call, improving performance. On the other hand, it tramples on the "private member = state of the object" principle, and may make the code harder to understand.
Does the answer depend on the size/complexity of class Bar? What about the number of objects declared? At what point would the benefits outweigh the drawbacks?
From a design point of view, using temporaries is cleaner if that data is not part of the object state, and should be preferred.
Never make design choices on performance grounds before actually profiling the application. You might just discover that you end up with a worse design that is actually not any better than the original design performance wise.
To all the answers that recommend to reuse objects if construction/destruction cost is high, it is important to remark that if you must reuse the object from one invocation to another, in many cases the object must be reset to a valid state between method invocations and that also has a cost. In many such cases, the cost of resetting can be comparable to construction/destruction.
If you do not reset the object state between invocations, the two solutions could yield different results, as in the first call, the argument would be initialized and the state would probably be different between method invocations.
Thread safety has a great impact on this decision also. Auto variables inside a function are created in the stack of each of the threads, and as such are inherently thread safe. Any optimization that pushes those local variable so that it can be reused between different invocations will complicate thread safety and could even end up with a performance penalty due to contention that can worsen the overall performance.
Finally, if you want to keep the object between method invocations I would still not make it a private member of the class (it is not part of the class) but rather an implementation detail (static function variable, global in an unnamed namespace in the compilation unit where doOperation is implemented, member of a PIMPL...[the first 2 sharing the data for all objects, while the latter only for all invocations in the same object]) users of your class do not care about how you solve things (as long as you do it safely, and document that the class is not thread safe).
// foo.h
class Foo {
public:
void doOperation();
private:
void doIt( Bar& data );
};
// foo.cpp
void Foo::doOperation()
{
static Bar reusable_data;
doIt( reusable_data );
}
// else foo.cpp
namespace {
Bar reusable_global_data;
}
void Foo::doOperation()
{
doIt( reusable_global_data );
}
// pimpl foo.h
class Foo {
public:
void doOperation();
private:
class impl_t;
boost::scoped_ptr<impl_t> impl;
};
// foo.cpp
class Foo::impl_t {
private:
Bar reusable;
public:
void doIt(); // uses this->reusable instead of argument
};
void Foo::doOperation() {
impl->doIt();
}
First of all it depends on the problem being solved. If you need to persist the values of temporary objects between calls you need a member variable. If you need to reinitialize them on each invokation - use local temporary variables. It a question of the task at hand, not of being right or wrong.
Temporary variables construction and destruction will take some extra time (compared to just persisting a member variable) depending on how complex the temporary variables classes are and what their constructors and destructors have to do. Deciding whether the cost is significant should only be done after profiling, don't try to optimize it "just in case".
I'd declare _data as temporary variable in most cases. The only drawback is performance, but you'll get way more benefits. You may want to try Prototype pattern if constructing and destructing are really performance killers.
If it is semantically correct to preserve a value of Bar inside Foo, then there is nothing wrong with making it a member - it is then that every Foo has-a bar.
There are multiple scenarios where it might not be correct, e.g.
if you have multiple threads performing doSomething, would they need all separate Bar instances, or could they accept a single one?
would it be bad if state from one computation carries over to the next computation.
Most of the time, issue 2 is the reason to create local variables: you want to be sure to start from a clean state.
Like a lot of coding answers it depends.
Solution 1 is a lot more thread-safe. So if doSomething were being called by many threads I'd go for Solution 1.
If you're working in a single threaded environment and the cost of creating the Bar object is high, then I'd go for Solution 2.
In a single threaded env and if the cost of creating Bar is low, then I think i'd go for Solution 1.
You have already considered "private member=state of the object" principle, so there is no point in repeating that, however, look at it in another way.
A bunch of methods, say a, b, and c take the data "d" and work on it again and again. No other methods of the class care about this data. In this case, are you sure a, b and c are in the right class?
Would it be better to create another smaller class and delegate, where d can be a member variable? Such abstractions are difficult to think of, but often lead to great code.
Just my 2 cents.
Is that an extremely simplified example? If not, what's wrong with doing it this
void doSomething(Bar data);
int main() {
while (true) {
doSomething();
}
}
way? If doSomething() is a pure algorithm that needs some data (Bar) to work with, why would you need to wrap it in a class? A class is for wrapping a state (data) and the ways (member functions) to change it.
If you just need a piece of data then use just that: a piece of data. If you just need an algorithm, then use a function. Only if you need to keep a state (data values) between invocations of several algorithms (functions) working on them, a class might be the right choice.
I admit that the borderlines between these are blurred, but IME they make a good rule of thumb.
If it's really that temporary that costs you the time, then i would say there is nothing wrong with including it into your class as a member. But note that this will possibly make your function thread-unsafe if used without proper synchronization - once again, this depends on the use of _data.
I would, however, mark such a variable as mutable. If you read a class definition with a member being mutable, you can immediately assume that it doesn't account for the value of its parent object.
class Foo {
private:
mutable Bar _data;
private:
void doIt(Bar& data);
public:
void doSomething() {
doIt(_data);
}
};
This will also make it possible to use _data as a mutable entity inside a const function - just like you could use it as a mutable entity if it was a local variable inside such a function.
If you want Bar to be initialised only once (due to cost in this case). Then I'd move it to a singleton pattern.