The any function implemented by this person is sufficient, but the code is not very readable. In fact, I am thinking, is it really necessary to implement so many functions? We also developed a policy mode...
Based on several ideas:
Any is just a container. The variable type stored in it should have the ability to manage the memory independently.
Operator = of variable type is perfect, such as class A {}; A B; A a = B (almost all types are perfect by default, some custom classes are well-rounded up after being redefined =. If you do not have custom classes =, leading to memory leakage, it is your responsibility to design your own classes, should not be considered by any)
Char *, a traditional string type, is implemented by the character pointer (this is used by yourself, and C ++ should remove it). stl: string can work normally at www.2cto.com.
The runtime type check function is not provided (you need to ensure that the any type is correct, 1 is because, for example, the iar compiler does not support RTTI, the old compiler does not support type derivation of C ++ 11, and C ++ programming is dominated by static programming ideas, if you forget what type you are using, you should not be responsible for any. If your compiler supports RTTI or C ++ 11, you can easily add a dynamic type check function, because the code is extremely simple and easy to understand)
Namespace boostStandalone {
Namespace anyimpl {
Class policyBase {}; // an empty base class used as a pointer and can point to a large number of classes after instantiation (void * is the only option without this)
Template <typename T>
Class policy: public policyBase {// The idea here is static polymorphism (instead of using virtual, instead of template) to generate a series of policies for Different Types
Public:
T & get_value () {return mValue ;}
Void assign (const T & _ object) {mValue = _ object ;}
Private:
T mValue;
};
}
Class any {
Public:
Any (): policyPtr (NULL) {}// NULL Constructor
Any (any & x) {x. releasePolicy () ;}// copy the constructor. Here, the idea of the copy structure is ownership transfer, and the original any is destroyed (the resource is transferred ). (Because I think the copy construction is generally used in function calls, the original any resource should be reclaimed. The disadvantage is that the copy constructor cannot be modified using const)
Template <typename T>
Any & operator = (const T & x) {// value assignment Structure
If (policyPtr ){
Delete policyPtr;
}
PolicyPtr = new anyimpl: policy <T>;
(Static_cast <anyimpl: policy <T> *> (policyPtr)-> assign (x );
Return * this;
}
Template <typename T>
T cast () {// not throw an exception like that of the buddy, because as mentioned above, this function will be faithfully executed based on the type you gave. Only when you give it the right way (for example, you have to use the uint8 type to represent uint32, no one can help .)
Return static_cast <anyimpl: policy <T> *> (policyPtr)-> get_value ();
}
Bool ifEmpty (){
Return policyPtr = NULL;
}
~ Any (){
If (policyPtr ){
Delete policyPtr;
}
}
Private:
Void releasePolicy () {policyPtr = NULL;} // release the resource. In this way, the policy resource will not be released during the any destructor, which is used for resource ownership transfer, that is, copy construction.
Anyimpl: policyBase * policyPtr;
};
};
The above is my implementation. I think so. Like the boost library, it is really powerful, everything is perfect, everything needs to be done with all functions, everything is possible, and everything has to be checked for errors. If so much code is used, the Compilation Time will not be long. What should I do... Referring to the code of the buddy on the website, it is not easy to read (it will be easier to understand the policy). Let's look back and think about it. Is that really necessary?
The implementation I have provided for this problem is described above. I used it myself. Although this implementation has few functions, most any functions can be used,