Skip to content

Commit

Permalink
Propose a typedefs header for paddle frameworks.
Browse files Browse the repository at this point in the history
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
reyoung committed Jun 23, 2017
1 parent 8b86624 commit e6ab599
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions paddle/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cc_library(ddim SRCS ddim.cc)
cc_test(ddim_test SRCS ddim_test.cc DEPS ddim)

nv_test(dim_test SRCS dim_test.cu DEPS ddim)
cc_test(typedefs_test SRCS typedefs_test.cc)
58 changes: 58 additions & 0 deletions paddle/framework/typedefs.h
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
26 changes: 26 additions & 0 deletions paddle/framework/typedefs_test.cc
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;
}

0 comments on commit e6ab599

Please sign in to comment.