AtomicFlags behaviour (atomic_flags v1.0.0)
Copy MarkdownFlags array implemented on top of :atomics module.
See __using__/1 and callback docs for more information about available option and usage.
Example module
All examples and doctests are using this module as an example
defmodule RoomFlags do use AtomicFlags, global: true, schema: [ :bin, tri: 3, lights: [ :off, :on ], coin: [ :head, :edge, :tails ] ] end
Summary
Callbacks
Cycles the value to the next one after steps. It is equivalent to executing next/2 for steps amount of times.
Gets the value of the flag.
Returns boolean/0 indicating if the term is a valid key for the flags array.
Can be used in guards
Creates an :atomics array which will store the information about flags.
Creates a global :atomics array. This function is generated only if global: true is
specified in the use macro call.
Cycles the value to the next one. Returns the new value, the one which is set as the result of the next call.
Resets the value to theif default ones. That is 0 for numerical flags or the first value for the enumerable flags.
Resets all values to theif default ones. That is 0 for numerical flags or the first value for the enumerable flags.
Sets the value of the flag.
Tries to perform the compare and exchange operation on the flag.
Returns :ok if operation is successful or {:error, value} when the from value
is different from the value which is inside the flags array.
Returns a Keyword.t/0 list representing the current state of the array.
Functions
Generates an implmenetation of the behaviour and all required functions for operation with the flags based on the specified schema.
Types
Callbacks
Cycles the value to the next one after steps. It is equivalent to executing next/2 for steps amount of times.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.cycle(flags, :lights, 1)
:on
iex> RoomFlags.cycle(flags, :lights, 2)
:on
iex> RoomFlags.cycle(flags, :lights, 3)
:off
iex> RoomFlags.cycle(flags, :lights, -3)
:on
Gets the value of the flag.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.get(flags, :lights)
:off
iex> RoomFlags.set(flags, :lights, :on)
iex> RoomFlags.get(flags, :lights)
:on
Returns boolean/0 indicating if the term is a valid key for the flags array.
Can be used in guards
Example:
iex> RoomFlags.is_key(:lights)
true
iex> RoomFlags.is_key(:definitely_not_a_key)
false
@callback new() :: :atomics.atomics_ref()
Creates an :atomics array which will store the information about flags.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.get(flags, :lights)
:off
@callback new_global() :: :atomics.atomics_ref()
Creates a global :atomics array. This function is generated only if global: true is
specified in the use macro call.
Example:
iex> RoomFlags.new_global()
iex> RoomFlags.get(:lights)
:off
Cycles the value to the next one. Returns the new value, the one which is set as the result of the next call.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.next(flags, :lights)
:on
iex> RoomFlags.next(flags, :lights)
:off
iex> RoomFlags.next(flags, :lights)
:on
iex> RoomFlags.get(flags, :lights)
:on
iex> flags = RoomFlags.new()
iex> RoomFlags.next(flags, :coin)
:edge
iex> RoomFlags.next(flags, :coin)
:tails
iex> RoomFlags.next(flags, :coin)
:head
iex> RoomFlags.next(flags, :coin)
:edge
iex> RoomFlags.next(flags, :coin)
:tails
Resets the value to theif default ones. That is 0 for numerical flags or the first value for the enumerable flags.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.set(flags, :lights, :on)
iex> RoomFlags.reset(flags, :lights)
iex> RoomFlags.get(flags, :lights)
:off
@callback reset_all(t()) :: :ok
Resets all values to theif default ones. That is 0 for numerical flags or the first value for the enumerable flags.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.set(flags, :lights, :on)
iex> RoomFlags.reset_all(flags)
iex> RoomFlags.get(flags, :lights)
:off
Sets the value of the flag.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.set(flags, :lights, :on)
iex> RoomFlags.get(flags, :lights)
:on
Tries to perform the compare and exchange operation on the flag.
Returns :ok if operation is successful or {:error, value} when the from value
is different from the value which is inside the flags array.
NOTE: You MUST specify options in the format
from: <fromvalue>, to: <tovalue>. In that specific order. This is done to explicitly note the direction of the swap.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.swap(flags, :lights, from: :off, to: :on)
:ok
iex> RoomFlags.get(flags, :lights)
:on
iex> RoomFlags.swap(flags, :lights, from: :off, to: :on)
{:error, :on}
Returns a Keyword.t/0 list representing the current state of the array.
Example:
iex> flags = RoomFlags.new()
iex> RoomFlags.to_list(flags)
[
bin: 0,
tri: 0,
lights: :off,
coin: :head
]
Functions
Generates an implmenetation of the behaviour and all required functions for operation with the flags based on the specified schema.
Options
:schema— A list, defining a schema. Example entries are::flag— defines a binary0or1flag under the name:flagstates: 3— defines a flag with values of0,1or2under the name:statesligths: [:on, :off]— defines an enumerable flag of values:onand:offunder the name:lights. Enumerable values can be of any type.
:only— A keyword list of{function, arity}which defines what functions or macros will be generated:except— A keyword list of{function, arity}which defines what functions or macros won't be generated:global— Iftrue, creates a functionnew_global/0which sets up a global array in persistent_term. And also generates adds this global array as the default for the first argument in every function and macro.Persistent term key
:persistent_termkey is__MODULE__.