Skip to content

Commit 2a78244

Browse files
author
Dane Springmeyer
committed
Reformat code with 'BreakBeforeBraces: Attach'
1 parent 3958e53 commit 2a78244

8 files changed

Lines changed: 59 additions & 128 deletions

File tree

src/module.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// "target" is a magic var that nodejs passes into a module's scope.
99
// When you write things to target, they become available to call from
1010
// Javascript world.
11-
static void init(v8::Local<v8::Object> target)
12-
{
11+
static void init(v8::Local<v8::Object> target) {
1312

1413
// expose hello method
1514
Nan::SetMethod(target, "hello", standalone::hello);

src/module_utils.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ namespace utils {
2121
*
2222
*/
2323
inline void CallbackError(std::string message,
24-
v8::Local<v8::Function> callback)
25-
{
24+
v8::Local<v8::Function> callback) {
2625
v8::Local<v8::Value> argv[1] = {Nan::Error(message.c_str())};
2726
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
2827
}

src/object_async/hello_async.cpp

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,15 @@ HelloObjectAsync::HelloObjectAsync(std::string&& name)
4646
: name_(std::move(name)) {}
4747

4848
// Triggered from Javascript world when calling "new HelloObjectAsync(name)"
49-
NAN_METHOD(HelloObjectAsync::New)
50-
{
51-
if (info.IsConstructCall())
52-
{
53-
try
54-
{
55-
if (info.Length() >= 1)
56-
{
57-
if (info[0]->IsString())
58-
{
49+
NAN_METHOD(HelloObjectAsync::New) {
50+
if (info.IsConstructCall()) {
51+
try {
52+
if (info.Length() >= 1) {
53+
if (info[0]->IsString()) {
5954
// Don't want to risk passing a null string around, which might create unpredictable behavior.
6055
Nan::Utf8String utf8_value(info[0]);
6156
int len = utf8_value.length();
62-
if (len <= 0)
63-
{
57+
if (len <= 0) {
6458
return Nan::ThrowTypeError("arg must be a non-empty string");
6559
}
6660

@@ -91,28 +85,20 @@ NAN_METHOD(HelloObjectAsync::New)
9185
**/
9286
auto* const self = new HelloObjectAsync(std::move(name));
9387
self->Wrap(info.This()); // Connects C++ object to Javascript object (this)
94-
}
95-
else
96-
{
88+
} else {
9789
return Nan::ThrowTypeError(
9890
"arg must be a string");
9991
}
100-
}
101-
else
102-
{
92+
} else {
10393
return Nan::ThrowTypeError(
10494
"must provide string arg");
10595
}
106-
}
107-
catch (const std::exception& ex)
108-
{
96+
} catch (const std::exception& ex) {
10997
return Nan::ThrowTypeError(ex.what());
11098
}
11199

112100
info.GetReturnValue().Set(info.This());
113-
}
114-
else
115-
{
101+
} else {
116102
return Nan::ThrowTypeError(
117103
"Cannot call constructor as function, you need to use 'new' keyword");
118104
}
@@ -121,23 +107,19 @@ NAN_METHOD(HelloObjectAsync::New)
121107
// This function performs expensive allocation of std::map, querying, and string
122108
// comparison, therefore threads are nice & busy.
123109
// Also, notice that name is passed by reference (std::string const& name)
124-
std::string do_expensive_work(bool louder, std::string const& name)
125-
{
110+
std::string do_expensive_work(bool louder, std::string const& name) {
126111

127112
std::map<std::size_t, std::string> container;
128113
std::size_t work_to_do = 100000;
129114

130-
for (std::size_t i = 0; i < work_to_do; ++i)
131-
{
115+
for (std::size_t i = 0; i < work_to_do; ++i) {
132116
container.emplace(i, std::to_string(i));
133117
}
134118

135-
for (std::size_t i = 0; i < work_to_do; ++i)
136-
{
119+
for (std::size_t i = 0; i < work_to_do; ++i) {
137120
std::string const& item = container[i];
138121

139-
if (item != std::to_string(i))
140-
{
122+
if (item != std::to_string(i)) {
141123

142124
// AsyncHelloWorker's Execute function will take care of this error
143125
// and return it to js-world via callback
@@ -150,8 +132,7 @@ std::string do_expensive_work(bool louder, std::string const& name)
150132

151133
std::string result = "...threads are busy async bees...hello " + name;
152134

153-
if (louder)
154-
{
135+
if (louder) {
155136
result += "!!!!";
156137
}
157138

@@ -179,14 +160,10 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
179160
// The Execute() function is getting called when the worker starts to run.
180161
// - You only have access to member variables stored in this worker.
181162
// - You do not have access to Javascript v8 objects here.
182-
void Execute() override
183-
{
184-
try
185-
{
163+
void Execute() override {
164+
try {
186165
result_ = do_expensive_work(louder_, *name_);
187-
}
188-
catch (const std::exception& e)
189-
{
166+
} catch (const std::exception& e) {
190167
SetErrorMessage(e.what());
191168
}
192169
}
@@ -198,8 +175,7 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
198175
// - You have access to Javascript v8 objects again
199176
// - You have to translate from C++ member variables to Javascript v8 objects
200177
// - Finally, you call the user's callback with your results
201-
void HandleOKCallback() override
202-
{
178+
void HandleOKCallback() override {
203179
Nan::HandleScope scope;
204180

205181
const auto argc = 2u;
@@ -220,8 +196,7 @@ struct AsyncHelloWorker : Nan::AsyncWorker // NOLINT to disable cppcoreguideline
220196
const std::string* name_;
221197
};
222198

223-
NAN_METHOD(HelloObjectAsync::helloAsync)
224-
{
199+
NAN_METHOD(HelloObjectAsync::helloAsync) {
225200
// "info" comes from the NAN_METHOD macro, which returns differently according
226201
// to the Node version
227202
// "What is node::ObjectWrap???" The short version is that node::ObjectWrap
@@ -242,28 +217,24 @@ NAN_METHOD(HelloObjectAsync::helloAsync)
242217
// instead of throwing.
243218
// Also, "info" comes from the NAN_METHOD macro, which returns differently
244219
// according to the version of node
245-
if (!info[1]->IsFunction())
246-
{
220+
if (!info[1]->IsFunction()) {
247221
return Nan::ThrowTypeError("second arg 'callback' must be a function");
248222
}
249223
v8::Local<v8::Function> callback = info[1].As<v8::Function>();
250224

251225
// Check first argument, should be an 'options' object
252-
if (!info[0]->IsObject())
253-
{
226+
if (!info[0]->IsObject()) {
254227
return utils::CallbackError("first arg 'options' must be an object",
255228
callback);
256229
}
257230
v8::Local<v8::Object> options = info[0].As<v8::Object>();
258231

259232
// Check options object for the "louder" property, which should be a boolean
260233
// value
261-
if (options->Has(Nan::New("louder").ToLocalChecked()))
262-
{
234+
if (options->Has(Nan::New("louder").ToLocalChecked())) {
263235
v8::Local<v8::Value> louder_val =
264236
options->Get(Nan::New("louder").ToLocalChecked());
265-
if (!louder_val->IsBoolean())
266-
{
237+
if (!louder_val->IsBoolean()) {
267238
return utils::CallbackError("option 'louder' must be a boolean",
268239
callback);
269240
}
@@ -282,14 +253,12 @@ NAN_METHOD(HelloObjectAsync::helloAsync)
282253
}
283254

284255
// Singleton
285-
Nan::Persistent<v8::Function>& HelloObjectAsync::create_once()
286-
{
256+
Nan::Persistent<v8::Function>& HelloObjectAsync::create_once() {
287257
static Nan::Persistent<v8::Function> init;
288258
return init;
289259
}
290260

291-
void HelloObjectAsync::Init(v8::Local<v8::Object> target)
292-
{
261+
void HelloObjectAsync::Init(v8::Local<v8::Object> target) {
293262
// A handlescope is needed so that v8 objects created in the local memory
294263
// space (this function in this case)
295264
// are cleaned up when the function is done running (and the handlescope is

src/object_async/hello_async.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ namespace object_async {
1010
* Also, this class adheres to the rule of Zero because we define no custom
1111
* destructor or copy constructor
1212
*/
13-
class HelloObjectAsync : public Nan::ObjectWrap
14-
{
13+
class HelloObjectAsync : public Nan::ObjectWrap {
1514

1615
public:
1716
// initializer

src/object_sync/hello.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ namespace object_sync {
3232
HelloObject::HelloObject(std::string&& name) : name_(std::move(name)) {}
3333

3434
// Triggered from Javascript world when calling "new HelloObject(name)"
35-
NAN_METHOD(HelloObject::New)
36-
{
37-
if (info.IsConstructCall())
38-
{
39-
try
40-
{
41-
if (info.Length() >= 1)
42-
{
43-
if (info[0]->IsString())
44-
{
35+
NAN_METHOD(HelloObject::New) {
36+
if (info.IsConstructCall()) {
37+
try {
38+
if (info.Length() >= 1) {
39+
if (info[0]->IsString()) {
4540
// Don't want to risk passing a null string around, which might create unpredictable behavior.
4641
Nan::Utf8String utf8_value(info[0]);
4742
int len = utf8_value.length();
48-
if (len <= 0)
49-
{
43+
if (len <= 0) {
5044
return Nan::ThrowTypeError("arg must be a non-empty string");
5145
}
5246

@@ -77,36 +71,27 @@ NAN_METHOD(HelloObject::New)
7771
*/
7872
auto* const self = new HelloObject(std::move(name));
7973
self->Wrap(info.This()); // Connects C++ object to Javascript object (this)
80-
}
81-
else
82-
{
74+
} else {
8375
return Nan::ThrowTypeError(
8476
"arg must be a string");
8577
}
86-
}
87-
else
88-
{
78+
} else {
8979
return Nan::ThrowTypeError(
9080
"must provide string arg");
9181
}
92-
}
93-
catch (const std::exception& ex)
94-
{
82+
} catch (const std::exception& ex) {
9583
return Nan::ThrowTypeError(ex.what());
9684
}
9785

9886
info.GetReturnValue().Set(info.This());
99-
}
100-
else
101-
{
87+
} else {
10288
return Nan::ThrowTypeError(
10389
"Cannot call constructor as function, you need to use 'new' keyword");
10490
}
10591
}
10692

10793
// NAN_METHOD is applicable to methods you want to expose to JS world
108-
NAN_METHOD(HelloObject::hello)
109-
{
94+
NAN_METHOD(HelloObject::hello) {
11095
/**
11196
* Note: a HandleScope is automatically included inside NAN_METHOD. See the
11297
* docs at NAN that say:
@@ -135,14 +120,12 @@ NAN_METHOD(HelloObject::hello)
135120

136121
// This is a Singleton, which is a general programming design concept for
137122
// creating an instance once within a process.
138-
Nan::Persistent<v8::Function>& HelloObject::create_once()
139-
{
123+
Nan::Persistent<v8::Function>& HelloObject::create_once() {
140124
static Nan::Persistent<v8::Function> init;
141125
return init;
142126
}
143127

144-
void HelloObject::Init(v8::Local<v8::Object> target)
145-
{
128+
void HelloObject::Init(v8::Local<v8::Object> target) {
146129
// A handlescope is needed so that v8 objects created in the local memory
147130
// space (this function in this case)
148131
// are cleaned up when the function is done running (and the handlescope is

src/object_sync/hello.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace object_sync {
88
* This is in a header file so we can access it across other .cpp files if necessary
99
* Also, this class adheres to the rule of Zero because we define no custom destructor or copy constructor
1010
*/
11-
class HelloObject : public Nan::ObjectWrap
12-
{
11+
class HelloObject : public Nan::ObjectWrap {
1312

1413
public:
1514
// initializer

src/standalone/hello.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace standalone {
1919
// hello is a "standalone function" because it's not a class.
2020
// If this function was not defined within a namespace, it would be in the
2121
// global scope.
22-
NAN_METHOD(hello)
23-
{
22+
NAN_METHOD(hello) {
2423

2524
// "info" comes from the NAN_METHOD macro, which returns differently
2625
// according to the version of node

0 commit comments

Comments
 (0)