-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Variable Design. #2586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable Design. #2586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Design doc of `Variable` | ||
|
|
||
| ## Overview | ||
| `Variable` is a general container that hosts a typed pointer. The main function of `Variable` is to hold a single value of any type. We summary some necessary features which should be satisfied by `Variable`: | ||
|
|
||
| * Accept external data pointer of any type | ||
| * Get typed data | ||
| * Destroy data in destructor | ||
|
|
||
| ## Prototype | ||
|
|
||
| Two aspects should be considered when designing `Variable`, the first is interactive interfaces with external data, the second is type maintaining. Here, we use `boost::any` to hold data and its type. | ||
|
|
||
| ```cpp | ||
| Class Variable { | ||
| public: | ||
| // Default and only constructor. | ||
| // Variable should be constructed without any parameter, | ||
| // the data pointer must be passed by calling Reset. | ||
| Variable(); | ||
| ~Variable(); | ||
|
|
||
| // Pass data pointer to Variable. | ||
| // When passing nullptr, just deconstruct hosted data. | ||
| // Otherwise deconstruct original data and host the passed data | ||
| // and init the destructor pointer. | ||
| template <calss T> | ||
| void Reset(T* ptr = nullptr); | ||
|
|
||
| // Get read-only typed object reference. | ||
| // The type checking will be done implicitly. | ||
| template <class T> | ||
| const T& Get() const; | ||
|
|
||
| // Get mutable pointer to the hosted data. | ||
| // If the value is nullptr, create a new object, then return it. And | ||
| // the type T should have a default constructor. | ||
| // The `is_new_obj` can use be used to determine whether the returned pointer | ||
| // is a new object. | ||
| template <class T> | ||
| T* GetMutable(bool* is_new_obj=nullptr); | ||
|
|
||
| privated: | ||
| // A typed data pointer, set the type when calling Reset. | ||
| boost::any value_; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we are going to use boost::any for both saving the pointer and the type information (caffe2::TypeMeta)? I know that boost::any has a method type(), which returns a typeid comparable with typeid(TypeName). But how could we connect the typeid with a deleter? |
||
|
|
||
| // A destroy call. | ||
| template <class T> | ||
| static void Destroy(void* pointer); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In C++, there are two concepts related with here -- destruction and delete. Let us not invent a name destroy, but just call it deleter, as in shared_ptr::shared_ptr. |
||
|
|
||
| // A pointer to save the destructor. | ||
| typedef void (*DestroyCall)(void *); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we represent value_ by boost::any, I think we are not going to use void* in type DestroyCall? How about std::function<void(boost::any&)> deleter_; |
||
| DestroyCall destroy_ = nullptr; | ||
| }; | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Here, we give several usages. | ||
|
|
||
| 1. New a `Variable` and set a tensor. | ||
|
|
||
| ```cpp | ||
| Tensor t = new Tensor(); | ||
| Variable var = new Variable(); | ||
| var.Reset(t); | ||
| ``` | ||
|
|
||
| 2. New a `Variable` and get a tensor by the `GetMutable` interface. | ||
|
|
||
| ```cpp | ||
| Variable var = new Variable(); | ||
| auto t = var.GetMutable<Tensor>(); | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class ==> class