#include <TWELITE>
#include <EASTL/fixed_string.h>
using namespace eastl;
using tstr128 = fixed_string<char, 127 + 1, false>;
tstr128 g_str1; // constructor is NOT called! needs to be initialized before use.
void setup() {
(void) new ((void*)&g_str1) tstr128("Hello World");
Serial << g_str1.c_str();
}
placement new のコードは少し乱雑に見えるため、補助関数mwx::pnew() を用意しています。先ほどの例を以下のように書き換えることができます。
(void) new ((void*)&g_str1) tstr128("Hello World");
// ↓
mwx::pnew(g_str1, "Hello World");
#include <TWELITE>
#include <EASTL/unique_ptr.h>
#include <EASTL/fixed_string.h>
using namespace eastl;
using tstr128 = fixed_string<char, 127 + 1, false>;
eastl::unique_ptr<tstr128> uq_str1;
void setup() {
uq_str1.reset(new tstr128("Hello World"));
if (uq_str1) { // true: object is stored.
Serial << uq_str1->c_str();
}
}
intrusive コンテナについて
以下の例は intrusive_list の要素定義例です。メンバーは int mX のみです。
struct IntNode : public eastl::intrusive_list_node {
int mX;
IntNode(int x = 0) : mX(x) { }
// no need to call super class's constructor eastl::intrusive_list_node()
};
inline bool operator<(const IntNode& a, const IntNode& b) { return a.mX < b.mX; }
inline bool operator>(const IntNode& a, const IntNode& b) { return a.mX > b.mX; }
Modified BSD License (3-Clause BSD license) see the file LICENSE in the project root.
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include<TWELITE>#include<EASTL/fixed_vector.h>#include<EASTL/intrusive_list.h>#include<EASTL/unique_ptr.h>// list element of intrusive_list.structIntNode:public eastl::intrusive_list_node { int mX;IntNode(int x =0) :mX(x) { }};inlinebooloperator>(constIntNode& a,constIntNode& b) { returna.mX >b.mX; } // for sortusingtpool= eastl::fixed_vector<IntNode,16,false>;usingtlst= eastl::intrusive_list<IntNode>;tpool pool; // instance pool.tlst l; // list objectvoidsetup() { mwx::pnew(pool); // prepare isntances mwx::pnew(l); // initialize (call constructor)pool.resize(5); // create 4 instances into pool // insert an IntNode element into List.int i =0;pool[i].mX =5; l.push_front(pool[i]); i++;pool[i].mX =1; l.push_front(pool[i]); i++;pool[i].mX =2; l.push_front(pool[i]); i++;pool[i].mX =4; l.push_front(pool[i]); i++;pool[i].mX =3; l.push_front(pool[i]); i++; Serial << crlf <<"init: ";for(auto& x : l) { Serial <<format(" %d",x.mX); }l.remove(pool[2]); Serial << crlf <<"remove: ";for(auto& x : l) { Serial <<format(" %d",x.mX); }l.sort(eastl::greater<tlst::value_type>()); Serial << crlf <<"sort: ";for(auto& x : l) { Serial <<format(" %d",x.mX); }}
#include<TWELITE>#include<EASTL/fixed_vector.h>#include<EASTL/bonus/ring_buffer.h>constsize_t N_RING_ELE =4; // element max for RING BUFFER.usingtvec= eastl::fixed_vector<uint8_t,N_RING_ELE+1,false>; // One extra element is required.usingtring= eastl::ring_buffer<uint8_t,tvec>;tring rb;voidsetup() { mwx::pnew(rb, N_RING_ELE);rb.push_front(5);rb.push_front(1);rb.push_front(4);rb.push_front(2); Serial << crlf; for (auto x : rb) Serial <<format(" %d", x);rb.push_front(3); Serial << crlf; for (auto x : rb) Serial <<format(" %d", x);rb.push_front(8); Serial << crlf; for (auto x : rb) Serial <<format(" %d", x);rb.push_front(9); Serial << crlf; for (auto x : rb) Serial <<format(" %d", x); Serial << crlf <<format("back=%d",rb.back()) << crlf;rb.pop_back();for (auto x : rb) Serial <<format(" %d", x);}
usingtmap= eastl::intrusive_hash_multimap<uint8_t,IntNode,N_BUCKET_CT>;... // find emelents by key `c'auto ip =mp.equal_range(uint8_t(c));for(auto&& it =ip.first; it !=ip.second; it++) {it->m_func(); }