forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propose a typedefs header for paddle frameworks.
The original discuess in * PaddlePaddle#2548 (comment) * PaddlePaddle#2579 (comment) This commit is just a proposal, let's do such kind of summarization in this PR.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include <boost/noncopyable.hpp> | ||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
//! Using unordered_map as Paddle's default map. It will faster than | ||
//! std::map | ||
template <typename Key, typename Val> | ||
using Map = std::unordered_map<Key, Val>; | ||
|
||
//! Using unordered_set as Paddle's default set. It will faster than | ||
//! std::set | ||
template <typename T> | ||
using Set = std::unordered_set<T>; | ||
|
||
//! Default Vector is std::vector | ||
template <typename T> | ||
using Vector = std::vector<T>; | ||
|
||
//! Default String is std::string | ||
using String = std::string; | ||
|
||
//! Default unique ptr is std::unique_ptr | ||
template <typename T> | ||
using UniquePtr = std::unique_ptr<T>; | ||
|
||
//! Default shared ptr is std::shared_ptr | ||
template <typename T> | ||
using SharedPtr = std::shared_ptr<T>; | ||
|
||
//! Default weak ptr is std::weak_ptr | ||
template <typename T> | ||
using WeakPtr = std::weak_ptr<T>; | ||
|
||
//! MakeShared will create std::shared_ptr | ||
template <typename T, typename... ARGS> | ||
inline SharedPtr<T> MakeShared(ARGS... args) { | ||
return std::make_shared<T>(std::forward<ARGS>(args)...); | ||
} | ||
|
||
//! MakeUnique will create std::unique_ptr | ||
template <typename T, typename... ARGS> | ||
inline UniquePtr<T> MakeUnique(ARGS... args) { | ||
return UniquePtr<T>(new T(std::forward<ARGS>(args)...)); | ||
} | ||
|
||
//! Using boost::noncopyable as non-copyable markup. | ||
using NonCopyable = boost::noncopyable; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <gtest/gtest.h> | ||
#include <paddle/framework/typedefs.h> | ||
|
||
template <typename Function> | ||
inline void TestSmartPtrImpl(int val, Function func) { | ||
auto tmp = func(); | ||
*tmp = val; | ||
ASSERT_TRUE(tmp); | ||
ASSERT_EQ(val, *tmp); | ||
} | ||
|
||
TEST(Typedefs, SharedPtr) { | ||
TestSmartPtrImpl(32, paddle::framework::MakeShared<int>); | ||
} | ||
|
||
TEST(Typedefs, UniquePtr) { | ||
TestSmartPtrImpl(64, paddle::framework::MakeUnique<int>); | ||
} | ||
|
||
struct TestNonCopy : public paddle::framework::NonCopyable {}; | ||
|
||
TEST(Typedefs, NonCopyAble) { | ||
// following lines will make compiler error. | ||
// TestNonCopy a; | ||
// TestNonCopy b = a; | ||
} |