C++ Loki Smart Pointer Threading Model and Checking Policy














































C++ Loki Smart Pointer Threading Model and Checking Policy



SMART POINTER THREADING MODEL & CHECKING POLICY

CODE:

template <class T> struct NoChecking // Checking Policy { static void Check(T*) {} }; template <class T> struct EnforceNotNull //Checking Policy { class NullPointerException : public std::exception { ... }; static void Check(T* ptr) { if (!ptr) throw NullPointerException(); } }; template <class T> struct EnsureNotNull { static void Check(T*& ptr) { if (!ptr) ptr = GetDefaultValue(); } }; template<class T,template <class> class CheckingPolicy, template <class> class ThreadingModel> class SmartPtr : public CheckingPolicy<T> , public ThreadingModel<SmartPtr> { ... T* operator->() { typename ThreadingModel<SmartPtr>::Lock guard(*this); CheckingPolicy<T>::Check(pointee_); return pointee_; } private: T* pointee_; };

EXPLANATION:
This an example of conflating policies. It is one of the most useful feature in designing. It is highly customizable and several behavior aspects of design can be formulated with ease. The Smart Pointer has two design implementations with two policies: Threading model and Checking policy before dereferencing the SmartPtr. The template has three arguments: a pointer of class T, Threading Model and Checking Policy. Implementing several policies the SmartPointer could be defined with typedef. The two policies are:

Checking Policy: This policy introduces a check function with value of class T pointer ( T*). The SmartPtr invokes the Check member function. The template is CheckingPolicy<T>.

Threading Model: It includes a inner type function Lock, of type of class T reference (T&). The template would be ThreadingModel(T).

There are two types of Checking Polices included, NoChecking and EnforceNotNull. The types of threading models are SingleThreaded and MultiThreaded. The beauty of combining the policies is that SmartPtr::Operator-> behaves differently on two extraneous dimensions. As we can notice, it covers a huge band of combinatorial behavior in a small code snippet.

More Articles of Abhinav Verma:

Name Views Likes
C++ Loki Abstract Factory Implementation 575 1
C++ Loki SmallObject Compiler Working 403 1
C++ Loki SmallObject 410 1
C++ Loki Abstract Factory Interface 470 1
C++ Loki Double Dispatching 350 2
C++ Loki SmallObjAllocator 370 2
C++ Loki Abstract Factory 353 2
C++ Loki Small Objects Allocation Methods 293 1
C++ Loki The Fixed Size Allocator 442 1
C++ Loki Necessity of Multi-Methods 255 1
C++ Loki Chunks 345 1
C++ Loki Packing Order of Small Object Allocator 275 2
C++ Loki Multimethods Polymorphism 285 2
C++ Loki How does memory allocator works? 275 1
C++ Loki Default Storage Allocator 271 1
C++ Loki Multimethods 348 1
C++ Loki Small Object Allocator 357 1
C++ Loki Techniques Compile Time Affirmations 262 1
C++ Loki Typelist Linear Hierarchy 266 1
C++ Loki Techniques type_info 239 1
C++ Loki Traits Generating Tuples 238 1
C++ Loki Techniques Partial Template Specialization 245 1
C++ Loki Typelist Generating Scattered Hierarchy 307 1
C++ Loki Techniques Select 247 1
C++ Loki Clubbing TypeList 235 1
C++ Loki Techniques Traits 220 1
C++ Loki TypeList Substituting Elements 236 1
C++ Loki Techniques Local Classes 222 1
C++ Loki TypeList Deleting Duplicated 212 1
C++ Loki Techniques Introduction 216 1
C++ Loki Effacing type from typelist 245 1
C++ Loki Append TypeList 274 1
C++ Loki Mouldering Classes 224 1
C++ Loki Searching TypeList 219 1
C++ Loki Smart Pointer Compatibility of Policies 241 1
C++ Loki TypeList Indexed Access 236 1
C++ Loki Smart Pointer Structure Customization 230 1
C++ Loki Length of TypeList 237 1
C++ Loki Smart Pointer Threading Model and Checking Policy 230 1
C++ Loki Voluntary Functionality through Incomplete Initialization 201 1
C++ Loki Policy Class Destructor 244 1
C++ Loki Enriched Policies 215 1
C++ Loki Policy Classes with Template Template Parameter and Member functions 219 1
C++ Loki Kernel of Policy Classes 221 1
C++ Loki Templates in design 227 1
C++ Loki Do-it-All Interface & Multiple Inheritance 230 1
C++ Loki Policy Based Design 288 2
C++ Loki Introduction 310 2
C++ iomanip ends 284 1
C++ iomanip flush 273 2
C++ iomanip ws 306 2

Comments