Skip to content

Commit c5fc220

Browse files
smilesa-maurice
authored andcommitted
Added FIREBASE_ARRAYSIZE macro to internal/common.h
Also, moved all compile time array size calculation to the macro. PiperOrigin-RevId: 266401243
1 parent 54eb813 commit c5fc220

File tree

25 files changed

+76
-78
lines changed

25 files changed

+76
-78
lines changed

admob/src/android/admob_android.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "admob/src/include/firebase/admob/types.h"
3434
#include "app/src/assert.h"
3535
#include "app/src/embedded_file.h"
36+
#include "app/src/include/firebase/internal/common.h"
3637
#include "app/src/include/google_play_services/availability.h"
3738
#include "app/src/reference_counted_future_impl.h"
3839
#include "app/src/util_android.h"
@@ -426,18 +427,16 @@ bool RegisterNatives() {
426427
};
427428
JNIEnv* env = GetJNI();
428429
return banner_view_helper::RegisterNatives(
429-
env, kBannerMethods,
430-
sizeof(kBannerMethods) / sizeof(kBannerMethods[0])) &&
430+
env, kBannerMethods, FIREBASE_ARRAYSIZE(kBannerMethods)) &&
431431
interstitial_ad_helper::RegisterNatives(
432432
env, kInterstitialMethods,
433-
sizeof(kInterstitialMethods) / sizeof(kInterstitialMethods[0])) &&
433+
FIREBASE_ARRAYSIZE(kInterstitialMethods)) &&
434434
native_express_ad_view_helper::RegisterNatives(
435435
env, kNativeExpressMethods,
436-
sizeof(kNativeExpressMethods) /
437-
sizeof(kNativeExpressMethods[0])) &&
436+
FIREBASE_ARRAYSIZE(kNativeExpressMethods)) &&
438437
rewarded_video::rewarded_video_helper::RegisterNatives(
439438
env, kRewardedVideoMethods,
440-
sizeof(kRewardedVideoMethods) / sizeof(kRewardedVideoMethods[0]));
439+
FIREBASE_ARRAYSIZE(kRewardedVideoMethods));
441440
}
442441

443442
} // namespace admob

