PageSourceSearch

https://ada-lang.io/assets/js/0bda5ed5.befdff13.js

js ada-lang.io collected 2026-10-02 07:02:08 UTC 9,673 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkada_lang_io=self.webpackChunkada_lang_io||[]).push([["5496"],{56760(e,n,a){a.r(n),a.d(n,{metadata:()=>r,default:()=>h,frontMatter:()=>l,contentTitle:()=>c,toc:()=>d,assets:()=>u});var r=JSON.parse('{"id":"learn/lang/advanced-techniques","title":"RAII","description":"Ada supports scope-based resources, also called RAII","source":"@site/docs/learn/lang/advanced-techniques.mdx","sourceDirName":"learn/lang","slug":"/learn/lang/advanced-techniques","permalink":"/docs/learn/lang/advanced-techniques","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":1,"frontMatter":{"sidebar_position":1},"sidebar":"tutorialSidebar","previous":{"title":"Advanced Techniques","permalink":"/docs/category/advanced-techniques"},"next":{"title":"Tasks","permalink":"/docs/learn/lang/tasks"}}'),t=a(74848),i=a(28453),o=a(57250),s=a(78010);let l={sidebar_position:1},c="RAII",u={},d=[{value:"Timing out on a Blocking Operation",id:"timing-out-on-a-blocking-operation",level:2},{value:"Waiting for all tasks to complete",id:"waiting-for-all-tasks-to-complete",level:2}];function p(e){let n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",...(0,i.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.header,{children:(0,t.jsx)(n.h1,{id:"raii",children:"RAII"})}),"\n",(0,t.jsxs)(n.p,{children:["Ada supports scope-based resources, also called ",(0,t.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization",children:"RAII"}),"\nby extending the ",(0,t.jsx)(n.code,{children:"Controlled"})," or ",(0,t.jsx)(n.code,{children:"LimitedControlled"})," types."]}),"\n",(0,t.jsxs)(s.A,{children:[(0,t.jsx)(o.A,{value:"ada",label:"Ada",children:(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-ada",children:"with Ada.Finalization;  use Ada.Finalization;\npackage Sample is\n    -- \"Controlled\" types exhibit RAII behavior:\n    type Capricorn is new Controlled with\n    record\n        Dummy : Integer;\n    end record;\n\n    overriding procedure Initialize(C : in out Capricorn);\n        -- Initialization after creation.\n\n    overriding procedure Adjust(C : in out Capricorn);\n        -- Adjustment after assignment.\n\n    overriding procedure Finalize(C : in out Capricorn);\n        -- Different than Java's Finalize, in that it's deterministic and more\n        -- analogous to a C++ destructor.\n\n    -- If you don't want one of these do to anything, you can avoid writing a\n    -- definition in the package body and define the procedure as \"do nothing\"\n    -- by writing:\n    --\n    -- overriding procedure Finalize(C : in out Capricorn) is null;\nend Sample;\n\npackage body Sample is\n    procedure Initialize(C : in out Capricorn) is\n    begin\n        -- Do something on initialize.\n    end Initialize;\n\n    procedure Adjust(C : in out Capricorn) is\n    begin\n        -- Adjustment after assignment.\n        --\n        -- If you want Adjust to do the same as Initialize and use the same object\n        -- code without generating a separate function, you can just do\n        -- procedure Adjust(C: in out Capricorn) renames Initialize;\n    end Adjust;\n\n    overriding procedure Finalize(C : in out Capricorn);\n        -- Different than Java's Finalize, in that it's deterministic and more\n        -- analogous to a C++ destructor.\nend Sample;\n"})})}),(0,t.jsx)(o.A,{value:"cpp",label:"C++",children:(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cpp",children:"class Capricorn {\npublic:\n// Similar for all constructors.\nCapricorn () {}\n\n// Copy constructor.\nCapricorn(const Capricorn&) {}\n\n// Move constructor.\nCapricorn(Capricorn&&) {}\n\n// Copy assignment.\nCapricorn& operator=(const Capricorn&) { return *this; }\n\n// Move assignment.\nCapricorn& operator=(Capricorn&&) { return *this; }\n\n// Destructor.\n~Capricorn () {}\n};\n"})})})]}),"\n",(0,t.jsx)(n.h2,{id:"timing-out-on-a-blocking-operation",children:"Timing out on a Blocking Operation"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-ada",children:"task body My_Task is\n    Elem : A_Queue_Element;\nbegin\n    loop -- processing loop\n        select\n            A_Queue.Blocking_Queue (Elem);\n        or\n            -- Stop processing after a 1 second timeout. Removing this delay causes\n            -- immediate exit if a block occurs.\n            delay 1.0;\n            exit;\n        end select;\n\n        -- ... process Elem ...\n    end loop\nend My_Task;\n"})}),"\n",(0,t.jsx)(n.h2,{id:"waiting-for-all-tasks-to-complete",children:"Waiting for all tasks to complete"}),"\n",(0,t.jsxs)(n.p,{children:["A list of statements doesn't exit until all tasks are complete, so by using\n",(0,t.jsx)(n.code,{children:"declare ... begin ... end"})," you can wait until all your tasks are done."]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-ada",children:"declare\n    A_Task : My_Task;  -- task which needs to finish before more processing\nbegin\n    null; -- Just wait until the task is done.\nend;\n\n-- Continue other operations here.\n"})})]})}function h(e={}){let{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(p,{...e})}):p(e)}},57250(e,n,a){a.d(n,{A:()=>i});var r=a(74848);a(96540);var t=a(18215);function i({children:e,hidden:n,className:a}){return(0,r.jsx)("div",{role:"tabpanel",className:(0,t.A)("tabItem_Ymn6",a),hidden:n,children:e})}},78010(e,n,a){a.d(n,{A:()=>C});var r=a(74848),t=a(96540),i=a(18215),o=a(88287),s=a(28584),l=a(56347),c=a(99989),u=a(96629),d=a(80618),p=a(41367);function h(e){return t.Children.toArray(e).filter(e=>"\n"!==e).map(e=>{if(!e||(0,t.isValidElement)(e)&&function(e){let{props:n}=e;return!!n&&"object"==typeof n&&"value"in n}(e))return e;throw Error(`Docusaurus error: Bad <Tabs>
1 child <${"string"==typeof e.type?e.type:e.type.name}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.`)})?.filter(Boolean)??[]}function m({value:e,tabValues:n}){return n.some(n=>n.value===e)}var f=a(19863);function g({className:e,block:n,selectedValue:a,selectValue:t,tabValues:o}){let l=[],{blockElementScrollPositionUntilNextRender:c}=(0,s.a_)(),u=e=>{let n=e.currentTarget,r=o[l.indexOf(n)].value;r!==a&&(c(n),t(r))},d=e=>{let n=null;switch(e.key){case"Enter":u(e);break;case"ArrowRight":{let a=l.indexOf(e.currentTarget)+1;n=l[a]??l[0];break}case"ArrowLeft":{let a=l.indexOf(e.currentTarget)-1;n=l[a]??l[l.length-1]}}n?.focus()};return(0,r.jsx)("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,i.A)("tabs",{"tabs--block":n},e),children:o.map(({value:e,label:n,attributes:t})=>(0,r.jsx)("li",{role:"tab",tabIndex:a===e?0:-1,"aria-selected":a===e,ref:e=>{l.push(e)},onKeyDown:d,onClick:u,...t,className:(0,i.A)("tabs__item","tabItem_LNqP",t?.className,{"tabs__item--active":a===e}),children:n??e},e))})}function b({lazy:e,children:n,selectedValue:a}){let o=(Array.isArray(n)?n:[n]).filter(Boolean);if(e){let e=o.find(e=>e.props.value===a);return e?(0,t.cloneElement)(e,{className:(0,i.A)("margin-top--md",e.props.className)}):null}return(0,r.jsx)("div",{className:"margin-top--md",children:o.map((e,n)=>(0,t.cloneElement)(e,{key:n,hidden:e.props.value!==a}))})}function v(e){let n=function(e){let n,{defaultValue:a,queryString:r=!1,groupId:i}=e,o=function(e){let{values:n,children:a}=e;return(0,t.useMemo)(()=>{let e=n??h(a).map(({props:{value:e,label:n,attributes:a,default:r}})=>({value:e,label:n,attributes:a,default:r})),r=(0,d.XI)(e,(e,n)=>e.value===n.value);if(r.length>0)throw Error(`Docusaurus error: Duplicate values "${r.map(e=>e.value).join(", ")}" found in <Tabs>. Every value needs to be unique.`);return e},[n,a])}(e),[s,f]=(0,t.useState)(()=>(function({defaultValue:e,tabValues:n}){if(0===n.length)throw Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(e){if(!m({value:e,tabValues:n}))throw Error(`Docusaurus error: The <Tabs> has a defaultValue "${e}" but none of its children has the corresponding value. Available values are: ${n.map(e=>e.value).join(", ")}. If you intend to show no default tab, use defaultValue={null} instead.`);return e}let a=n.find(e=>e.default)??n[0];if(!a)throw Error("Unexpected error: 0 tabValues");return a.value})({defaultValue:a,tabValues:o})),[g,b]=function({queryString:e=!1,groupId:n}){let a=(0,l.W6)(),r=function({queryString:e=!1,groupId:n}){if("string"==typeof e)return e;if(!1===e)return null;if(!0===e&&!n)throw Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return n??null}({queryString:e,groupId:n});return[(0,u.aZ)(r),(0,t.useCallback)(e=>{if(!r)return;let n=new URLSearchParams(a.location.search);n.set(r,e),a.replace({...a.location,search:n.toString()})},[r,a])]}({queryString:r,groupId:i}),[v,C]=function({groupId:e}){let n=e?`docusaurus.tab.${e}`:null,[a,r]=(0,p.Dv)(n);return[a,(0,t.useCallback)(e=>{n&&r.set(e)},[n,r])]}({groupId:i}),x=m({value:n=g??v,tabValues:o})?n:null;return(0,c.A)(()=>{x&&f(x)},[x]),{selectedValue:s,selectValue:(0,t.useCallback)(e=>{if(!m({value:e,tabValues:o}))throw Error(`Can't select invalid tab value=${e}`);f(e),b(e),C(e)},[b,C,o]),tabValues:o}}(e);return(0,r.jsxs)("div",{className:(0,i.A)(o.G.tabs.container,"tabs-container","tabList__CuJ"),children:[(0,r.jsx)(g,{...n,...e}),(0,r.jsx)(b,{...n,...e})]})}function C(e){let n=(0,f.A)();return(0,r.jsx)(v,{...e,children:h(e.children)},String(n))}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.