Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Flatten the class hierarchy

Refactoring everything into classes earned me a fatal "illegal redeclaration of
symbol 'XMMS2::Client'". Seems it didn't like names like "XMMS2::Client::Result"
for some reason, but that's fixed now.
  • Loading branch information...
commit 474fd9ca82729df533f3527cf5f35ce301502c2c 1 parent eeeeaef
authored December 17, 2010
4  lib/XMMS2/Client.pm6
... ...
@@ -1,6 +1,6 @@
1 1
 use v6;
2 2
 use NativeCall;
3  
-use XMMS2::Client::Connection;
  3
+use XMMS2::Connection;
4 4
 
5 5
 #####
6 6
 # The user-interfaceable part
@@ -16,6 +16,6 @@ class XMMS2::Client {
16 16
     }
17 17
 
18 18
     submethod BUILD(Str $client_name, Str $path) {
19  
-        $!connection = XMMS2::Client::Connection.new($client_name, $path);
  19
+        $!connection = XMMS2::Connection.new($client_name, $path);
20 20
     }
21 21
 };
54  lib/XMMS2/Client/Connection.pm6
... ...
@@ -1,54 +0,0 @@
1  
-use v6;
2  
-use NativeCall;
3  
-use XMMS2::Client::Result;
4  
-
5  
-# Pointer to an xmms2d connection
6  
-# Would be nice if I could put the "is OpaquePointer" on the class below and have it DWIM...
7  
-class xmmsc_connection_t is OpaquePointer { };
8  
-
9  
-# Wrapper around a connection pointer
10  
-class XMMS2::Client::Connection {
11  
-    has xmmsc_connection_t $.connection;
12  
-
13  
-    method new(Str $client_name, Str $path = %*ENV<XMMS_PATH>) {
14  
-        self.bless(*, :$client_name, :$path);
15  
-    }
16  
-
17  
-    # TODO: there's async API, but this doesn't do that yet.
18  
-    method play(:$synchronous!) returns Bool {
19  
-        my $result = 
20  
-        my Bool $success = XMMS2::Client::Result.new(:result => xmmsc_playback_start($!connection)).ok;
21  
-
22  
-        warn 'Playback start failed!' if not $success;
23  
-
24  
-        return $success;
25  
-    }
26  
-
27  
-    submethod BUILD(Str $client_name, Str $path) {
28  
-        # FIXME: this can return NULL in an out-of-memory condition. No matter how implausible that
29  
-        # might sound, it should still be checked...
30  
-        $!connection = xmmsc_init($client_name);
31  
-
32  
-        xmmsc_connect($!connection, $path || pir::null__P())
33  
-            or die "Connecting via '$path' failed with error: {xmmsc_get_last_error($!connection)}";
34  
-    }
35  
-
36  
-    submethod DESTROY {
37  
-        xmmsc_unref($!connection);
38  
-    }
39  
-};
40  
-
41  
-sub xmmsc_playback_start(xmmsc_connection_t --> xmmsc_result_t)
42  
-    is native('libxmmsclient') { ... }
43  
-
44  
-sub xmmsc_init(Str $clientname --> xmmsc_connection_t)
45  
-    is native('libxmmsclient') { ... }
46  
-
47  
-sub xmmsc_connect(xmmsc_connection_t, Str $path --> Int)
48  
-    is native('libxmmsclient') { ... }
49  
-
50  
-sub xmmsc_unref(xmmsc_connection_t)
51  
-    is native('libxmmsclient') { ... }
52  
-
53  
-sub xmmsc_get_last_error(xmmsc_connection_t --> Str)
54  
-    is native('libxmmsclient') { ... }
37  lib/XMMS2/Client/Result.pm6
... ...
@@ -1,37 +0,0 @@
1  
-use v6;
2  
-use NativeCall;
3  
-use XMMS2::Value;
4  
-
5  
-# Result struct from a command
6  
-class xmmsc_result_t is OpaquePointer { };
7  
-
8  
-# Wrapper around a result object
9  
-class XMMS2::Client::Result {
10  
-    has xmmsc_result_t $.result;
11  
-
12  
-    # Check whether this result is an error status
13  
-    method ok(:$verbose!) returns Bool {
14  
-        xmmsc_result_wait($!result);
15  
-        my $status = XMMS2::Value.new: xmmsc_result_get_value($!result);
16  
-        my $failed = ?$status.error;
17  
-
18  
-        if $verbose and $failed {
19  
-            warn $status.error;
20  
-        }
21  
-
22  
-        return not $failed;
23  
-    }
24  
-
25  
-    submethod DESTROY {
26  
-        xmmsc_result_unref($!result);
27  
-    }
28  
-};
29  
-
30  
-sub xmmsc_result_wait(xmmsc_result_t)
31  
-    is native('libxmmsclient') { ... }
32  
-
33  
-sub xmmsc_result_get_value(xmmsc_result_t --> xmmsv_t)
34  
-    is native('libxmmsclient') { ... }
35  
-
36  
-sub xmmsc_result_unref(xmmsc_result_t)
37  
-    is native('libxmmsclient') { ... }
53  lib/XMMS2/Connection.pm6
... ...
@@ -0,0 +1,53 @@
  1
