{"id":501,"date":"2005-12-14T09:36:23","date_gmt":"2005-12-14T09:36:23","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2005\/12\/adding_class_variables_to_object_orientated_inline_c-shtml\/"},"modified":"2005-12-14T09:36:23","modified_gmt":"2005-12-14T09:36:23","slug":"adding_class_variables_to_object_orientated_inline_c-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/","title":{"rendered":"Adding Class Variables To Object Orientated Inline::C"},"content":{"rendered":"<p>\nI&#8217;ve been working using the <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> module <a href=\"http:\/\/search.cpan.org\/~ingy\/Inline\/\">Inline::C<\/a> lately.\n<\/p>\n<p>\nI&#8217;ve been using it for some Object Orientated code, and I&#8217;ve had the need to have a shared class variable between various C classes. I thought I&#8217;d share how I&#8217;d accomplished this.\n<\/p>\n<p>\nThe code I&#8217;m using here is based on the object example in <a href=\"http:\/\/search.cpan.org\/~ingy\/Inline\/C\/C-Cookbook.pod\">Inline::C-Cookbook<\/a>.\n<\/p>\n<p>\nI&#8217;m going to add a class variable called <var>count<\/var> to the example <var>Soldier<\/var> class.\n<\/p>\n<p>\nFirstly we need to add a new line of code to declare this variable\n<\/p>\n<div class=\"code\"><code>## class variable<br \/>\nstatic int counter = 0;<br \/>\n<\/code><\/div>\n<p>\nNoticed I&#8217;ve made the variable <code>static<\/code> to ensure only one copy of it exists. I&#8217;ve also initialised it to 0.\n<\/p>\n<p>\nOne thing I&#8217;ve done, that isn&#8217;t really necessary is to add it to the Soldier object. The new Soldier object looks like this&#8230;\n<\/p>\n<div class=\"code\"><code>typedef struct {<br \/>\nchar* name;<br \/>\nchar* rank;<br \/>\nlong  serial;<br \/>\nint* count;<br \/>\n} Soldier;<br \/>\n<\/code><\/div>\n<p>\nThe new line here is <code>int* count<\/code>, a pointer to an integer called count. We&#8217;ll point this at our static counter variable.\n<\/p>\n<p>\nTo point <var>count<\/var> to <var>counter<\/var> we modify the <code>new<\/code> function to add the following&#8230;\n<\/p>\n<div class=\"code\"><code>solder->serial = serial; ## existing code<br \/>\nsoldier->count = &amp;counter;<br \/>\n<\/code><\/div>\n<p>\nHere we&#8217;re specifically saying that our pointer count should point to the address of <var>counter<\/var>.\n<\/p>\n<p>\nGreat, but we need a way of accessing this data. We&#8217;ll create a new method called <code>get_count<\/code> to return the current value of count.\n<\/p>\n<div class=\"code\"><code>int get_count(SV* obj) {<br \/>\nreturn *((Soldier*)SvIV(SvRV(obj)))-&gt;count;<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nGreat, now we can just call <code>get_count<\/code> from our Soldier object.\n<\/p>\n<p>\nBy itself this isn&#8217;t really much use as we never manipulate the value of <var>counter<\/var>. To prove this works, just modify the <code>new<\/code> method to add a line that increments <var>counter<\/var>. For example,\n<\/p>\n<div class=\"code\"><code>## increment counter<br \/>\ncounter++;<br \/>\n<\/code><\/div>\n<p>\nVoila! We we have a static class variable that can now be used with Inline::C objects!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working using the Perl module Inline::C lately. I&#8217;ve been using it for some Object Orientated code, and I&#8217;ve had the need to have a shared class variable between various C classes. I thought I&#8217;d share how I&#8217;d accomplished this. The code I&#8217;m using here is based on the object example in Inline::C-Cookbook. I&#8217;m &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding Class Variables To Object Orientated Inline::C&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[17,47],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-dev","tag-dev","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Adding Class Variables To Object Orientated Inline::C - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding Class Variables To Object Orientated Inline::C - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been working using the Perl module Inline::C lately. I&#8217;ve been using it for some Object Orientated code, and I&#8217;ve had the need to have a shared class variable between various C classes. I thought I&#8217;d share how I&#8217;d accomplished this. The code I&#8217;m using here is based on the object example in Inline::C-Cookbook. I&#8217;m &hellip; Continue reading &quot;Adding Class Variables To Object Orientated Inline::C&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2005-12-14T09:36:23+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Adding Class Variables To Object Orientated Inline::C\",\"datePublished\":\"2005-12-14T09:36:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/\"},\"wordCount\":278,\"keywords\":[\"Dev\",\"Perl\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/\",\"name\":\"Adding Class Variables To Object Orientated Inline::C - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2005-12-14T09:36:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_class_variables_to_object_orientated_inline_c-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adding Class Variables To Object Orientated Inline::C\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adding Class Variables To Object Orientated Inline::C - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Adding Class Variables To Object Orientated Inline::C - Robert Price","og_description":"I&#8217;ve been working using the Perl module Inline::C lately. I&#8217;ve been using it for some Object Orientated code, and I&#8217;ve had the need to have a shared class variable between various C classes. I thought I&#8217;d share how I&#8217;d accomplished this. The code I&#8217;m using here is based on the object example in Inline::C-Cookbook. I&#8217;m &hellip; Continue reading \"Adding Class Variables To Object Orientated Inline::C\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/","og_site_name":"Robert Price","article_published_time":"2005-12-14T09:36:23+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Adding Class Variables To Object Orientated Inline::C","datePublished":"2005-12-14T09:36:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/"},"wordCount":278,"keywords":["Dev","Perl"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/","name":"Adding Class Variables To Object Orientated Inline::C - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2005-12-14T09:36:23+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_class_variables_to_object_orientated_inline_c-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Adding Class Variables To Object Orientated Inline::C"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}