app/src/app_common.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "app/src/callback.h"
3030
#include "app/src/cleanup_notifier.h"
3131
#include "app/src/include/firebase/app.h"
32+
#include "app/src/include/firebase/internal/common.h"
3233
#include "app/src/include/firebase/internal/platform.h"
3334
#include "app/src/include/firebase/version.h"
3435
#include "app/src/mutex.h"
@@ -425,8 +426,7 @@ void GetOuterMostSdkAndVersion(std::string* sdk, std::string* version) {
425426
FIREBASE_CPP_USER_AGENT_PREFIX,
426427
};
427428
LibraryRegistry* registry = LibraryRegistry::Initialize();
428-
for (size_t i = 0; i < sizeof(kLibraryVersions) / sizeof(kLibraryVersions[0]);
429-
++i) {
429+
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(kLibraryVersions); ++i) {
430430
std::string library(kLibraryVersions[i]);
431431
std::string library_version = registry->GetLibraryVersion(library);
432432
if (!library_version.empty()) {

app/src/app_desktop.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "app/src/include/firebase/app.h"
18-
1917
#include <string.h>
18+
2019
#include <fstream>
2120

2221
#include "app/src/app_common.h"
2322
#include "app/src/function_registry.h"
23+
#include "app/src/include/firebase/app.h"
24+
#include "app/src/include/firebase/internal/common.h"
2425
#include "app/src/include/firebase/version.h"
2526
#include "app/src/log.h"
2627
#include "app/src/util.h"
@@ -99,8 +100,8 @@ AppOptions* AppOptions::LoadDefault(AppOptions* options) {
99100
allocated_options = true;
100101
}
101102
std::string config_files;
102-
size_t number_of_config_filenames = sizeof(kDefaultGoogleServicesNames) /
103-
sizeof(kDefaultGoogleServicesNames[0]);
103+
size_t number_of_config_filenames =
104+
FIREBASE_ARRAYSIZE(kDefaultGoogleServicesNames);
104105
for (size_t i = 0; i < number_of_config_filenames; i++) {
105106
std::string full_path = internal::g_default_config_path +
106107
kDefaultGoogleServicesNames[i];

app/src/app_options.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "app/google_services_resource.h"
2424
#include "app/src/assert.h"
2525
#include "app/src/include/firebase/app.h"
26+
#include "app/src/include/firebase/internal/common.h"
2627
#include "app/src/include/firebase/internal/platform.h"
2728
#include "app/src/log.h"
2829
#include "flatbuffers/idl.h"
@@ -158,8 +159,7 @@ AppOptions* AppOptions::LoadFromJsonConfig(const char* config, // NOLINT
158159
// We explicitly ignore the value of GA tracking ID and Messaging Sender
159160
// ID as we don't support analytics on desktop at the moment.
160161
};
161-
for (size_t i = 0;
162-
i < sizeof(options_to_validate) / sizeof(options_to_validate[0]); ++i) {
162+
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(options_to_validate); ++i) {
163163
const auto& validate_option = options_to_validate[i];
164164
if (strlen(validate_option.option_value) == 0) {
165165
LogWarning("%s not set in the Firebase config.",

app/src/google_play_services/availability_android.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "app/google_api_resources.h"
2424
#include "app/src/embedded_file.h"
25+
#include "app/src/include/firebase/internal/common.h"
2526
#include "app/src/log.h"
2627
#include "app/src/reference_counted_future_impl.h"
2728
#include "app/src/util_android.h"
@@ -174,8 +175,7 @@ bool Initialize(JNIEnv* env, jobject activity) {
174175
&embedded_files) &&
175176
googleapiavailabilityhelper::CacheMethodIds(env, activity) &&
176177
googleapiavailabilityhelper::RegisterNatives(
177-
env, kHelperMethods,
178-
sizeof(kHelperMethods) / sizeof(kHelperMethods[0]))) {
178+
env, kHelperMethods, FIREBASE_ARRAYSIZE(kHelperMethods))) {
179179
// Everything initialized properly.
180180
g_data->classes_loaded = true;
181181
return true;
@@ -232,8 +232,7 @@ Availability CheckAvailability(JNIEnv* env, jobject activity) {
232232
activity);
233233
firebase::util::CheckAndClearJniExceptions(env);
234234
env->DeleteLocalRef(api);
235-
for (size_t i = 0; i < sizeof(ConnectionResultToAvailability) /
236-
sizeof(ConnectionResultToAvailability[0]);
235+
for (size_t i = 0; i < FIREBASE_ARRAYSIZE(ConnectionResultToAvailability);
237236
i++) {
238237
if (result == ConnectionResultToAvailability[i].java) {
239238
g_data->cached_availability = ConnectionResultToAvailability[i].cpp;

app/src/include/firebase/internal/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,10 @@ struct AlignedStorage {
121121
#endif
122122
#endif // FIREBASE_DEPRECATED
123123

124+
// Calculates the number of elements in an array.
125+
#define FIREBASE_ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
126+
127+
// Guaranteed compile time strlen.
128+
#define FIREBASE_STRLEN(s) (FIREBASE_ARRAYSIZE(s) - sizeof((s)[0]))
129+
124130
#endif // FIREBASE_APP_CLIENT_CPP_SRC_INCLUDE_FIREBASE_INTERNAL_COMMON_H_

app/src/invites/android/invites_android_helper.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "app/invites_resources.h"
2323
#include "app/src/embedded_file.h"
24+
#include "app/src/include/firebase/internal/common.h"
2425
#include "app/src/invites/receiver_interface.h"
2526
#include "app/src/invites/sender_receiver_interface.h"
2627
#include "app/src/log.h"
@@ -81,8 +82,7 @@ AndroidHelper::AndroidHelper(const ::firebase::App& app,
8182
dynamic_links_native_wrapper::CacheMethodIds(env,
8283
app_->activity()) &&
8384
dynamic_links_native_wrapper::RegisterNatives(
84-
env, kNativeMethods,
85-
sizeof(kNativeMethods) / sizeof(kNativeMethods[0])))) {
85+
env, kNativeMethods, FIREBASE_ARRAYSIZE(kNativeMethods)))) {
8686
util::Terminate(env);
8787
app_ = nullptr;
8888
return;

app/src/log_android_callback.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <string>
2121

22+
#include "app/src/include/firebase/internal/common.h"
2223
#include "app/src/log.h"
2324
#include "app/src/util_android.h"
2425

@@ -44,8 +45,7 @@ Java_com_google_firebase_app_internal_cpp_Log_nativeLog(
4445
kLogLevelError, // 6 = android.util.Log.ERROR
4546
kLogLevelAssert, // 7 = android.util.Log.ASSERT
4647
};
47-
assert(priority <
48-
sizeof(kLogPriorityToLogLevel) / sizeof(kLogPriorityToLogLevel[0]));
48+
assert(priority < FIREBASE_ARRAYSIZE(kLogPriorityToLogLevel));
4949
assert(priority >= 0);
5050
LogMessage(kLogPriorityToLogLevel[priority], "(%s) %s", ctag.c_str(),
5151
cmsg.c_str());

app/src/log_ios.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#import "FIRConfiguration.h"
2222

23+
#include "app/src/include/firebase/internal/common.h"
24+
2325
#include <stdarg.h>
2426

2527
namespace firebase {
@@ -58,7 +60,7 @@ void LogInitialize() {
5860

5961
// Set the platform specific SDK log level.
6062
void LogSetPlatformLevel(LogLevel level) {
61-
assert(level < sizeof(kCppToIOSLogLevel) / sizeof(kCppToIOSLogLevel[0]));
63+
assert(level < FIREBASE_ARRAYSIZE(kCppToIOSLogLevel));
6264
[[FIRConfiguration sharedInstance] setLoggerLevel:kCppToIOSLogLevel[level]];
6365
}
6466

app/src/log_stdio.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdio.h>
2020
#include <string.h>
2121

22+
#include "app/src/include/firebase/internal/common.h"
2223
#include "app/src/include/firebase/internal/platform.h"
2324
#include "app/src/log.h"
2425

@@ -57,7 +58,7 @@ void LogSetPlatformLevel(LogLevel level) {}
5758

5859
// Log a firebase message.
5960
void LogMessageV(LogLevel log_level, const char* format, va_list args) {
60-
assert(log_level < (sizeof(kLogLevelPrefix) / sizeof(kLogLevelPrefix[0])));
61+
assert(log_level < FIREBASE_ARRAYSIZE(kLogLevelPrefix));
6162
const char* prefix = kLogLevelPrefix[log_level];
6263
printf("%s", prefix);
6364
vprintf(format, args);

0 commit comments

Comments
 (0)