+use v6;
  2
+use NativeCall;
  3
+use XMMS2::Result;
  4
+
  5
+# Pointer to an xmms2d connection
  6
+class xmmsc_connection_t is OpaquePointer { };
  7
+
  8
+# Native functions
  9
+sub xmmsc_playback_start(xmmsc_connection_t --> xmmsc_result_t)
  10
+    is native('libxmmsclient') { ... }
  11
+
  12
+sub xmmsc_init(Str $clientname --> xmmsc_connection_t)
  13
+    is native('libxmmsclient') { ... }
  14
+
  15
+sub xmmsc_connect(xmmsc_connection_t, Str $path --> Int)
  16
+    is native('libxmmsclient') { ... }
  17
+
  18
+sub xmmsc_unref(xmmsc_connection_t)
  19
+    is native('libxmmsclient') { ... }
  20
+
  21
+sub xmmsc_get_last_error(xmmsc_connection_t --> Str)
  22
+    is native('libxmmsclient') { ... }
  23
+
  24
+# Wrapper around a connection pointer
  25
+class XMMS2::Connection;
  26
+has xmmsc_connection_t $.connection;
  27
+
  28
+method new(Str $client_name, Str $path = %*ENV<XMMS_PATH>) {
  29
+    self.bless(*, :$client_name, :$path);
  30
+}
  31
+
  32
+# TODO: there's async API, but this doesn't do that yet.
  33
+method play(:$synchronous!) returns Bool {
  34
+    my $result = 
  35
+    my Bool $success = XMMS2::Result.new(:result => xmmsc_playback_start($!connection)).ok;
  36
+
  37
+    warn 'Playback start failed!' if not $success;
  38
+
  39
+    return $success;
  40
+}
  41
+
  42
+submethod BUILD(Str $client_name, Str $path) {
  43
+    # FIXME: this can return NULL in an out-of-memory condition. No matter how implausible that
  44
+    # might sound, it should still be checked...
  45
+    $!connection = xmmsc_init($client_name);
  46
+
  47
+    xmmsc_connect($!connection, $path || pir::null__P())
  48
+        or die "Connecting via '$path' failed with error: {xmmsc_get_last_error($!connection)}";
  49
+}
  50
+
  51
+submethod DESTROY {
  52
+    xmmsc_unref($!connection);
  53
+}
37  lib/XMMS2/Result.pm6
... ...
@@ -0,0 +1,37 @@
  1
+use v6;
  2
+use NativeCall;
  3
+use XMMS2::Value;
  4
+
  5
+# Result struct from a command
  6
+class xmmsc_result_t is OpaquePointer { };
  7
+
  8
+# Native functions
  9
+sub xmmsc_result_wait(xmmsc_result_t)
  10
+    is native('libxmmsclient') { ... }
  11
+
  12
+sub xmmsc_result_get_value(xmmsc_result_t --> xmmsv_t)
  13
+    is native('libxmmsclient') { ... }
  14
+
  15
+sub xmmsc_result_unref(xmmsc_result_t)
  16
+    is native('libxmmsclient') { ... }
  17
+
  18
+# Wrapper around a result object
  19
+class XMMS2::Result;
  20
+has xmmsc_result_t $.result;
  21
+
  22
+# Check whether this result is an error status
  23
+method ok(:$verbose!) returns Bool {
  24
+    xmmsc_result_wait($!result);
  25
+    my $status = XMMS2::Value.new: xmmsc_result_get_value($!result);
  26
+    my $failed = ?$status.error;
  27
+
  28
+    if $verbose and $failed {
  29
+        warn $status.error;
  30
+    }
  31
+
  32
+    return not $failed;
  33
+}
  34
+
  35
+submethod DESTROY {
  36
+    xmmsc_result_unref($!result);
  37
+}
24  lib/XMMS2/Value.pm6
@@ -4,20 +4,20 @@ use NativeCall;
4 4
 # XMMS2 variant value
5 5
 class xmmsv_t is OpaquePointer { };
6 6
 
7  
-# Wrapper around an XMMS2 value struct
8  
-class XMMS2::Value {
9  
-    has $!value;
10  
-
11  
-    method error returns Str {
12  
-        return Str unless xmmsv_is_error($!value);
13  
-
14  
-        xmmsv_get_error($!value, my $error-str);
15  
-        return $error-str;
16  
-    }
17  
-}
18  
-
  7
+# Native functions
19 8
 sub xmmsv_is_error(xmmsv_t --> Int)
20 9
     is native('libxmmsclient') { ... }
21 10
 
22 11
 sub xmmsv_get_error(xmmsv_t, Str $error is rw --> Int)
23 12
     is native('libxmmsclient') { ... }
  13
+
  14
+# Wrapper around an XMMS2 value struct
  15
+class XMMS2::Value;
  16
+has $!value;
  17
+
  18
+method error returns Str {
  19
+    return Str unless xmmsv_is_error($!value);
  20
+
  21
+    xmmsv_get_error($!value, my $error-str);
  22
+    return $error-str;
  23
+}

0 notes on commit 474fd9c

Please sign in to comment.
Something went wrong with that request. Please try again.