Skip to content

Commit 9a148e2

Browse files
committed
code styling refactor changes - part1
1 parent 0a8e7e9 commit 9a148e2

File tree

9 files changed

+972
-696
lines changed

9 files changed

+972
-696
lines changed

CITATION.cff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ message: "If you use this software, please cite it as below."
33
authors:
44
- family-names: "Izquierdo"
55
given-names: "Sergio"
6-
orcid: "https://orcid.org/0000-0000-0000-0000"
6+
orcid: "https://orcid.org/0000-0002-5639-5035"
77
title: "cppflow"
88
version: 1.0.0
99
doi: 10.5281/zenodo.1234
1010
date-released: 2019-05-16
11-
url: "https://github.com/sguttikon/cppflow"
11+
url: "https://github.com/serizba/cppflow"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ You can also use the [older version](https://github.com/serizba/cppflow/tree/243
7676

7777
## Style guide
7878

79-
We use the [Google's C++ style guide](https://google.github.io/styleguide/cppguide.html).
79+
We use the [Google's C++ style guide](https://google.github.io/styleguide/cppguide.html) using static code linker [cpplint](https://github.com/cpplint/cpplint).
8080

8181
## Remark
8282

include/cppflow/context.h

Lines changed: 97 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,121 @@
1+
// MIT License
12
//
2-
// Created by serizba on 27/6/20.
3+
// Copyright (c) 2020 Sergio Izquierdo
4+
// Copyright (c) 2020 Jiannan Liu
35
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
24+
/*!
25+
* @file context.h
26+
* @author Jiannan Liu
27+
* @author Sergio Izquierdo
28+
* @date @showdate "%B %d, %Y" 2020-06-27
29+
*/
30+
31+
#ifndef INCLUDE_CPPFLOW_CONTEXT_H_
32+
#define INCLUDE_CPPFLOW_CONTEXT_H_
33+
34+
// C headers
35+
#include <tensorflow/c/c_api.h>
36+
#include <tensorflow/c/eager/c_api.h>
437

5-
#ifndef CPPFLOW2_CONTEXT_H
6-
#define CPPFLOW2_CONTEXT_H
7-
38+
// C++ headers
839
#include <memory>
940
#include <stdexcept>
1041
#include <utility>
1142

12-
#include <tensorflow/c/c_api.h>
13-
#include <tensorflow/c/eager/c_api.h>
14-
1543
namespace cppflow {
1644

17-
inline bool status_check(TF_Status* status) {
18-
if (TF_GetCode(status) != TF_OK) {
19-
throw std::runtime_error(TF_Message(status));
20-
}
21-
return true;
22-
}
45+
inline bool status_check(TF_Status* status) {
46+
if (TF_GetCode(status) != TF_OK) {
47+
throw std::runtime_error(TF_Message(status));
48+
}
49+
return true;
50+
}
2351

24-
class context {
25-
public:
26-
static TFE_Context* get_context();
52+
class context {
53+
public:
54+
explicit context(TFE_ContextOptions* opts = nullptr);
2755

28-
// only use get_status() for eager ops
29-
static TF_Status* get_status();
56+
context(context const&) = delete;
57+
context(context&&) noexcept;
3058

31-
private:
32-
TFE_Context* tfe_context{nullptr};
59+
~context();
3360

34-
public:
35-
explicit context(TFE_ContextOptions* opts = nullptr);
61+
context& operator=(context const&) = delete;
62+
context& operator=(context&&) noexcept;
3663

37-
context(context const&) = delete;
38-
context& operator=(context const&) = delete;
39-
context(context&&) noexcept;
40-
context& operator=(context&&) noexcept;
64+
static TFE_Context* get_context();
4165

42-
~context();
43-
};
66+
// only use get_status() for eager ops
67+
static TF_Status* get_status();
4468

45-
// TODO: create ContextManager class if needed
46-
// Set new context, thread unsafe, must be called at the beginning.
47-
// TFE_ContextOptions* tfe_opts = ...
48-
// cppflow::get_global_context() = cppflow::context(tfe_opts);
49-
inline context& get_global_context() {
50-
static context global_context;
51-
return global_context;
52-
}
69+
private:
70+
TFE_Context* tfe_context{nullptr};
71+
}; // Class context
5372

73+
// @todo create ContextManager class if needed
74+
// Set new context, thread unsafe, must be called at the beginning.
75+
// TFE_ContextOptions* tfe_opts = ...
76+
// cppflow::get_global_context() = cppflow::context(tfe_opts);
77+
inline context& get_global_context() {
78+
static context global_context;
79+
return global_context;
5480
}
81+
} // namespace cppflow
5582

5683
namespace cppflow {
5784

58-
inline TFE_Context* context::get_context() {
59-
return get_global_context().tfe_context;
60-
}
61-
62-
inline TF_Status* context::get_status() {
63-
thread_local std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)> local_tf_status(TF_NewStatus(), &TF_DeleteStatus);
64-
return local_tf_status.get();
65-
}
66-
67-
inline context::context(TFE_ContextOptions* opts) {
68-
auto tf_status = context::get_status();
69-
if(opts == nullptr) {
70-
std::unique_ptr<TFE_ContextOptions, decltype(&TFE_DeleteContextOptions)> new_opts(TFE_NewContextOptions(), &TFE_DeleteContextOptions);
71-
this->tfe_context = TFE_NewContext(new_opts.get(), tf_status);
72-
} else {
73-
this->tfe_context = TFE_NewContext(opts, tf_status);
74-
}
75-
status_check(tf_status);
76-
}
77-
78-
inline context::context(context&& ctx) noexcept :
79-
tfe_context(std::exchange(ctx.tfe_context, nullptr))
80-
{
81-
}
82-
83-
inline context& context::operator=(context&& ctx) noexcept {
84-
tfe_context = std::exchange(ctx.tfe_context, tfe_context);
85-
return *this;
86-
}
87-
88-
inline context::~context() {
89-
TFE_DeleteContext(this->tfe_context);
90-
}
85+
inline TFE_Context* context::get_context() {
86+
return get_global_context().tfe_context;
87+
}
88+
89+
inline TF_Status* context::get_status() {
90+
thread_local std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)>
91+
local_tf_status(TF_NewStatus(), &TF_DeleteStatus);
92+
return local_tf_status.get();
93+
}
9194

95+
inline context::context(TFE_ContextOptions* opts) {
96+
auto tf_status = context::get_status();
97+
if (opts == nullptr) {
98+
std::unique_ptr<TFE_ContextOptions, decltype(&TFE_DeleteContextOptions)>
99+
new_opts(TFE_NewContextOptions(), &TFE_DeleteContextOptions);
100+
this->tfe_context = TFE_NewContext(new_opts.get(), tf_status);
101+
} else {
102+
this->tfe_context = TFE_NewContext(opts, tf_status);
103+
}
104+
status_check(tf_status);
92105
}
93106

94-
#endif //CPPFLOW2_CONTEXT_H
107+
inline context::context(context&& ctx) noexcept
108+
: tfe_context(std::exchange(ctx.tfe_context, nullptr)) {}
109+
110+
inline context& context::operator=(context&& ctx) noexcept {
111+
tfe_context = std::exchange(ctx.tfe_context, tfe_context);
112+
return *this;
113+
}
114+
115+
inline context::~context() {
116+
TFE_DeleteContext(this->tfe_context);
117+
}
118+
119+
} // namespace cppflow
120+
121+
#endif // INCLUDE_CPPFLOW_CONTEXT_H_

include/cppflow/cppflow.h

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,69 @@
1+
// MIT License
12
//
2-
// Created by serizba on 17/9/20.
3+
// Copyright (c) 2020 Sergio Izquierdo
4+
// Copyright (c) 2020 Jiannan Liu
35
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
423

5-
#ifndef EXAMPLE_CPPFLOW_H
6-
#define EXAMPLE_CPPFLOW_H
24+
/*!
25+
* @file cppflow.h
26+
* @author Jiannan Liu
27+
* @author Sergio Izquierdo
28+
* @date @showdate "%B %d, %Y" 2020-09-17
29+
*/
730

8-
#include "tensor.h"
9-
#include "model.h"
10-
#include "raw_ops.h"
11-
#include "ops.h"
12-
#include "datatype.h"
31+
#ifndef INCLUDE_CPPFLOW_CPPFLOW_H_
32+
#define INCLUDE_CPPFLOW_CPPFLOW_H_
1333

34+
// C headers
1435
#include <tensorflow/c/c_api.h>
1536

37+
// C++ headers
38+
#include <string>
39+
40+
// CppFlow headers
41+
#include "cppflow/datatype.h"
42+
#include "cppflow/model.h"
43+
#include "cppflow/ops.h"
44+
#include "cppflow/raw_ops.h"
45+
#include "cppflow/tensor.h"
46+
1647
namespace cppflow {
1748

18-
/**
19-
* Version of TensorFlow and CppFlow
20-
* @return A string containing the version of TensorFow and CppFlow
21-
*/
22-
std::string version();
49+
/**
50+
* Version of TensorFlow and CppFlow
51+
* @return A string containing the version of TensorFow and CppFlow
52+
*/
53+
std::string version();
2354

24-
}
55+
} // namespace cppflow
2556

2657
/******************************
2758
* IMPLEMENTATION DETAILS *
2859
******************************/
2960

3061
namespace cppflow {
31-
inline std::string version() {
32-
return "TensorFlow: " + std::string(TF_Version()) + " CppFlow: 2.0.0";
33-
}
62+
63+
inline std::string version() {
64+
return "TensorFlow: " + std::string(TF_Version()) + " CppFlow: 2.0.0";
3465
}
3566

36-
#endif //EXAMPLE_CPPFLOW_H
67+
} // namespace cppflow
68+
69+
#endif // INCLUDE_CPPFLOW_CPPFLOW_H_

0 commit comments

Comments
 (0)