policy-based-design

Policy based design and best practices - C++

允我心安 提交于 2020-06-23 22:26:09
问题 struct InkPen { void Write() { this->WriteImplementation(); } void WriteImplementation() { std::cout << "Writing using a inkpen" << std::endl; } }; struct BoldPen { void Write() { std::cout << "Writing using a boldpen" << std::endl; } }; template<class PenType> class Writer : public PenType { public: void StartWriting() { PenType::Write(); } }; int main() { Writer<InkPen> writer; writer.StartWriting(); Writer<BoldPen> writer1; writer1.StartWriting(); return 0; } I wrote the above code as part

What are the measures to call a Python code a policy-based design?

偶尔善良 提交于 2020-01-03 17:23:28
问题 Description I wonder if the code I am showing can be considered as an example of policy-based design in Python. Also, I would like to know if have you seen python modules using something like this example so I can learn from them? I wrote more details and examples about this approach in a post. Recently I needed something like policy-based design for a python module I was working on. I found a similar question in this forum, but it was closed, and I was not able to add a comment. Let me give

Mechanics of multiple inheritance compared to templates wrt building flexible designs

旧街凉风 提交于 2019-12-23 15:19:02
问题 This is a narrower version of the question put on hold due to being too broad. On pages 6-7 of Modern C++ Design , Andrei Alexandrescu lists three ways in which the multiple inheritance is weaker than templates with respect to building flexible designs. In particular, he states that the mechanics provided by multiple inheritance is poor (the text in square brackets and formatting are mine as per my understanding of the context): In such a setting [i.e. multiple inheritance ], [to build a

Partial specialization for a parent of multiple classes

别说谁变了你拦得住时间么 提交于 2019-12-22 07:07:14
问题 I want to use partial specialization for a template class so that all children of that template class will use that specialization. Let me explain it with an example :) template < typename T, unsigned int rows, unsigned int cols> class BaseMatrix {...}; This class will have children that specify the structure of the matrix, like sparse,dense,diagonal,.. template < typename T, unsigned int rows, unsigned int cols> class DiagonalMatrix : public BaseMatrix<T,rows,cols>{..} Then those classes

Policy based design in Python [closed]

非 Y 不嫁゛ 提交于 2019-12-13 10:57:06
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I was very impressed by the policy based design described in Modern C++ Design by Andrei Alexandrescu and tried it successfully in some light weight programs. Now I have to write a real world system in Python and I think that approach would be very useful here. However, I can't find a single

Policy classes with differing interfaces

£可爱£侵袭症+ 提交于 2019-12-07 18:59:39
问题 Suppose an algorithm that has a policy FooPolicy . Policy classes that implement this policy feature a static member function foo , but, for some of them, foo takes an int argument, while for others it does not. I am trying to enable the use of these policy classes with differing interfaces by means of constexpr static data members: struct SimpleFoo { static constexpr bool paramFlag = false; static void foo() { std::cout << "In SimpleFoo" << std::endl; } }; struct ParamFoo { static constexpr

Policy classes with differing interfaces

拜拜、爱过 提交于 2019-12-06 08:26:32
Suppose an algorithm that has a policy FooPolicy . Policy classes that implement this policy feature a static member function foo , but, for some of them, foo takes an int argument, while for others it does not. I am trying to enable the use of these policy classes with differing interfaces by means of constexpr static data members: struct SimpleFoo { static constexpr bool paramFlag = false; static void foo() { std::cout << "In SimpleFoo" << std::endl; } }; struct ParamFoo { static constexpr bool paramFlag = true; static void foo(int param) { std::cout << "In ParamFoo " << param << std::endl;

Understanding the exposition of Alexandrescu about the weaknesses of multiple inheritance [closed]

☆樱花仙子☆ 提交于 2019-12-04 11:02:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . UPDATE: I have asked a narrower question here. On pages 6-7 of Modern C++ Design , Andrei Alexandrescu gives a very fundamental discussion of the strengths and weaknesses of two C++ language features -- multiple inheritance and templates -- with respect to building flexible

What is the difference between a trait and a policy?

你。 提交于 2019-12-03 01:35:56
问题 I have a class whose behavior I am trying to configure. template<int ModeT, bool IsAsync, bool IsReentrant> ServerTraits; Then later on I have my server object itself: template<typename TraitsT> class Server {...}; My question is for my usage above is my naming misnamed? Is my templated parameter actually a policy instead of a trait? When is a templated argument a trait versus a policy? 回答1: Policies Policies are classes (or class templates) to inject behavior into a parent class, typically

What is the difference between a trait and a policy?

五迷三道 提交于 2019-12-02 13:51:53
I have a class whose behavior I am trying to configure. template<int ModeT, bool IsAsync, bool IsReentrant> ServerTraits; Then later on I have my server object itself: template<typename TraitsT> class Server {...}; My question is for my usage above is my naming misnamed? Is my templated parameter actually a policy instead of a trait? When is a templated argument a trait versus a policy? Policies Policies are classes (or class templates) to inject behavior into a parent class, typically through inheritance. Through decomposing a parent interface into orthogonal (independent) dimensions, policy