Skip to content

Commit

Permalink
PlaceholderInit added
Browse files Browse the repository at this point in the history
  • Loading branch information
pstolarz committed Feb 15, 2025
1 parent 4b8f73f commit f292a54
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 38 deletions.
9 changes: 4 additions & 5 deletions examples/arduino/DallasTemperature/DallasTemperature.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2022,2024 Piotr Stolarz
* Copyright (c) 2019-2022,2024,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -164,11 +164,10 @@ void loop()
/* single sensor environment */

/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
* Zero-initialized scratchpad placeholder is static to allow reuse of
* the associated sensor id while reissuing readScratchpadSingle() calls.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
static PlaceholderInit<DSTherm::Scratchpad> scrpd;

OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
Expand Down
9 changes: 4 additions & 5 deletions examples/esp-idf/DallasTemperature/main/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022,2024 Piotr Stolarz
* Copyright (c) 2022,2024,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -128,11 +128,10 @@ void loop()
/* single sensor environment */

/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
* Zero-initialized scratchpad placeholder is static to allow reuse of
* the associated sensor id while reissuing readScratchpadSingle() calls.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
static PlaceholderInit<DSTherm::Scratchpad> scrpd;

OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
Expand Down
9 changes: 4 additions & 5 deletions examples/mbed-os/DallasTemperature/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022,2024 Piotr Stolarz
* Copyright (c) 2022,2024,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -134,11 +134,10 @@ void loop()
/* single sensor environment */

/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
* Zero-initialized scratchpad placeholder is static to allow reuse of
* the associated sensor id while reissuing readScratchpadSingle() calls.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
static PlaceholderInit<DSTherm::Scratchpad> scrpd;

OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
Expand Down
9 changes: 4 additions & 5 deletions examples/pico-sdk/DallasTemperature/DallasTemperature.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022,2024 Piotr Stolarz
* Copyright (c) 2022,2024,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -160,11 +160,10 @@ void loop()
/* single sensor environment */

/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
* Zero-initialized scratchpad placeholder is static to allow reuse of
* the associated sensor id while reissuing readScratchpadSingle() calls.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
static PlaceholderInit<DSTherm::Scratchpad> scrpd;

OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OneWireNg_CurrentPlatform KEYWORD1
DSTherm KEYWORD1
MAX31850 KEYWORD1
Placeholder KEYWORD1
PlaceholderInit KEYWORD1

Id KEYWORD3
ErrorCode KEYWORD3
Expand Down
5 changes: 2 additions & 3 deletions src/drivers/DSTherm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022,2024 Piotr Stolarz
* Copyright (c) 2021,2022,2024,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand Down Expand Up @@ -369,8 +369,7 @@ class DSTherm
* is used and the @c readSingleId() is not called. To avoid ambiguous
* behavior of initial call of the routine resulted from using uninitialized
* memory of the scratchpad placeholder, it's recommended to initialize
* the placeholder with zeroes. See @c Init parameter of @ref Placeholder
* template.
* the placeholder with zeroes. @see PlaceholderInit.
* - If @c reuseId is @c false, the routine calls @c readSingleId() every
* time to scan the bus for a connected sensor.
*
Expand Down
43 changes: 28 additions & 15 deletions src/utils/Placeholder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021,2022 Piotr Stolarz
* Copyright (c) 2021,2022,2025 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
Expand All @@ -12,19 +12,21 @@

#ifndef __OWNG_PLACEHOLDER__
#define __OWNG_PLACEHOLDER__

#include <stdint.h>
#include <string.h> /* memset */
#include "platform/Platform_New.h"

/**
* Simple placeholder template with automatic conversion to a reference or a
* pointer of the stored object. Overloaded @c operator&() and @c operator*()
* allow to retrieve casted address and a reference to the stored object.
* @c operator->() enables accessing placeholded object's members.
*/
template<class T, bool Init = false>
class Placeholder

#include <stdint.h>
#include <string.h> /* memset */
#include "platform/Platform_New.h"

namespace {

template<class T, uint8_t B = 0, bool Init = false>
class Placeholder_priv
{
public:
T *operator&() {
Expand All @@ -51,16 +53,27 @@ class Placeholder
ALLOC_ALIGNED uint8_t _buf[sizeof(T)];
};

/**
* Zero initialized @c Placeholder specialization.
*/
template<class T>
class Placeholder<T, true>: public Placeholder<T, false>
template<class T, uint8_t B>
class Placeholder_priv<T, B, true>: public Placeholder_priv<T, B, false>
{
public:
Placeholder() {
memset(this->_buf, 0, sizeof(this->_buf));
Placeholder_priv() {
memset(this->_buf, B, sizeof(this->_buf));
}
};

} /* namespace */

/**
* Uninitialized placeholder - placeholder's memory not initialized.
*/
template<class T>
struct Placeholder: Placeholder_priv<T> {};

/**
* Initialized placeholder - placeholder's memory filled by @c B (0 by default).
*/
template<class T, uint8_t B = 0>
struct PlaceholderInit: Placeholder_priv<T, B, true> {};

#endif /* __OWNG_PLACEHOLDER__ */

0 comments on commit f292a54

Please sign in to comment.