diff --git a/debian/copyright b/debian/copyright index 7c296986e..17dd500ca 100644 --- a/debian/copyright +++ b/debian/copyright @@ -45,7 +45,8 @@ Files: src/core/fht.* Copyright: 2004, Melchior FRANZ License: GPL-2+ -Files: src/core/scoped_nsobject.h +Files: ext/libclementine-common/core/arraysize.h + src/core/scoped_nsobject.h src/core/scoped_cftyperef.h src/core/scoped_nsautorelease_pool.* Copyright: 2011, The Chromium Authors diff --git a/ext/clementine-spotifyblob/spotifyclient.cpp b/ext/clementine-spotifyblob/spotifyclient.cpp index 1e24f08ab..df10375b5 100644 --- a/ext/clementine-spotifyblob/spotifyclient.cpp +++ b/ext/clementine-spotifyblob/spotifyclient.cpp @@ -18,14 +18,9 @@ // it is used by the Spotify blob which links against libspotify and is not GPL // compatible. -#include - -#include "mediapipeline.h" #include "spotifyclient.h" -#include "spotifykey.h" -#include "spotifymessages.pb.h" -#include "spotify_utilities.h" -#include "core/logging.h" + +#include #include #include @@ -33,6 +28,13 @@ #include #include +#include "core/arraysize.h" +#include "core/logging.h" +#include "mediapipeline.h" +#include "spotifykey.h" +#include "spotifymessages.pb.h" +#include "spotify_utilities.h" + const int SpotifyClient::kSpotifyImageIDSize = 20; const int SpotifyClient::kWaveHeaderSize = 44; @@ -615,7 +617,7 @@ void SpotifyClient::ConvertTrack(sp_track* track, pb::spotify::Track* pb) { // URI - Blugh char uri[256]; sp_link* link = sp_link_create_from_track(track, 0); - sp_link_as_string(link, uri, sizeof(uri)); + sp_link_as_string(link, uri, arraysize(uri)); sp_link_release(link); pb->set_uri(uri); @@ -645,7 +647,7 @@ void SpotifyClient::ConvertAlbum(sp_album* album, pb::spotify::Track* pb) { // URI - Blugh char uri[256]; sp_link* link = sp_link_create_from_album(album); - sp_link_as_string(link, uri, sizeof(uri)); + sp_link_as_string(link, uri, arraysize(uri)); sp_link_release(link); pb->set_uri(uri); diff --git a/ext/libclementine-common/core/arraysize.h b/ext/libclementine-common/core/arraysize.h new file mode 100644 index 000000000..ff43e200b --- /dev/null +++ b/ext/libclementine-common/core/arraysize.h @@ -0,0 +1,34 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// From Chromium src/base/macros.h + +#include // For size_t. + +// The arraysize(arr) macro returns the # of elements in an array arr. +// The expression is a compile-time constant, and therefore can be +// used in defining new arrays, for example. If you use arraysize on +// a pointer by mistake, you will get a compile-time error. +// +// One caveat is that arraysize() doesn't accept any array of an +// anonymous type or a type defined inside a function. In these rare +// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is +// due to a limitation in C++'s template system. The limitation might +// eventually be removed, but it hasn't happened yet. + +// This template function declaration is used in defining arraysize. +// Note that the function doesn't need an implementation, as we only +// use its type. +template +char (&ArraySizeHelper(T (&array)[N]))[N]; + +// That gcc wants both of these prototypes seems mysterious. VC, for +// its part, can't decide which to use (another mystery). Matching of +// template overloads: the final frontier. +#ifndef _MSC_VER +template +char (&ArraySizeHelper(const T (&array)[N]))[N]; +#endif + +#define arraysize(array) (sizeof(ArraySizeHelper(array))) diff --git a/src/analyzers/nyancatanalyzer.cpp b/src/analyzers/nyancatanalyzer.cpp index 760e97fb6..4de3f8b3f 100644 --- a/src/analyzers/nyancatanalyzer.cpp +++ b/src/analyzers/nyancatanalyzer.cpp @@ -16,13 +16,15 @@ */ #include "nyancatanalyzer.h" -#include "core/logging.h" #include #include #include +#include "core/arraysize.h" +#include "core/logging.h" + const char* NyanCatAnalyzer::kName = "Nyanalyzer cat"; const float NyanCatAnalyzer::kPixelScale = 0.02f; @@ -36,7 +38,7 @@ NyanCatAnalyzer::NyanCatAnalyzer(QWidget* parent) px_per_frame_(0), x_offset_(0), background_brush_(QColor(0x0f, 0x43, 0x73)) { - memset(history_, 0, sizeof(history_)); + memset(history_, 0, arraysize(history_)); for (int i = 0; i < kRainbowBands; ++i) { colors_[i] = QPen(QColor::fromHsv(i * 255 / kRainbowBands, 255, 255), diff --git a/src/moodbar/moodbarrenderer.cpp b/src/moodbar/moodbarrenderer.cpp index bb6807dc8..55df0faad 100644 --- a/src/moodbar/moodbarrenderer.cpp +++ b/src/moodbar/moodbarrenderer.cpp @@ -20,6 +20,8 @@ #include #include +#include "core/arraysize.h" + const int MoodbarRenderer::kNumHues = 12; ColorVector MoodbarRenderer::Colors(const QByteArray& data, MoodbarStyle style, @@ -60,7 +62,7 @@ ColorVector MoodbarRenderer::Colors(const QByteArray& data, MoodbarStyle style, int hue_distribution[360]; int total = 0; - memset(hue_distribution, 0, sizeof(hue_distribution)); + memset(hue_distribution, 0, arraysize(hue_distribution)); ColorVector colors;