Skip to content

Commit d508650

Browse files
authored
Merge pull request #272 from Libvisual/c++17-string-view
Core: Optimize string arguments and returns.
2 parents e0a4d62 + 375e3d0 commit d508650

32 files changed

+186
-116
lines changed

libvisual/libvisual/lv_actor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lv_common.h"
2626
#include "lv_plugin_registry.h"
2727
#include <stdexcept>
28+
#include <string>
2829

2930
namespace LV {
3031

@@ -74,30 +75,30 @@ namespace LV {
7475
return actor_plugin ? actor_plugin->vidoptions.depth : VISUAL_VIDEO_DEPTH_NONE;
7576
}
7677

77-
bool Actor::available(std::string const& name) {
78+
bool Actor::available(std::string_view name) {
7879
return LV::PluginRegistry::instance()->has_plugin (VISUAL_PLUGIN_TYPE_ACTOR, name);
7980
}
8081

81-
ActorPtr Actor::load (std::string const& name)
82+
ActorPtr Actor::load (std::string_view name)
8283
{
8384
try {
84-
return {new Actor (name), false};
85+
return {new Actor {name}, false};
8586
}
8687
catch (std::exception& error) {
8788
visual_log (VISUAL_LOG_ERROR, "%s", error.what ());
8889
return nullptr;
8990
}
9091
}
9192

92-
Actor::Actor (std::string const& name)
93+
Actor::Actor (std::string_view name)
9394
: m_impl {new Impl}
9495
, m_ref_count {1}
9596
{
9697
if (!available (name)) {
9798
throw std::runtime_error {"Actor plugin not found"};
9899
}
99100

100-
m_impl->plugin = visual_plugin_load (VISUAL_PLUGIN_TYPE_ACTOR, name.c_str ());
101+
m_impl->plugin = visual_plugin_load (VISUAL_PLUGIN_TYPE_ACTOR, std::string {name}.c_str ());
101102
if (!m_impl->plugin) {
102103
throw std::runtime_error {"Failed to load actor plugin"};
103104
}

libvisual/libvisual/lv_actor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
#ifdef __cplusplus
3838

3939
#include <libvisual/lv_intrusive_ptr.hpp>
40-
#include <string>
4140
#include <memory>
41+
#include <string_view>
4242

4343
namespace LV
4444
{
@@ -59,7 +59,7 @@ namespace LV
5959
*
6060
* @return True if an actor plugin by that name is available, else false
6161
*/
62-
static bool available (std::string const& name);
62+
static bool available (std::string_view name);
6363

6464
/**
6565
* Creates a new Actor with a plugin of a given name.
@@ -70,7 +70,7 @@ namespace LV
7070
*
7171
* @return New actor, or nullptr if plugin failed to load
7272
*/
73-
static ActorPtr load (std::string const& name);
73+
static ActorPtr load (std::string_view name);
7474

7575
Actor (Actor const&) = delete;
7676

@@ -165,7 +165,7 @@ namespace LV
165165

166166
mutable unsigned int m_ref_count;
167167

168-
explicit Actor (std::string const& name);
168+
explicit Actor (std::string_view name);
169169
};
170170

171171
inline void intrusive_ptr_add_ref (Actor const* actor)

libvisual/libvisual/lv_audio.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lv_audio.h"
2626
#include "private/lv_audio_convert.hpp"
2727
#include "private/lv_audio_stream.hpp"
28+
#include "private/lv_string_hash.hpp"
2829
#include "lv_common.h"
2930
#include "lv_fourier.h"
3031
#include "lv_math.h"
@@ -44,13 +45,13 @@ namespace LV {
4445
{
4546
public:
4647

47-
typedef std::unordered_map<std::string, AudioChannelPtr> ChannelList;
48+
using ChannelList = std::unordered_map<std::string, AudioChannelPtr, StringHash, std::equal_to<>>;
4849

4950
ChannelList channels;
5051

51-
void upload_to_channel (std::string const& name, BufferConstPtr const& samples, Time const& timestamp);
52+
void upload_to_channel (std::string_view name, BufferConstPtr const& samples, Time const& timestamp);
5253

53-
AudioChannel* get_channel (std::string const& name) const;
54+
AudioChannel* get_channel (std::string_view name) const;
5455
};
5556

5657
class AudioChannel
@@ -60,7 +61,7 @@ namespace LV {
6061
std::string name;
6162
AudioStream stream;
6263

63-
explicit AudioChannel (std::string const& name);
64+
explicit AudioChannel (std::string_view name);
6465

6566
~AudioChannel ();
6667

@@ -88,22 +89,24 @@ namespace LV {
8889

8990
} // anonymous
9091

91-
void Audio::Impl::upload_to_channel (std::string const& name, BufferConstPtr const& samples, Time const& timestamp)
92+
void Audio::Impl::upload_to_channel (std::string_view name, BufferConstPtr const& samples, Time const& timestamp)
9293
{
93-
if (!get_channel (name)) {
94-
channels[name] = std::make_unique<AudioChannel> (name);
94+
auto entry {channels.find (name)};
95+
96+
if (entry == channels.end ()) {
97+
entry = channels.emplace (name, std::make_unique<AudioChannel> (name)).first;
9598
}
9699

97-
channels[name]->add_samples (samples, timestamp);
100+
entry->second->add_samples (samples, timestamp);
98101
}
99102

100-
AudioChannel* Audio::Impl::get_channel (std::string const& name) const
103+
AudioChannel* Audio::Impl::get_channel (std::string_view name) const
101104
{
102105
auto entry = channels.find (name);
103106
return entry != channels.end () ? entry->second.get () : nullptr;
104107
}
105108

106-
AudioChannel::AudioChannel (std::string const& name_)
109+
AudioChannel::AudioChannel (std::string_view name_)
107110
: name (name_)
108111
{}
109112

@@ -140,7 +143,7 @@ namespace LV {
140143
return *this;
141144
}
142145

143-
bool Audio::get_sample (BufferPtr const& buffer, std::string const& channel_name)
146+
bool Audio::get_sample (BufferPtr const& buffer, std::string_view channel_name)
144147
{
145148
auto channel = m_impl->get_channel (channel_name);
146149

@@ -220,7 +223,7 @@ namespace LV {
220223
}
221224
}
222225

223-
void Audio::get_spectrum (BufferPtr const& buffer, std::size_t samplelen, std::string const& channel_name, bool normalised)
226+
void Audio::get_spectrum (BufferPtr const& buffer, std::size_t samplelen, std::string_view channel_name, bool normalised)
224227
{
225228
auto sample = Buffer::create (samplelen);
226229

@@ -230,7 +233,7 @@ namespace LV {
230233
buffer->fill (0);
231234
}
232235

233-
void Audio::get_spectrum (BufferPtr const& buffer, std::size_t samplelen, std::string const& channel_name, bool normalised, float multiplier)
236+
void Audio::get_spectrum (BufferPtr const& buffer, std::size_t samplelen, std::string_view channel_name, bool normalised, float multiplier)
234237
{
235238
auto spectrum {Buffer::create (buffer->get_size ())};
236239
get_spectrum (spectrum, samplelen, channel_name, normalised);
@@ -314,7 +317,7 @@ namespace LV {
314317
void Audio::input (BufferPtr const& buffer,
315318
VisAudioSampleRateType rate,
316319
VisAudioSampleFormatType format,
317-
std::string const& channel_name)
320+
std::string_view channel_name)
318321
{
319322
(void)rate;
320323

libvisual/libvisual/lv_audio.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef enum {
6666
#ifdef __cplusplus
6767

6868
#include <memory>
69-
#include <string>
69+
#include <string_view>
7070
#include <cstdarg>
7171

7272
namespace LV {
@@ -114,7 +114,7 @@ namespace LV {
114114
*
115115
* @return true if successful, false if channel does not exist
116116
*/
117-
bool get_sample (BufferPtr const& buffer, std::string const& channel_name);
117+
bool get_sample (BufferPtr const& buffer, std::string_view channel_name);
118118

119119
/**
120120
* Returns samples downmixed by averaging a set of channels.
@@ -153,9 +153,9 @@ namespace LV {
153153
* @param channel_name name of channel
154154
* @param normalised normalise ampltitudes to [0.0, 1.0]
155155
*/
156-
void get_spectrum (BufferPtr const& buffer, std::size_t sample_count, std::string const& channel_name, bool normalised);
156+
void get_spectrum (BufferPtr const& buffer, std::size_t sample_count, std::string_view channel_name, bool normalised);
157157

158-
void get_spectrum (BufferPtr const& buffer, std::size_t sample_count, std::string const& channel_name, bool normalised, float multiplier);
158+
void get_spectrum (BufferPtr const& buffer, std::size_t sample_count, std::string_view channel_name, bool normalised, float multiplier);
159159

160160
/**
161161
* Returns the amplitude spectrum of a set of samples.
@@ -196,7 +196,7 @@ namespace LV {
196196
void input (BufferPtr const& buffer,
197197
VisAudioSampleRateType rate,
198198
VisAudioSampleFormatType format,
199-
std::string const& channel_name);
199+
std::string_view channel_name);
200200

201201
private:
202202

libvisual/libvisual/lv_bin.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "config.h"
2525
#include "lv_bin.h"
2626
#include "lv_common.h"
27+
#include <string>
2728

2829
namespace LV {
2930

@@ -151,7 +152,7 @@ namespace LV {
151152
return m_impl->input;
152153
}
153154

154-
void Bin::set_morph (std::string const& morph_name)
155+
void Bin::set_morph (std::string_view morph_name)
155156
{
156157
m_impl->morph = Morph::load (morph_name);
157158
visual_return_if_fail (m_impl->morph);
@@ -190,7 +191,7 @@ namespace LV {
190191
return true;
191192
}
192193

193-
bool Bin::connect (std::string const& actor_name, std::string const& input_name)
194+
bool Bin::connect (std::string_view actor_name, std::string_view input_name)
194195
{
195196
// Create the actor
196197
auto actor = Actor::load (actor_name);
@@ -362,10 +363,13 @@ namespace LV {
362363
return *m_impl->actor->get_palette ();
363364
}
364365

365-
void Bin::switch_actor (std::string const& actor_name)
366+
void Bin::switch_actor (std::string_view actor_name)
366367
{
368+
// FIXME: This is needed because visual_log() takes only null-terminated C strings.
369+
std::string actor_name_str {actor_name};
370+
367371
visual_log (VISUAL_LOG_DEBUG, "switching to a new actor: %s, old actor: %s",
368-
actor_name.c_str (), visual_plugin_get_info (m_impl->actor->get_plugin ())->plugname);
372+
actor_name_str.c_str (), visual_plugin_get_info (m_impl->actor->get_plugin ())->plugname);
369373

370374
if (m_impl->actmorph) {
371375
m_impl->actmorph.reset ();

libvisual/libvisual/lv_bin.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef enum {
4343
#ifdef __cplusplus
4444

4545
#include <memory>
46-
#include <string>
46+
#include <string_view>
4747

4848
namespace LV {
4949

@@ -65,9 +65,9 @@ namespace LV {
6565
InputPtr const& get_input () const;
6666
MorphPtr const& get_morph () const;
6767

68-
void set_morph (std::string const& morph_name);
68+
void set_morph (std::string_view morph_name);
6969

70-
bool connect (std::string const& actor_name, std::string const& input_name);
70+
bool connect (std::string_view actor_name, std::string_view input_name);
7171

7272
void sync (bool noevent);
7373

@@ -87,7 +87,7 @@ namespace LV {
8787

8888
Palette const& get_palette () const;
8989

90-
void switch_actor (std::string const& actname);
90+
void switch_actor (std::string_view actname);
9191

9292
void switch_finalize ();
9393

libvisual/libvisual/lv_input.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "lv_common.h"
2727
#include "lv_plugin_registry.h"
2828
#include <stdexcept>
29+
#include <string>
2930

3031
namespace LV {
3132

@@ -60,11 +61,11 @@ namespace LV {
6061
return static_cast<VisInputPlugin*> (visual_plugin_get_info (plugin)->plugin);
6162
}
6263

63-
bool Input::available(std::string const& name) {
64+
bool Input::available(std::string_view name) {
6465
return LV::PluginRegistry::instance()->has_plugin (VISUAL_PLUGIN_TYPE_INPUT, name);
6566
}
6667

67-
InputPtr Input::load (std::string const& name)
68+
InputPtr Input::load (std::string_view name)
6869
{
6970
try {
7071
return {new Input{name}, false};
@@ -75,15 +76,15 @@ namespace LV {
7576
}
7677
}
7778

78-
Input::Input (std::string const& name)
79+
Input::Input (std::string_view name)
7980
: m_impl {new Impl}
8081
, m_ref_count {1}
8182
{
8283
if (!available (name)) {
8384
throw std::runtime_error {"Input plugin not found"};
8485
}
8586

86-
m_impl->plugin = visual_plugin_load (VISUAL_PLUGIN_TYPE_INPUT, name.c_str ());
87+
m_impl->plugin = visual_plugin_load (VISUAL_PLUGIN_TYPE_INPUT, std::string {name}.c_str ());
8788
if (!m_impl->plugin) {
8889
throw std::runtime_error {"Failed to load input plugin"};
8990
}

libvisual/libvisual/lv_input.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <libvisual/lv_intrusive_ptr.hpp>
3838
#include <functional>
3939
#include <memory>
40+
#include <string_view>
4041

4142
namespace LV {
4243

@@ -55,7 +56,7 @@ namespace LV {
5556
*
5657
* @return True if an input plugin by that name is available, else false
5758
*/
58-
static bool available (std::string const& name);
59+
static bool available (std::string_view name);
5960

6061
/**
6162
* Creates a new Input with a plugin of a given name.
@@ -64,7 +65,7 @@ namespace LV {
6465
*
6566
* @return A new Input, or nullptr on failure.
6667
*/
67-
static InputPtr load (std::string const& name);
68+
static InputPtr load (std::string_view name);
6869

6970
~Input ();
7071

@@ -116,7 +117,7 @@ namespace LV {
116117

117118
mutable unsigned int m_ref_count;
118119

119-
explicit Input (std::string const& name);
120+
explicit Input (std::string_view name);
120121
};
121122

122123
inline void intrusive_ptr_add_ref (Input const* input)

libvisual/libvisual/lv_libvisual.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#include "gettext.h"
3939

40+
#include <string>
41+
4042
extern "C" {
4143
void visual_cpu_initialize (void);
4244
void visual_mem_initialize (void);
@@ -107,9 +109,10 @@ namespace LV
107109
m_instance.reset (new System {argc, argv});
108110
}
109111

110-
std::string System::get_version () const
112+
std::string const& System::get_version () const
111113
{
112-
return VISUAL_VERSION " (" LV_REVISION ")";
114+
static std::string const version_str {VISUAL_VERSION " (" LV_REVISION ")"};
115+
return version_str;
113116
}
114117

115118
int System::get_api_version () const

libvisual/libvisual/lv_libvisual.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace LV {
6363
*
6464
* @return version string
6565
*/
66-
std::string get_version () const;
66+
std::string const& get_version () const;
6767

6868
/**
6969
* Returns the Libvisual API verison.

0 commit comments

Comments
 (0)