From 003ba3f4d687782a9104e22876ca41cca94d3ce5 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Thu, 4 Feb 2021 17:00:40 +0000 Subject: [PATCH 01/14] . working on ib changes, periodic changes . dont finish order on the inbound more changes more modifications . . starting on timezone stuff make exception explicit more ib updates a few tweaks tweaks deal with futures fixes --- Makefile | 6 +- aat/config/parser.py | 6 + aat/core/data/error.py | 5 + aat/core/data/order.py | 6 + aat/core/data/trade.py | 13 +- aat/engine/dispatch/execution/execution.py | 157 ++++++++++++++---- aat/engine/dispatch/order_entry.py | 36 ++-- aat/engine/dispatch/periodic.py | 14 +- aat/engine/dispatch/utils.py | 36 +++- aat/engine/engine.py | 162 +++++++++++------- aat/exchange/generic/csv.py | 4 +- aat/exchange/public/ib/ib.py | 163 ++++++++++++++----- aat/exchange/public/iex.py | 3 +- aat/exchange/test/harness.py | 30 +++- aat/strategy/sample/csv/readonly.py | 4 +- aat/strategy/sample/csv/readonly_periodic.py | 4 +- aat/strategy/sample/csv/received.py | 1 + aat/strategy/utils.py | 20 ++- aat/tests/exchange/test_ib_race.py | 110 +++++++++++++ 19 files changed, 604 insertions(+), 176 deletions(-) create mode 100644 aat/tests/exchange/test_ib_race.py diff --git a/Makefile b/Makefile index ccf22aa0..3c5fedd1 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,11 @@ testpycpp: ## Make unit tests testjs: ## Make js tests cd js; yarn test -testruns: testrunscsv testrunsiex ## Run a few examples as a live end-to-end test +testruns: testrunscsv testrunsiex testrunsother ## Run a few examples as a live end-to-end test + +testrunsother: + $(PYTHON) -m aat.exchange.test.harness + $(PYTHON) -m aat.tests.exchange.test_ib_race testrunscsv: $(PYTHON) -m aat.strategy.sample.csv.readonly diff --git a/aat/config/parser.py b/aat/config/parser.py index 2db3a12f..26f10d11 100644 --- a/aat/config/parser.py +++ b/aat/config/parser.py @@ -3,6 +3,7 @@ import itertools import os import os.path +import pytz from configparser import ConfigParser from typing import Any, Dict, List, Union, TYPE_CHECKING @@ -43,6 +44,7 @@ def _args_to_dict(args: Any) -> dict: ret["general"]["trading_type"] = args.trading_type ret["general"]["load_accounts"] = args.load_accounts ret["general"]["api"] = args.api + ret["general"]["timezone"] = args.timezone ret["exchange"] = { "exchanges": list( _.split(",") for _ in itertools.chain.from_iterable(args.exchanges) @@ -113,6 +115,10 @@ def parseConfig(argv: list = None) -> dict: "--api", action="store_true", help="Enable HTTP server", default=False ) + parser.add_argument( + "--timezone", help="Timezone override", default="", choices=pytz.all_timezones + ) + parser.add_argument( "--load_accounts", action="store_true", diff --git a/aat/core/data/error.py b/aat/core/data/error.py index 75253460..be9a89c9 100644 --- a/aat/core/data/error.py +++ b/aat/core/data/error.py @@ -36,6 +36,11 @@ def __init__( def timestamp(self) -> int: return self.__timestamp + @timestamp.setter + def id(self, timestamp: datetime) -> None: + assert isinstance(timestamp, datetime) + self.__timestamp = timestamp + @property def type(self) -> DataType: return self.__type diff --git a/aat/core/data/order.py b/aat/core/data/order.py index cec98661..7c5f104f 100644 --- a/aat/core/data/order.py +++ b/aat/core/data/order.py @@ -200,6 +200,12 @@ def __repr__(self) -> str: def __eq__(self, other: object) -> bool: assert isinstance(other, Order) + + # id takes precedence + if self.id and self.id == other.id: + return True + + # else compare all fields return ( self.id == other.id and self.instrument == other.instrument diff --git a/aat/core/data/trade.py b/aat/core/data/trade.py index 6265b0de..f084a055 100644 --- a/aat/core/data/trade.py +++ b/aat/core/data/trade.py @@ -61,10 +61,6 @@ def __init__( # ******** # # Readonly # # ******** # - @property - def timestamp(self) -> datetime: - return self.taker_order.timestamp - @property def type(self) -> DataType: return self.__type @@ -108,6 +104,15 @@ def id(self, id: str) -> None: assert isinstance(id, (str, int)) self.__id = str(id) + @property + def timestamp(self) -> datetime: + return self.taker_order.timestamp + + @timestamp.setter + def timestamp(self, timestamp: datetime) -> None: + assert isinstance(timestamp, datetime) + self.taker_order.timestamp = timestamp + @property def maker_orders(self) -> List[Order]: # no setter diff --git a/aat/engine/dispatch/execution/execution.py b/aat/engine/dispatch/execution/execution.py index 8ccc7b1c..35f951da 100644 --- a/aat/engine/dispatch/execution/execution.py +++ b/aat/engine/dispatch/execution/execution.py @@ -1,5 +1,6 @@ from typing import TYPE_CHECKING, cast, Dict, List, Optional, Tuple +from aat import AATException from aat.core import Order, Event, Trade, ExchangeType from aat.core.handler import EventHandler from aat.exchange import Exchange @@ -16,8 +17,13 @@ def __init__(self) -> None: # map exchangetype to exchange instance self._exchanges: Dict[ExchangeType, Exchange] = {} + # for race conditions where trade/cancel comes before acknowledgement + self._pending_action: List[Order] = [] + # track which strategies generated which orders - self._pending_orders: Dict[str, Tuple[Order, Optional["Strategy"]]] = {} + self._pending_orders: Dict[ + str, Tuple[Order, Optional["Strategy"]] + ] = {} # lookup strategy by order # past orders self._past_orders: List[Order] = [] @@ -38,17 +44,55 @@ async def newOrder(self, strategy: Optional["Strategy"], order: Order) -> bool: if not exchange: raise Exception("Exchange not installed: {}".format(order.exchange)) + # stash in case execution occurs before newOrder returns + self._pending_action.append((order, strategy)) + + # tell exchange to submit ret = await exchange.newOrder(order) - self._pending_orders[order.id] = (order, strategy) + + if ret: + # if received, store in pending and return to caller + self._pending_orders[order.id] = (order, strategy) + + if (order, strategy) in self._pending_action: + # remove from pre pending + self._pending_action.remove((order, strategy)) + + # tell manager it was received/rejected + if ret: + await self._manager._onReceived(strategy, order) + else: + await self._manager._onRejected(strategy, order) + + # return to caller return ret async def cancelOrder(self, strategy: Optional["Strategy"], order: Order) -> bool: exchange = self._exchanges.get(order.exchange) if not exchange: - raise Exception("Exchange not installed: {}".format(order.exchange)) + raise AATException("Exchange not installed: {}".format(order.exchange)) + + # stash in case cancel occurs before cancelOrder returns + self._pending_action.append((order, strategy)) + # tell exchange to cancel ret = await exchange.cancelOrder(order) - self._pending_orders.pop(order.id, None) + + if ret: + # if cancelled, remove from pending and return to caller + self._pending_orders.pop(order.id, None) + + if (order, strategy) in self._pending_action: + # remove from pre pending + self._pending_action.remove((order, strategy)) + + # tell manager it was canceled/rejected + if ret: + await self._manager._onCanceled(strategy, order) + else: + await self._manager._onRejected(strategy, order) + + # return to caller return ret # ********************** @@ -57,47 +101,83 @@ async def cancelOrder(self, strategy: Optional["Strategy"], order: Order) -> boo async def onTrade(self, event: Event) -> None: """Match trade with order""" action: bool = False + ooo: bool = False # execution is out of order with receipt strat: Optional[EventHandler] = None + trade: Trade = event.target - trade: Trade = event.target # type: ignore + # if it comes with the order, use that + if trade.my_order: + action = True - for maker_order in trade.maker_orders: - if maker_order.id in self._pending_orders: + # lookup order + if trade.my_order.id in self._pending_orders: + # grab order and strategy from pending orders + order, strat = self._pending_orders[trade.my_order.id] + else: + # execution was out of order, look which orders are pending actions + lookup = [x for x in self._pending_action if x[0] == trade.my_order] + ooo = True + + if not lookup: + # no trace of this order, so bail + raise AATException( + "Exchange did not acknowledge order before trade!" + ) + + # TODO more than one? probably not due to setting of ID + order, strat = lookup[0] + + # remove from pending action, processing here + self._pending_action.remove((order, strat)) + + # add to pending for future lookups + if not order.finished(): + self._pending_orders[order.id] = (order, strat) + + # FIXME + trade.id = trade.my_order.id + + # otherwise derive from mapping + else: + for maker_order in trade.maker_orders: + if maker_order.id in self._pending_orders: + action = True + order, strat = self._pending_orders[maker_order.id] + + # TODO cleaner? + trade.my_order = order + # FIXME + trade.id = order.id + order.filled = maker_order.filled + break + + if trade.taker_order.id in self._pending_orders: action = True - order, strat = self._pending_orders[maker_order.id] + order, strat = self._pending_orders[trade.taker_order.id] # TODO cleaner? trade.my_order = order + # FIXME trade.id = order.id - order.filled = maker_order.filled - break - - if trade.taker_order.id in self._pending_orders: - action = True - order, strat = self._pending_orders[trade.taker_order.id] - - # TODO cleaner? - trade.my_order = order - trade.id = order.id - order.filled = trade.taker_order.filled + order.filled = trade.taker_order.filled if action: + if ooo: + # execution occurred before order acknowledge, + # so send receipt first + await self._manager._onReceived(cast("Strategy", strat), trade.my_order) + if order.side == Order.Sides.SELL: - # TODO ugly private method - await self._manager._onSold( - cast("Strategy", strat), cast(Trade, event.target) - ) + await self._manager._onSold(cast("Strategy", strat), trade) else: - # TODO ugly private method - await self._manager._onBought( - cast("Strategy", strat), cast(Trade, event.target) - ) + await self._manager._onBought(cast("Strategy", strat), trade) if order.finished(): - del self._pending_orders[order.id] + self._pending_orders.pop(order.id, None) + self._past_orders.append(order) async def onCancel(self, event: Event) -> None: - canceled_order: Order = event.target # type: ignore + canceled_order: Order = event.target if canceled_order.id in self._pending_orders: order, strat = self._pending_orders[canceled_order.id] @@ -106,7 +186,24 @@ async def onCancel(self, event: Event) -> None: # TODO ugly private method await self._manager._onCanceled(cast("Strategy", strat), order) - del self._pending_orders[canceled_order.id] + self._pending_orders.pop(canceled_order.id, None) + + else: + # cancel was out of order, look which orders are pending actions + lookup = [x for x in self._pending_action if x[0] == order] + + if not lookup: + # no trace of this order, so bail + # raise AATException("Exchange did not acknowledge order before cancel!") + ... # TODO + + # TODO more than one? probably not due to setting of ID + order, strat = lookup[0] + + # remove from pending action, processing here + self._pending_action.remove((order, strat)) + + await self._manager._onCanceled(cast("Strategy", strat), order) async def onOpen(self, event: Event) -> None: # TODO diff --git a/aat/engine/dispatch/order_entry.py b/aat/engine/dispatch/order_entry.py index 0179f7c8..5f740c05 100644 --- a/aat/engine/dispatch/order_entry.py +++ b/aat/engine/dispatch/order_entry.py @@ -35,7 +35,7 @@ async def _onBought(self, strategy: "Strategy", trade: Trade) -> None: # push event to loop ev = Event(type=Event.Types.BOUGHT, target=trade) - self._engine.pushTargetedEvent(strategy, ev) + await self._engine.pushTargetedEvent(strategy, ev) # synchronize state when engine processes this self._alerted_events[ev] = (strategy, trade.my_order) @@ -47,38 +47,38 @@ async def _onSold(self, strategy: "Strategy", trade: Trade) -> None: # push event to loop ev = Event(type=Event.Types.SOLD, target=trade) - self._engine.pushTargetedEvent(strategy, ev) + await self._engine.pushTargetedEvent(strategy, ev) # synchronize state when engine processes this self._alerted_events[ev] = (strategy, trade.my_order) # TODO ugly private method - async def _onReceived(self, strategy: "Strategy", order: Order) -> None: # push event to loop ev = Event(type=Event.Types.RECEIVED, target=order) - self._engine.pushTargetedEvent(strategy, ev) - # synchronize state when engine processes this self._alerted_events[ev] = (strategy, order) + await self._engine.pushTargetedEvent(strategy, ev) async def _onCanceled(self, strategy: "Strategy", order: Order) -> None: # push event to loop ev = Event(type=Event.Types.CANCELED, target=order) - self._engine.pushTargetedEvent(strategy, ev) - # synchronize state when engine processes this self._alerted_events[ev] = (strategy, order) + await self._engine.pushTargetedEvent(strategy, ev) async def _onRejected(self, strategy: "Strategy", order: Order) -> None: # push event to loop ev = Event(type=Event.Types.REJECTED, target=order) - self._engine.pushTargetedEvent(strategy, ev) + self._alerted_events[ev] = (strategy, order) + await self._engine.pushTargetedEvent(strategy, ev) + + # always return false + return False # ********************* # Order Entry Methods * # ********************* - async def newOrder(self, strategy: "Strategy", order: Order) -> bool: """helper method, defers to buy/sell""" # ensure has list @@ -103,26 +103,14 @@ async def newOrder(self, strategy: "Strategy", order: Order) -> bool: # was this trade allowed? if approved: # send to be executed - received = await self._order_mgr.newOrder(strategy, order) - - if received: - await self._onReceived(strategy, order) - return received + return await self._order_mgr.newOrder(strategy, order) # raise onRejected - await self._onRejected(strategy, order) - return False + return await self._onRejected(strategy, order) async def cancelOrder(self, strategy: "Strategy", order: Order) -> bool: """cancel an open order""" - ret = await self._order_mgr.cancelOrder(strategy, order) - if ret: - await self._onCanceled(strategy, order) - return ret - - # TODO something else? - await self._onRejected(strategy, order) - return False + return await self._order_mgr.cancelOrder(strategy, order) def orders( self, diff --git a/aat/engine/dispatch/periodic.py b/aat/engine/dispatch/periodic.py index 55f8a4e2..79c7aa6a 100644 --- a/aat/engine/dispatch/periodic.py +++ b/aat/engine/dispatch/periodic.py @@ -3,7 +3,7 @@ from datetime import datetime from typing import Awaitable, Callable, List, Optional -from temporalcache.utils import should_expire # type: ignore +from temporalcache.utils import should_expire, calc # type: ignore class Periodic(object): @@ -15,6 +15,7 @@ def __init__( second: Optional[int], minute: Optional[int], hour: Optional[int], + interval: bool = False, ) -> None: self._loop = loop self._function: Callable[..., Awaitable[None]] = function @@ -27,6 +28,7 @@ def __init__( self._last = last_ts self._continue = True + self._interval = interval @property def second(self) -> Optional[int]: @@ -46,6 +48,13 @@ def stop(self) -> None: def expires(self, timestamp: datetime) -> bool: if (timestamp - self._last).total_seconds() < 1: return False + if self._interval: + total = (timestamp - self._last).total_seconds() + if (timestamp - self._last).total_seconds() < 1: + return False + return total > calc( + self.second or 1, self.minute or 0, self.hour or 0, 0, 0, 0, 0 + ) return should_expire(self._last, timestamp, self.second, self.minute, self.hour) async def execute(self, timestamp: datetime) -> Optional[Future]: @@ -70,6 +79,9 @@ def periodicIntervals(self) -> int: """ ret = 3600 for p in self._periodics: + if p._interval: + # 1 for all interval + return 1 if p.second is None: # if any secondly, return 0 right away return 1 diff --git a/aat/engine/dispatch/utils.py b/aat/engine/dispatch/utils.py index f73702ff..412fd6d5 100644 --- a/aat/engine/dispatch/utils.py +++ b/aat/engine/dispatch/utils.py @@ -114,11 +114,39 @@ async def _wrapper() -> Any: return _wrapper def periodic( + self, + function: Callable, + seconds: int = 0, + minutes: int = 0, + hours: int = 0, + ): + """run a function periodically: + if the amount of time between previous call and current call + is more than `seconds` seconds + `minutes` minutes + `hours` hours, + then reexecute + """ + return self._periodic(function, seconds, minutes, hours, interval=True) + + def at( + self, + function: Callable, + second: Union[int, str] = 0, + minute: Union[int, str] = "*", + hour: Union[int, str] = "*", + ): + """run a function at a certain point in time: + e.g. run it on every `second` second every minute of every hour + So 2, "*", "*", would run 1:00:02, 1:01:02, etc + """ + return self._periodic(function, second, minute, hour, interval=False) + + def _periodic( self, function: Callable, second: Union[int, str] = 0, minute: Union[int, str] = "*", hour: Union[int, str] = "*", + interval: bool = False, ) -> Periodic: """periodically run a given async function. NOTE: precise timing is NOT guaranteed due to event loop scheduling.""" @@ -161,7 +189,13 @@ def periodic( # End Validation periodic = Periodic( - self.loop(), self._engine._latest, function, second, minute, hour # type: ignore + self.loop(), + self._engine._latest, + function, + second, + minute, + hour, + interval, ) self._periodics.append(periodic) return periodic diff --git a/aat/engine/engine.py b/aat/engine/engine.py index a7cdd9dd..df84b66c 100644 --- a/aat/engine/engine.py +++ b/aat/engine/engine.py @@ -2,6 +2,7 @@ import inspect import os import os.path +import pytz from aiostream.stream import merge # type: ignore from collections import deque @@ -69,6 +70,12 @@ class TradingEngine(Application): verbose = Bool(default_value=True) api = Bool(default_value=False) port = Unicode(default_value="8080", help="Port to run on").tag(config=True) + tz = Instance( + klass=pytz.BaseTzInfo, + default_value=None, + allow_none=True, + help="Timezone to localize to", + ).tag(config=True) event_loop = Instance(klass=asyncio.events.AbstractEventLoop) executor = Instance(klass=ThreadPoolExecutor, args=(4,), kwargs={}) @@ -119,6 +126,13 @@ def __init__(self, **config: dict) -> None: # enable API access? self.api = bool(int(config.get("general", {}).get("api", self.api))) + # override timezome + self.tz = ( + pytz.timezone(config.get("general", {}).get("timezone", None)) + if config.get("general", {}).get("timezone", None) + else None + ) + # Trading type self.trading_type = TradingType( config.get("general", {}).get("trading_type", "simulation").upper() @@ -152,7 +166,9 @@ def __init__(self, **config: dict) -> None: } # setup `now` handler for backtest - self._latest = datetime.fromtimestamp(0) if self._offline() else datetime.now() + self._latest = ( + datetime.fromtimestamp(0, tz=self.tz) if self._offline() else datetime.now() + ) # register internal management event handler before all strategy handlers self.registerHandler(self.manager) @@ -318,26 +334,32 @@ def registerCallback( return True return False - def pushEvent(self, event: Event) -> None: + async def pushEvent(self, event: Event) -> None: """push non-exchange event into the queue""" - self._queued_events.append(event) + await self._queued_events.put(event) - def pushTargetedEvent(self, strategy: Strategy, event: Event) -> None: + async def pushTargetedEvent(self, strategy: Strategy, event: Event) -> None: """push non-exchange event targeted to a specific strat into the queue""" - self._queued_targeted_events.append((strategy, event)) + await self._queued_targeted_events.put((event, strategy)) + + async def _wraptick(self, ticker) -> Event: + async for ev in ticker: + yield ev + yield Event(type=EventType.EXIT, target=None) async def run(self) -> None: """run the engine""" # setup future queue - self._queued_events: Deque[Event] = deque() - self._queued_targeted_events: Deque[Tuple[Strategy, Event]] = deque() + self._queued_events: Deque[Event] = asyncio.Queue() + self._queued_targeted_events: Deque[Tuple[Strategy, Event]] = asyncio.Queue() + self._futures: Deque[asyncio.Future] = deque() # await all connections await asyncio.gather( - *(asyncio.create_task(exch.connect()) for exch in self.exchanges) + *(asyncio.ensure_future(exch.connect()) for exch in self.exchanges) ) await asyncio.gather( - *(asyncio.create_task(exch.instruments()) for exch in self.exchanges) + *(asyncio.ensure_future(exch.instruments()) for exch in self.exchanges) ) # send start event to all callbacks @@ -347,16 +369,40 @@ async def run(self) -> None: # Main event loop # **************** # async with merge( + self._tick_queued_events(), + self._tick_queued_targeted_events(), *( - exch.tick() + self._wraptick(exch.tick()) for exch in self.exchanges + [self] if inspect.isasyncgenfunction(exch.tick) - ) + ), ).stream() as stream: # stream through all events async for event in stream: + # unpack targetted events + if isinstance(event, tuple): + event, strategy = event + else: + strategy = None + + # if done event + if event.type == EventType.EXIT: + break + # TODO move out of critical path if self._offline(): + # handle timezone + if ( + hasattr(event, "target") + and hasattr(event.target, "timestamp") + and self._latest.tzinfo + and not event.target.timestamp.tzinfo + ): + # assume in local time + event.target.timestamp = event.target.timestamp.replace( + tzinfo=self._latest.tzinfo + ) + # inject periodics # TODO optimize # Manager should keep track of the intervals for its periodics, @@ -364,7 +410,7 @@ async def run(self) -> None: # live engine's `tick` method does below). Instead we can just # calculate exactly the intervals if ( - self._latest != datetime.fromtimestamp(0) + self._latest != datetime.fromtimestamp(0, tz=self.tz) and hasattr(event, "target") and hasattr(event.target, "timestamp") ): @@ -378,6 +424,9 @@ async def run(self) -> None: / intervals ) ): + print( + "{} - {}".format(event.target.timestamp, self._latest) + ) self._latest = self._latest + timedelta( seconds=1 * intervals ) @@ -394,7 +443,7 @@ async def run(self) -> None: ) # tick exchange event to handlers - await self.processEvent(event) + self._futures.extend(await self.processEvent(event, strategy)) # TODO move out of critical path if self._offline(): @@ -407,47 +456,43 @@ async def run(self) -> None: ) else: # use now - self._latest = datetime.now() - - # process any secondary events - while self._queued_events: - event = self._queued_events.popleft() - await self.processEvent(event) - - # process any secondary callback-targeted events (e.g. order fills) - # these need to route to a specific callback, - # rather than all callbacks - while self._queued_targeted_events: - strat, event = self._queued_targeted_events.popleft() - - # send to the generating strategy - await self.processEvent(event, strat) + self._latest = datetime.now(tz=self.tz) # process any periodics - periodic_result = await asyncio.gather( - *( - asyncio.create_task(p.execute(self._latest)) - for p in self.manager.periodics() - if p.expires(self._latest) - ) - ) - - exceptions = [r for r in periodic_result if r.exception()] - if any(exceptions): - raise exceptions[0].exception() + self._futures.extend([asyncio.create_task(p.execute(self._latest)) + for p in self.manager.periodics() + if p.expires(self._latest)]) + + remaining_futures = deque() + for fut in self._futures: + if fut.done(): + # trigger exception if necessary + fut.result() + else: + remaining_futures.append(fut) + self._futures = remaining_futures # Before engine shutdown, send an exit event await self.processEvent(Event(type=EventType.EXIT, target=None)) + async def _tick_queued_events(self): + while True: + yield await self._queued_events.get() + + async def _tick_queued_targeted_events(self): + while True: + yield await self._queued_targeted_events.get() + async def processEvent(self, event: Event, strategy: Strategy = None) -> None: """send an event to all registered event handlers Arguments: event (Event): event to send """ + ret = [] if event.type == EventType.HEARTBEAT: # ignore heartbeat - return + return ret for callback, handler in self._handler_subscriptions[event.type]: # TODO make cleaner? move to somewhere not in critical path? @@ -469,7 +514,7 @@ async def processEvent(self, event: Event, strategy: Strategy = None) -> None: continue try: - await callback(event) + ret.append(asyncio.ensure_future(callback(event))) except KeyboardInterrupt: raise except SystemExit: @@ -478,18 +523,21 @@ async def processEvent(self, event: Event, strategy: Strategy = None) -> None: if event.type == EventType.ERROR: # don't infinite error raise - await self.processEvent( - Event( - type=EventType.ERROR, - target=Error( - target=event, - handler=handler, - callback=callback, - exception=e, - ), - ) - ) await asyncio.sleep(1) + return [asyncio.ensure_future( + self.processEvent( + Event( + type=EventType.ERROR, + target=Error( + target=event, + handler=handler, + callback=callback, + exception=e, + ), + ) + ) + )] + return ret async def tick(self) -> AsyncGenerator: """helper method to ensure periodic methods execute periodically in absence @@ -497,6 +545,9 @@ async def tick(self) -> AsyncGenerator: if self._offline(): # periodics injected manually, see main event loop above + while True: + yield Event(type=EventType.HEARTBEAT, target=None) + await asyncio.sleep(0) return while True: @@ -509,7 +560,7 @@ def now(self) -> datetime: return ( self._latest if self.trading_type == TradingType.BACKTEST - else datetime.now() + else datetime.now(tz=self.tz) ) def start(self) -> None: @@ -522,7 +573,6 @@ def start(self) -> None: except KeyboardInterrupt: pass + # FIXME double sending due to wraptick # send exit event to all callbacks - asyncio.ensure_future( - self.processEvent(Event(type=EventType.EXIT, target=None)) - ) + # self.event_loop.run_until_complete(self.processEvent(Event(type=EventType.EXIT, target=None))) diff --git a/aat/exchange/generic/csv.py b/aat/exchange/generic/csv.py index f7a0549d..f96d23b7 100644 --- a/aat/exchange/generic/csv.py +++ b/aat/exchange/generic/csv.py @@ -77,11 +77,11 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] price=order.price, taker_order=order, maker_orders=[], - my_order=order, + my_order=order, # FIXME this isnt technically necessary as + # the engine should do this automatically ) yield Event(type=EventType.TRADE, target=t) - await asyncio.sleep(0) async def cancelOrder(self, order: Order) -> bool: # Can't cancel, orders execute immediately diff --git a/aat/exchange/public/ib/ib.py b/aat/exchange/public/ib/ib.py index d7f94642..6cdbdd09 100644 --- a/aat/exchange/public/ib/ib.py +++ b/aat/exchange/public/ib/ib.py @@ -1,7 +1,7 @@ import asyncio import threading from datetime import datetime -from queue import Empty, Queue +from queue import Queue from random import randint from typing import Any, AsyncGenerator, Dict, List, Set, Tuple, Union @@ -32,7 +32,7 @@ def __init__( account_position_queue: Queue, ) -> None: EClient.__init__(self, self) - self.nextOrderId: int = 0 + self.nextOrderId: int = 1 self.nextReqId = 1 # account # if more than one @@ -155,21 +155,38 @@ def execDetailsEnd(self, reqId: int) -> None: # TODO? def error(self, reqId: int, errorCode: int, errorString: str) -> None: - super().error(reqId, errorCode, errorString) - if errorCode in (201,): + if errorCode in ( + 110, # The price does not conform to the minimum price variation for this contract. + 201, # Order rejected - Reason: + ): self._order_event_queue.put( dict( orderId=reqId, status="Rejected", ) ) - elif errorCode in (202,): + elif errorCode in ( + 136, # This order cannot be cancelled. + 161, # Cancel attempted when order is not in a cancellable state. Order permId = + 10148, # OrderId that needs to be cancelled can not be cancelled, state: + ): self._order_event_queue.put( dict( orderId=reqId, - status="Cancelled", + status="RejectedCancel", ) ) + elif errorCode in (202,): # Order cancelled - Reason: + ... + # also sends event, don't need to handle error here + # self._order_event_queue.put( + # dict( + # orderId=reqId, + # status="Cancelled", + # ) + # ) + else: + super().error(reqId, errorCode, errorString) def tickPrice(self, reqId: int, tickType: int, price: float, attrib: str) -> None: # TODO implement more of order book @@ -231,11 +248,13 @@ def __init__( self._orders: Dict[str, Order] = {} # map order id to received event - self._order_received_map: Dict[str, asyncio.Event] = {} + self._order_received_map_set: Dict[str, asyncio.Event] = {} + self._order_received_map_get: Dict[str, asyncio.Event] = {} self._order_received_res: Dict[str, bool] = {} # map order id to cancelled event - self._order_cancelled_map: Dict[str, asyncio.Event] = {} + self._order_cancelled_map_set: Dict[str, asyncio.Event] = {} + self._order_cancelled_map_get: Dict[str, asyncio.Event] = {} self._order_cancelled_res: Dict[str, bool] = {} # track "finished" orders so we can ignore them @@ -312,22 +331,89 @@ async def lookup(self, instrument: Instrument) -> List[Instrument]: async def subscribe(self, instrument: Instrument) -> None: self._api.subscribeMarketData(instrument) - def _send_order_received(self, orderId: str, ret: bool) -> None: + def _create_order_received(self, orderId: str) -> None: + # create initial event + self._order_received_map_get[orderId] = asyncio.Event() + self._order_received_map_set[orderId] = asyncio.Event() + + async def _send_order_received( + self, orderId: str, ret: bool, waitfor: bool = True + ) -> None: # set result self._order_received_res[orderId] = ret - def _send_cancel_received(self, orderId: str, ret: bool) -> None: + # if setter exists, set it + if orderId in self._order_received_map_set: + self._order_received_map_set[orderId].set() + + # let the event be consumed + await asyncio.sleep(0) + + # wait for result to be consumed + await self._order_received_map_get[orderId].wait() + + async def _send_cancel_received( + self, orderId: str, ret: bool, waitfor: bool = True + ) -> None: # set result self._order_cancelled_res[orderId] = ret + # if setter exists, set it + if orderId in self._order_cancelled_map_set: + self._order_cancelled_map_set[orderId].set() + + # let the event be consumed + await asyncio.sleep(0) + + # wait for result to be consumed + await self._order_cancelled_map_get[orderId].wait() + async def _consume_order_received(self, orderId: str) -> bool: - while orderId not in self._order_received_res: - await asyncio.sleep(0.1) + # if already received result + if orderId in self._order_received_res: + return self._order_received_res.pop(orderId) + + # if already waiting for result, reject + if orderId in self._order_received_map_set: + return False + + # initialize events + self._order_received_map_get[orderId] = asyncio.Event() + self._order_received_map_set[orderId] = asyncio.Event() + + # wait for it to be set + await self._order_received_map_set[orderId].wait() + + # trigger getter + self._order_received_map_get[orderId].set() + + # let setter finish return self._order_received_res.pop(orderId) async def _consume_cancel_received(self, orderId: str) -> bool: - while orderId not in self._order_cancelled_res: - await asyncio.sleep(0.1) + # if already received result + if orderId in self._order_cancelled_res: + return self._order_cancelled_res.pop(orderId) + + # if already waiting for result, reject + if orderId in self._order_cancelled_map_set: + return False + + # initialize events + self._order_cancelled_map_get[orderId] = asyncio.Event() + self._order_cancelled_map_set[orderId] = asyncio.Event() + + # wait for it to be set + await self._order_cancelled_map_set[orderId].wait() + + # trigger getter + self._order_cancelled_map_get[orderId].set() + + # if finished, add to finished + if self._order_cancelled_res[orderId]: + self._finished_orders.add(orderId) + + # let setter finish return self._order_cancelled_res.pop(orderId) async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] @@ -335,11 +421,7 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] while True: # clear order events while self._order_event_queue.qsize() > 0: - try: - order_data = self._order_event_queue.get_nowait() - except Empty: - await asyncio.sleep(0.1) - continue + order_data = self._order_event_queue.get() status = order_data["status"] order = self._orders[str(order_data["orderId"])] if status in ( @@ -354,22 +436,20 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] elif status in ("Inactive",): self._finished_orders.add(order.id) - self._send_order_received(order.id, False) - self._send_cancel_received(order.id, False) elif status in ("Rejected",): self._finished_orders.add(order.id) - self._send_order_received(order.id, False) - await asyncio.sleep(0) + await self._send_order_received(order.id, False) + + elif status in ("RejectedCancel",): + await self._send_cancel_received(order.id, False) elif status in ("Submitted",): - self._send_order_received(order.id, True) - await asyncio.sleep(0) + await self._send_order_received(order.id, True) elif status in ("Cancelled",): self._finished_orders.add(order.id) - self._send_cancel_received(order.id, True) - await asyncio.sleep(0) + await self._send_cancel_received(order.id, True) elif status in ("Filled",): # this is the filled from orderStatus, but we @@ -391,10 +471,7 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] if order.finished(): self._finished_orders.add(order.id) - # if it was cancelled but already executed, clear out the wait - self._send_cancel_received(order.id, False) - - # create trade object + # create trade object t = Trade( volume=order_data["filled"], # type: ignore price=order_data["avgFillPrice"], # type: ignore @@ -403,21 +480,19 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] ) # set my order + # FIXME this isnt technically necessary as + # the engine should do this automatically t.my_order = order e = Event(type=EventType.TRADE, target=t) # if submitted was skipped, clear out the wait - self._send_order_received(order.id, True) + await self._send_order_received(order.id, True) yield e # clear market data events while self._market_data_queue.qsize() > 0: - try: - market_data = self._market_data_queue.get_nowait() - except Empty: - await asyncio.sleep(0.1) - continue + market_data = self._market_data_queue.get() instrument: Instrument = market_data["instrument"] # type: ignore price: float = market_data["price"] # type: ignore o = AATOrder( @@ -465,14 +540,14 @@ async def newOrder(self, order: AATOrder) -> bool: _temp_id = str(self._api.nextOrderId) + # pre update order id since ib runs on thread + order.id = _temp_id + self._orders[order.id] = order + # send to IB - id = self._api.placeOrder(ibcontract, iborder) + self._api.placeOrder(ibcontract, iborder) # update order id - order.id = id - self._orders[order.id] = order - - # get result from IB return await self._consume_order_received(_temp_id) async def cancelOrder(self, order: AATOrder) -> bool: @@ -480,12 +555,12 @@ async def cancelOrder(self, order: AATOrder) -> bool: For MarketData-only, can just return None """ - # ignore if order not sujbmitted yet + # ignore if order not submitted yet if not order.id: return False # ignore if already finished - if order.id and order.id in self._finished_orders: + if order.id in self._finished_orders: return False # send to IB diff --git a/aat/exchange/public/iex.py b/aat/exchange/public/iex.py index 5aab3604..7eb225cc 100644 --- a/aat/exchange/public/iex.py +++ b/aat/exchange/public/iex.py @@ -308,7 +308,8 @@ def _callback(record: dict) -> None: price=order.price, taker_order=order, maker_orders=[], - my_order=order, + my_order=order, # FIXME this isnt technically necessary as + # the engine should do this automatically ) yield Event(type=EventType.TRADE, target=t) diff --git a/aat/exchange/test/harness.py b/aat/exchange/test/harness.py index bfb2a969..9f9688fd 100644 --- a/aat/exchange/test/harness.py +++ b/aat/exchange/test/harness.py @@ -31,7 +31,7 @@ async def connect(self) -> None: pass async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] - now = self._start + self._now = self._start for i in range(1000): if self._client_order: self._client_order.filled = self._client_order.volume @@ -41,7 +41,7 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] taker_order=self._client_order, maker_orders=[], ) - t.taker_order.timestamp = now + t.taker_order.timestamp = self._now self._client_order = None yield Event(type=EventType.TRADE, target=t) continue @@ -52,17 +52,18 @@ async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] Side.BUY, self._instrument, self.exchange(), - timestamp=now, + timestamp=self._now, filled=1, ) - t = Trade(1, i, o, []) + t = Trade(1, i, taker_order=o, maker_orders=[]) yield Event(type=EventType.TRADE, target=t) - now += timedelta(minutes=30) + self._now += timedelta(minutes=30) async def newOrder(self, order: Order) -> bool: order.id = str(self._id) self._id += 1 self._client_order = order + order.timestamp = self._now return True @@ -73,25 +74,36 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: self._trades: List[Trade] = [] async def onStart(self, event: Event) -> None: - self.periodic(self.onPeriodic, second=0, minute=30) + self.at(self.onPeriodic, second=0, minute=0) async def onTrade(self, event: Event) -> None: - pass + # print("onTrade {}".format(event.target.timestamp)) + ... async def onTraded(self, event: Event) -> None: + # print("onTraded {}".format(event.target.timestamp)) self._trades.append(event.target) # type: ignore async def onPeriodic(self, **kwargs: Any) -> None: + # print("onPeriodic {}".format(kwargs.get("timestamp"))) o = Order(1, 1, Side.BUY, self.instruments()[0], ExchangeType("testharness")) _ = await self.newOrder(o) self._orders.append(o) async def onExit(self, event: Event) -> None: + print( + len(self._orders), + len(self._trades), + self._trades[0].price, + self._trades[1].price, + self._trades[-1].price, + ) assert len(self._orders) == len(self._trades) assert len(self._trades) == 334 - assert self._trades[0].price == 2 - assert self._trades[1].price == 3 + assert self._trades[0].price == 1 + assert self._trades[1].price == 2 assert self._trades[-1].price == 999 + print("all good") if __name__ == "__main__": diff --git a/aat/strategy/sample/csv/readonly.py b/aat/strategy/sample/csv/readonly.py index 4267bbdb..2d0d5ed0 100644 --- a/aat/strategy/sample/csv/readonly.py +++ b/aat/strategy/sample/csv/readonly.py @@ -34,8 +34,10 @@ async def onExit(self, event: Event) -> None: cfg = parseConfig( [ "--trading_type", - "backtest", + "sandbox", "--load_accounts", + "--timezone", + "America/New_York", "--exchanges", "aat.exchange.generic:CSV,{}".format( os.path.join(os.path.dirname(__file__), "data", "aapl.csv") diff --git a/aat/strategy/sample/csv/readonly_periodic.py b/aat/strategy/sample/csv/readonly_periodic.py index 512aade6..f24ff5ee 100644 --- a/aat/strategy/sample/csv/readonly_periodic.py +++ b/aat/strategy/sample/csv/readonly_periodic.py @@ -17,7 +17,7 @@ async def onStart(self, event: Event) -> None: for i in self.instruments(): await self.subscribe(i) - self.periodic(self.onPeriodic, second=0, minute=0, hour=1) + self.at(self.onPeriodic, second=0, minute=0, hour=1) async def onTrade(self, event: Event) -> None: pprint(event) @@ -41,6 +41,8 @@ async def onPeriodic(self, **kwargs: Any) -> None: "--trading_type", "backtest", "--load_accounts", + "--timezone", + "America/New_York", "--exchanges", "aat.exchange.generic:CSV,{}".format( os.path.join(os.path.dirname(__file__), "data", "aapl.csv") diff --git a/aat/strategy/sample/csv/received.py b/aat/strategy/sample/csv/received.py index 1a763436..7407e976 100644 --- a/aat/strategy/sample/csv/received.py +++ b/aat/strategy/sample/csv/received.py @@ -62,4 +62,5 @@ async def onExit(self, event: Event) -> None: print(cfg) t = TradingEngine(**cfg) t.start() + print(t.strategies[0]._received_count) assert t.strategies[0]._received_count == 64 diff --git a/aat/strategy/utils.py b/aat/strategy/utils.py index 722ff4ee..d7a42ac8 100644 --- a/aat/strategy/utils.py +++ b/aat/strategy/utils.py @@ -119,6 +119,24 @@ async def book(self, instrument: Instrument) -> Optional[OrderBook]: return await self._manager.book(instrument) def periodic( + self, + function: Callable, + seconds: Union[int, str] = 0, + minutes: Union[int, str] = "*", + hours: Union[int, str] = "*", + ) -> Periodic: + """periodically run a given async function. NOTE: precise timing + is NOT guaranteed due to event loop scheduling. + + Args: + function (callable); function to call periodically + second (Union[int, str]); second to align periodic to, or '*' for every second + minute (Union[int, str]); minute to align periodic to, or '*' for every minute + hour (Union[int, str]); hour to align periodic to, or '*' for every hour + """ + return self._manager.periodic(function, seconds, minutes, hours) + + def at( self, function: Callable, second: Union[int, str] = 0, @@ -142,7 +160,7 @@ def periodic( periodic(0, 30, '*') periodic(0, 45, '*') """ - return self._manager.periodic(function, second, minute, hour) + return self._manager.at(function, second, minute, hour) def restrictTradingHours( self, diff --git a/aat/tests/exchange/test_ib_race.py b/aat/tests/exchange/test_ib_race.py new file mode 100644 index 00000000..a9875a7f --- /dev/null +++ b/aat/tests/exchange/test_ib_race.py @@ -0,0 +1,110 @@ +import asyncio +from datetime import datetime, timedelta +from typing import List, Any, AsyncGenerator, Optional +from aat import Strategy +from aat.config import EventType, InstrumentType, Side, TradingType +from aat.core import ExchangeType, Event, Instrument, Trade, Order +from aat.exchange import Exchange + + +class Harness(Exchange): + def __init__(self, trading_type: TradingType, verbose: bool) -> None: + super().__init__(ExchangeType("testharness")) + self._trading_type = trading_type + self._verbose = verbose + self._instrument = Instrument("Test.inst", InstrumentType.EQUITY) + + self._order: Optional[Order] = None + self._done: bool = False + + async def instruments(self) -> List[Instrument]: + """get list of available instruments""" + return [self._instrument] + + async def connect(self) -> None: + self._event = asyncio.Event() + + async def tick(self) -> AsyncGenerator[Any, Event]: # type: ignore[override] + while True: + if self._order: + self._order.filled = self._order.volume + t = Trade( + self._order.volume, + self._order.price, + taker_order=self._order, + maker_orders=[], + ) + t.my_order = self._order + print("yielding trade") + yield Event(type=EventType.TRADE, target=t) + self._order = None + self._event.set() + yield Event( + type=EventType.TRADE, + target=Trade( + 1, + 1, + taker_order=Order( + 1, + 1, + Side.BUY, + self._instrument, + ExchangeType("testharness"), + filled=1, + ), + maker_orders=[], + ), + ) + if self._done: + return + + async def newOrder(self, order: Order) -> bool: + order.id = 1 + self._order = order + print("waiting") + await self._event.wait() + self._done = True + return True + + +class TestStrategy(Strategy): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super(TestStrategy, self).__init__(*args, **kwargs) + self._receivedCount = 0 + self._order: Optional[Order] = None + + async def onTrade(self, event: Event) -> None: + if not self._order: + self._order = Order( + 1, 1, Side.BUY, self.instruments()[0], ExchangeType("testharness") + ) + await self.newOrder(self._order) + + async def onTraded(self, order: Order) -> None: + print("onTraded") + + async def onReceived(self, order: Order) -> None: + print("onReceived") + self._receivedCount += 1 + + async def onExit(self, event: Event) -> None: + assert self._receivedCount == 1 + print("all set!") + + +if __name__ == "__main__": + from aat import TradingEngine, parseConfig + + cfg = parseConfig( + [ + "--trading_type", + "backtest", + "--exchanges", + "aat.tests.exchange.test_ib_race:Harness", + "--strategies", + "aat.tests.exchange.test_ib_race:TestStrategy", + ] + ) + print(cfg) + t = TradingEngine(**cfg) + t.start() From 4b204abc7f2a8d9890647bf6d1467e48b023e21d Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Wed, 24 Feb 2021 22:47:14 -0500 Subject: [PATCH 02/14] dont remove on cancel unless it was actually cancelled --- aat/engine/dispatch/execution/execution.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/aat/engine/dispatch/execution/execution.py b/aat/engine/dispatch/execution/execution.py index 35f951da..d32f5311 100644 --- a/aat/engine/dispatch/execution/execution.py +++ b/aat/engine/dispatch/execution/execution.py @@ -82,15 +82,15 @@ async def cancelOrder(self, strategy: Optional["Strategy"], order: Order) -> boo # if cancelled, remove from pending and return to caller self._pending_orders.pop(order.id, None) - if (order, strategy) in self._pending_action: - # remove from pre pending - self._pending_action.remove((order, strategy)) + if (order, strategy) in self._pending_action: + # remove from pre pending + self._pending_action.remove((order, strategy)) # tell manager it was canceled/rejected - if ret: - await self._manager._onCanceled(strategy, order) - else: - await self._manager._onRejected(strategy, order) + await self._manager._onCanceled(strategy, order) + + else: + await self._manager._onRejected(strategy, order) # return to caller return ret From 8b3addad044400ab8a842fa7c4c8031730abe053 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Wed, 24 Feb 2021 23:31:03 -0500 Subject: [PATCH 03/14] fix lint and annotate --- aat/core/data/error.py | 2 +- aat/engine/dispatch/execution/execution.py | 26 +++++----- aat/engine/dispatch/order_entry.py | 2 +- aat/engine/dispatch/periodic.py | 3 -- aat/engine/dispatch/utils.py | 50 +++++++----------- aat/engine/engine.py | 59 +++++++++++++--------- aat/strategy/utils.py | 34 ++++++------- aat/tests/exchange/test_ib_race.py | 1 - 8 files changed, 86 insertions(+), 91 deletions(-) diff --git a/aat/core/data/error.py b/aat/core/data/error.py index be9a89c9..2d8e5c39 100644 --- a/aat/core/data/error.py +++ b/aat/core/data/error.py @@ -37,7 +37,7 @@ def timestamp(self) -> int: return self.__timestamp @timestamp.setter - def id(self, timestamp: datetime) -> None: + def timestamp(self, timestamp: datetime) -> None: assert isinstance(timestamp, datetime) self.__timestamp = timestamp diff --git a/aat/engine/dispatch/execution/execution.py b/aat/engine/dispatch/execution/execution.py index d32f5311..c85e0d2d 100644 --- a/aat/engine/dispatch/execution/execution.py +++ b/aat/engine/dispatch/execution/execution.py @@ -18,11 +18,11 @@ def __init__(self) -> None: self._exchanges: Dict[ExchangeType, Exchange] = {} # for race conditions where trade/cancel comes before acknowledgement - self._pending_action: List[Order] = [] + self._pending_action: List[Tuple[Order, "Strategy"]] = [] # track which strategies generated which orders self._pending_orders: Dict[ - str, Tuple[Order, Optional["Strategy"]] + str, Tuple[Order, "Strategy"] ] = {} # lookup strategy by order # past orders @@ -45,24 +45,24 @@ async def newOrder(self, strategy: Optional["Strategy"], order: Order) -> bool: raise Exception("Exchange not installed: {}".format(order.exchange)) # stash in case execution occurs before newOrder returns - self._pending_action.append((order, strategy)) + self._pending_action.append((order, cast("Strategy", strategy))) # tell exchange to submit ret = await exchange.newOrder(order) if ret: # if received, store in pending and return to caller - self._pending_orders[order.id] = (order, strategy) + self._pending_orders[order.id] = (order, cast("Strategy", strategy)) if (order, strategy) in self._pending_action: # remove from pre pending - self._pending_action.remove((order, strategy)) + self._pending_action.remove((order, cast("Strategy", strategy))) # tell manager it was received/rejected if ret: - await self._manager._onReceived(strategy, order) + await self._manager._onReceived(cast("Strategy", strategy), order) else: - await self._manager._onRejected(strategy, order) + await self._manager._onRejected(cast("Strategy", strategy), order) # return to caller return ret @@ -73,7 +73,7 @@ async def cancelOrder(self, strategy: Optional["Strategy"], order: Order) -> boo raise AATException("Exchange not installed: {}".format(order.exchange)) # stash in case cancel occurs before cancelOrder returns - self._pending_action.append((order, strategy)) + self._pending_action.append((order, cast("Strategy", strategy))) # tell exchange to cancel ret = await exchange.cancelOrder(order) @@ -84,13 +84,13 @@ async def cancelOrder(self, strategy: Optional["Strategy"], order: Order) -> boo if (order, strategy) in self._pending_action: # remove from pre pending - self._pending_action.remove((order, strategy)) + self._pending_action.remove((order, cast("Strategy", strategy))) # tell manager it was canceled/rejected - await self._manager._onCanceled(strategy, order) + await self._manager._onCanceled(cast("Strategy", strategy), order) else: - await self._manager._onRejected(strategy, order) + await self._manager._onRejected(cast("Strategy", strategy), order) # return to caller return ret @@ -103,7 +103,7 @@ async def onTrade(self, event: Event) -> None: action: bool = False ooo: bool = False # execution is out of order with receipt strat: Optional[EventHandler] = None - trade: Trade = event.target + trade: Trade = cast(Trade, event.target) # if it comes with the order, use that if trade.my_order: @@ -177,7 +177,7 @@ async def onTrade(self, event: Event) -> None: self._past_orders.append(order) async def onCancel(self, event: Event) -> None: - canceled_order: Order = event.target + canceled_order: Order = cast(Order, event.target) if canceled_order.id in self._pending_orders: order, strat = self._pending_orders[canceled_order.id] diff --git a/aat/engine/dispatch/order_entry.py b/aat/engine/dispatch/order_entry.py index 5f740c05..f34b51fc 100644 --- a/aat/engine/dispatch/order_entry.py +++ b/aat/engine/dispatch/order_entry.py @@ -67,7 +67,7 @@ async def _onCanceled(self, strategy: "Strategy", order: Order) -> None: self._alerted_events[ev] = (strategy, order) await self._engine.pushTargetedEvent(strategy, ev) - async def _onRejected(self, strategy: "Strategy", order: Order) -> None: + async def _onRejected(self, strategy: "Strategy", order: Order) -> bool: # push event to loop ev = Event(type=Event.Types.REJECTED, target=order) self._alerted_events[ev] = (strategy, order) diff --git a/aat/engine/dispatch/periodic.py b/aat/engine/dispatch/periodic.py index 79c7aa6a..c4f450f1 100644 --- a/aat/engine/dispatch/periodic.py +++ b/aat/engine/dispatch/periodic.py @@ -19,9 +19,6 @@ def __init__( ) -> None: self._loop = loop self._function: Callable[..., Awaitable[None]] = function - assert ( - second != "*" and minute != "*" and hour != "*" - ), "Please use None instead of '*'" self.__second = second self.__minute = minute self.__hour = hour diff --git a/aat/engine/dispatch/utils.py b/aat/engine/dispatch/utils.py index 412fd6d5..ca55758c 100644 --- a/aat/engine/dispatch/utils.py +++ b/aat/engine/dispatch/utils.py @@ -117,9 +117,9 @@ def periodic( self, function: Callable, seconds: int = 0, - minutes: int = 0, - hours: int = 0, - ): + minutes: int = None, + hours: int = None, + ) -> Periodic: """run a function periodically: if the amount of time between previous call and current call is more than `seconds` seconds + `minutes` minutes + `hours` hours, @@ -130,10 +130,10 @@ def periodic( def at( self, function: Callable, - second: Union[int, str] = 0, - minute: Union[int, str] = "*", - hour: Union[int, str] = "*", - ): + second: int = 0, + minute: int = None, + hour: int = None, + ) -> Periodic: """run a function at a certain point in time: e.g. run it on every `second` second every minute of every hour So 2, "*", "*", would run 1:00:02, 1:01:02, etc @@ -143,9 +143,9 @@ def at( def _periodic( self, function: Callable, - second: Union[int, str] = 0, - minute: Union[int, str] = "*", - hour: Union[int, str] = "*", + second: int = 0, + minute: int = None, + hour: int = None, interval: bool = False, ) -> Periodic: """periodically run a given async function. NOTE: precise timing @@ -157,34 +157,22 @@ def _periodic( if not asyncio.iscoroutinefunction(function): function = self._make_async(function) - if not isinstance(second, (int, str)): - raise Exception("`second` arg must be int or str") + if second is not None and not isinstance(second, int): + raise Exception("`second` arg must be int") - if not isinstance(minute, (int, str)): - raise Exception("`minute` arg must be int or str") + if minute is not None and not isinstance(minute, int): + raise Exception("`minute` arg must be int") - if not isinstance(hour, (int, str)): - raise Exception("`hour` arg must be int or str") + if hour is not None and not isinstance(hour, int): + raise Exception("`hour` arg must be int") - if isinstance(second, str) and second != "*": - raise Exception('Only "*" or int allowed for argument `second`') - elif isinstance(second, str): - second = None # type: ignore - elif second < 0 or second > 60: + if second is not None and (second < 0 or second > 60): raise Exception("`second` must be between 0 and 60") - if isinstance(minute, str) and minute != "*": - raise Exception('Only "*" or int allowed for argument `minute`') - elif isinstance(minute, str): - minute = None # type: ignore - elif minute < 0 or minute > 60: + if minute is not None and (minute < 0 or minute > 60): raise Exception("`minute` must be between 0 and 60") - if isinstance(hour, str) and hour != "*": - raise Exception('Only "*" or int allowed for argument `hour`') - elif isinstance(hour, str): - hour = None # type: ignore - elif hour < 0 or hour > 24: + if hour is not None and (hour < 0 or hour > 24): raise Exception("`hour` must be between 0 and 24") # End Validation diff --git a/aat/engine/engine.py b/aat/engine/engine.py index df84b66c..3aa6a80a 100644 --- a/aat/engine/engine.py +++ b/aat/engine/engine.py @@ -4,6 +4,7 @@ import os.path import pytz +from asyncio import Future, Queue from aiostream.stream import merge # type: ignore from collections import deque from concurrent.futures import ThreadPoolExecutor @@ -161,7 +162,7 @@ def __init__(self, **config: dict) -> None: self.event_loop = asyncio.get_event_loop() # setup subscriptions - self._handler_subscriptions: Dict[EventType, List] = { + self._handler_subscriptions: Dict[EventType, ListType] = { m: [] for m in EventType.__members__.values() } @@ -342,7 +343,7 @@ async def pushTargetedEvent(self, strategy: Strategy, event: Event) -> None: """push non-exchange event targeted to a specific strat into the queue""" await self._queued_targeted_events.put((event, strategy)) - async def _wraptick(self, ticker) -> Event: + async def _wraptick(self, ticker) -> Event: # type: ignore async for ev in ticker: yield ev yield Event(type=EventType.EXIT, target=None) @@ -350,9 +351,9 @@ async def _wraptick(self, ticker) -> Event: async def run(self) -> None: """run the engine""" # setup future queue - self._queued_events: Deque[Event] = asyncio.Queue() - self._queued_targeted_events: Deque[Tuple[Strategy, Event]] = asyncio.Queue() - self._futures: Deque[asyncio.Future] = deque() + self._queued_events: Queue[Event] = Queue() + self._queued_targeted_events: Queue[Tuple[Event, Strategy]] = Queue() + self._futures: Deque[Future] = deque() # await all connections await asyncio.gather( @@ -459,11 +460,15 @@ async def run(self) -> None: self._latest = datetime.now(tz=self.tz) # process any periodics - self._futures.extend([asyncio.create_task(p.execute(self._latest)) - for p in self.manager.periodics() - if p.expires(self._latest)]) + self._futures.extend( + [ + asyncio.create_task(p.execute(self._latest)) + for p in self.manager.periodics() + if p.expires(self._latest) + ] + ) - remaining_futures = deque() + remaining_futures: Deque[Future] = deque() for fut in self._futures: if fut.done(): # trigger exception if necessary @@ -475,21 +480,25 @@ async def run(self) -> None: # Before engine shutdown, send an exit event await self.processEvent(Event(type=EventType.EXIT, target=None)) - async def _tick_queued_events(self): + async def _tick_queued_events(self) -> AsyncGenerator[Event, None]: while True: yield await self._queued_events.get() - async def _tick_queued_targeted_events(self): + async def _tick_queued_targeted_events( + self, + ) -> AsyncGenerator[Tuple[Event, Strategy], None]: while True: yield await self._queued_targeted_events.get() - async def processEvent(self, event: Event, strategy: Strategy = None) -> None: + async def processEvent( + self, event: Event, strategy: Strategy = None + ) -> ListType[Future]: """send an event to all registered event handlers Arguments: event (Event): event to send """ - ret = [] + ret: ListType[Future] = [] if event.type == EventType.HEARTBEAT: # ignore heartbeat return ret @@ -524,19 +533,21 @@ async def processEvent(self, event: Event, strategy: Strategy = None) -> None: # don't infinite error raise await asyncio.sleep(1) - return [asyncio.ensure_future( - self.processEvent( - Event( - type=EventType.ERROR, - target=Error( - target=event, - handler=handler, - callback=callback, - exception=e, - ), + return [ + asyncio.ensure_future( + self.processEvent( + Event( + type=EventType.ERROR, + target=Error( + target=event, + handler=handler, + callback=callback, + exception=e, + ), + ) ) ) - )] + ] return ret async def tick(self) -> AsyncGenerator: diff --git a/aat/strategy/utils.py b/aat/strategy/utils.py index d7a42ac8..e1849d98 100644 --- a/aat/strategy/utils.py +++ b/aat/strategy/utils.py @@ -1,7 +1,7 @@ import asyncio from datetime import datetime -from typing import Union, Callable, Optional, List, TYPE_CHECKING +from typing import Callable, Optional, List, TYPE_CHECKING from ..config import Side, TradingType, ExitRoutine, InstrumentType from ..core import Trade, Instrument, ExchangeType, Order, OrderBook from ..exchange import Exchange @@ -121,44 +121,44 @@ async def book(self, instrument: Instrument) -> Optional[OrderBook]: def periodic( self, function: Callable, - seconds: Union[int, str] = 0, - minutes: Union[int, str] = "*", - hours: Union[int, str] = "*", + seconds: int = 0, + minutes: int = None, + hours: int = None, ) -> Periodic: """periodically run a given async function. NOTE: precise timing is NOT guaranteed due to event loop scheduling. Args: function (callable); function to call periodically - second (Union[int, str]); second to align periodic to, or '*' for every second - minute (Union[int, str]); minute to align periodic to, or '*' for every minute - hour (Union[int, str]); hour to align periodic to, or '*' for every hour + second (str); second to align periodic to + minute (str); minute to align periodic to + hour (str); hour to align periodic to """ return self._manager.periodic(function, seconds, minutes, hours) def at( self, function: Callable, - second: Union[int, str] = 0, - minute: Union[int, str] = "*", - hour: Union[int, str] = "*", + second: int = 0, + minute: int = None, + hour: int = None, ) -> Periodic: """periodically run a given async function. NOTE: precise timing is NOT guaranteed due to event loop scheduling. Args: function (callable); function to call periodically - second (Union[int, str]); second to align periodic to, or '*' for every second - minute (Union[int, str]); minute to align periodic to, or '*' for every minute - hour (Union[int, str]); hour to align periodic to, or '*' for every hour + second (str); second to align periodic to + minute (str); minute to align periodic to + hour (str); hour to align periodic to NOTE: this is a rudimentary scheme but should be sufficient. For more complicated scheduling, just install multiple instances of the same periodic e.g. for running on :00, :15, :30, and :45 install - periodic(0, 0, '*') - periodic(0, 15, '*') - periodic(0, 30, '*') - periodic(0, 45, '*') + periodic(0, 0) + periodic(0, 15) + periodic(0, 30) + periodic(0, 45) """ return self._manager.at(function, second, minute, hour) diff --git a/aat/tests/exchange/test_ib_race.py b/aat/tests/exchange/test_ib_race.py index a9875a7f..aa033dd3 100644 --- a/aat/tests/exchange/test_ib_race.py +++ b/aat/tests/exchange/test_ib_race.py @@ -1,5 +1,4 @@ import asyncio -from datetime import datetime, timedelta from typing import List, Any, AsyncGenerator, Optional from aat import Strategy from aat.config import EventType, InstrumentType, Side, TradingType From f38cfa0177ff52c0bcc382024511de94a57b8969 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Tue, 2 Mar 2021 03:04:41 +0000 Subject: [PATCH 04/14] tweak execution to ignore past orders --- aat/engine/dispatch/execution/execution.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/aat/engine/dispatch/execution/execution.py b/aat/engine/dispatch/execution/execution.py index c85e0d2d..61674a43 100644 --- a/aat/engine/dispatch/execution/execution.py +++ b/aat/engine/dispatch/execution/execution.py @@ -26,7 +26,9 @@ def __init__(self) -> None: ] = {} # lookup strategy by order # past orders - self._past_orders: List[Order] = [] + self._past_orders: Dict[ + str, Tuple[Order, "Strategy"] + ] = {} # lookup strategy by order def addExchange(self, exchange: Exchange) -> None: """add an exchange""" @@ -102,7 +104,7 @@ async def onTrade(self, event: Event) -> None: """Match trade with order""" action: bool = False ooo: bool = False # execution is out of order with receipt - strat: Optional[EventHandler] = None + strat: Optional["Strategy"] = None trade: Trade = cast(Trade, event.target) # if it comes with the order, use that @@ -113,6 +115,9 @@ async def onTrade(self, event: Event) -> None: if trade.my_order.id in self._pending_orders: # grab order and strategy from pending orders order, strat = self._pending_orders[trade.my_order.id] + elif trade.my_order.id in self._past_orders: + # duplicate message, ignore + return else: # execution was out of order, look which orders are pending actions lookup = [x for x in self._pending_action if x[0] == trade.my_order] @@ -174,7 +179,7 @@ async def onTrade(self, event: Event) -> None: if order.finished(): self._pending_orders.pop(order.id, None) - self._past_orders.append(order) + self._past_orders[order.id] = (order, cast("Strategy", strat)) async def onCancel(self, event: Event) -> None: canceled_order: Order = cast(Order, event.target) From 30bec9d415122500334694684c994aca1265a68e Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Sat, 6 Mar 2021 21:56:48 -0500 Subject: [PATCH 05/14] add spread reconstitute --- aat/exchange/public/ib/spread.py | 45 +++++++++++++++ .../public/test_ib_spread_reconstitute.py | 57 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 aat/exchange/public/ib/spread.py create mode 100644 aat/tests/exchange/public/test_ib_spread_reconstitute.py diff --git a/aat/exchange/public/ib/spread.py b/aat/exchange/public/ib/spread.py new file mode 100644 index 00000000..6f4cfbc5 --- /dev/null +++ b/aat/exchange/public/ib/spread.py @@ -0,0 +1,45 @@ +from aat.config import Side +from aat.core import Order +from collections import deque +from typing import Dict, Deque, Optional + + +class SpreadReconstitute(object): + def __init__(self) -> None: + self._orders: Dict[str, Dict[Side, Deque[Order]]] = {} + + def push(self, order: Order) -> None: + if order.id not in self._orders: + self._orders[order.id] = {Side.BUY: deque(), Side.SELL: deque()} + + self._orders[order.id][order.side].append(order) + + def get(self, originalOrder: Order) -> Optional[Order]: + if originalOrder.id not in self._orders: + return None + + if ( + self._orders[originalOrder.id][Side.BUY] + and self._orders[originalOrder.id][Side.SELL] + ): + # if orders on both sides, pop out and assemble unified order + buy = self._orders[originalOrder.id][Side.BUY].popleft() + sell = self._orders[originalOrder.id][Side.SELL].popleft() + + # need to move in lock step for now + assert buy.volume == sell.volume + + order = Order( + volume=buy.volume, + price=buy.price - sell.price, + side=originalOrder.side, + instrument=originalOrder.instrument, + exchange=originalOrder.exchange, + order_type=originalOrder.order_type, + id=originalOrder.id, + timestamp=originalOrder.timestamp, + ) + + return order + + return None diff --git a/aat/tests/exchange/public/test_ib_spread_reconstitute.py b/aat/tests/exchange/public/test_ib_spread_reconstitute.py new file mode 100644 index 00000000..ea8137b6 --- /dev/null +++ b/aat/tests/exchange/public/test_ib_spread_reconstitute.py @@ -0,0 +1,57 @@ +import sys +from mock import MagicMock +from aat.core import Order, Instrument +from aat.config import OrderType, InstrumentType, Side + +leg1 = Instrument(name="LEG1", type=InstrumentType.FUTURE) +leg2 = Instrument(name="LEG2", type=InstrumentType.FUTURE) +spread = Instrument(name="LEG1_LEG2", type=InstrumentType.SPREAD, leg1=leg1, leg2=leg2) + + +class Wrapper: + ... + + +sys.modules["ibapi"] = MagicMock() +sys.modules["ibapi.client"] = MagicMock() +sys.modules["ibapi.client"].EClient = object +sys.modules["ibapi.commission_report"] = MagicMock() +sys.modules["ibapi.contract"] = MagicMock() +sys.modules["ibapi.execution"] = MagicMock() +sys.modules["ibapi.order"] = MagicMock() +sys.modules["ibapi.wrapper"] = MagicMock() +sys.modules["ibapi.wrapper"].EWrapper = Wrapper + +from aat.exchange.public.ib.spread import SpreadReconstitute + + +class TestIBReconstituteSpread: + def test_spread(self): + + sr = SpreadReconstitute() + + original_order = Order(10, 7, Side.BUY, spread) + + order1_l1 = Order(3, 10, Side.BUY, instrument=leg1) + order1_l2 = Order(3, 2, Side.SELL, instrument=leg1) + order1 = Order(3, 8, Side.BUY, instrument=spread) + + sr.push(order1_l1) + + assert sr.get(original_order) == None + + sr.push(order1_l2) + + assert sr.get(original_order) == order1 + + order2_l1 = Order(7, 11, Side.BUY, instrument=leg1) + order2_l2 = Order(7, 3, Side.SELL, instrument=leg1) + order2 = Order(7, 8, Side.SELL, instrument=spread) + + sr.push(order2_l1) + + assert sr.get(original_order) == None + + sr.push(order2_l2) + + assert sr.get(original_order) == order2 From 694c437c16efb2f8b4a39364c3cfb61d304b9afe Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Sat, 27 Mar 2021 23:37:20 -0400 Subject: [PATCH 06/14] fix lint --- aat/tests/exchange/public/test_ib_spread_reconstitute.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aat/tests/exchange/public/test_ib_spread_reconstitute.py b/aat/tests/exchange/public/test_ib_spread_reconstitute.py index ea8137b6..2e9f3eec 100644 --- a/aat/tests/exchange/public/test_ib_spread_reconstitute.py +++ b/aat/tests/exchange/public/test_ib_spread_reconstitute.py @@ -1,7 +1,7 @@ import sys from mock import MagicMock from aat.core import Order, Instrument -from aat.config import OrderType, InstrumentType, Side +from aat.config import InstrumentType, Side leg1 = Instrument(name="LEG1", type=InstrumentType.FUTURE) leg2 = Instrument(name="LEG2", type=InstrumentType.FUTURE) @@ -22,7 +22,7 @@ class Wrapper: sys.modules["ibapi.wrapper"] = MagicMock() sys.modules["ibapi.wrapper"].EWrapper = Wrapper -from aat.exchange.public.ib.spread import SpreadReconstitute +from aat.exchange.public.ib.spread import SpreadReconstitute # noqa: E402 class TestIBReconstituteSpread: @@ -38,7 +38,7 @@ def test_spread(self): sr.push(order1_l1) - assert sr.get(original_order) == None + assert sr.get(original_order) == None # noqa: E711 sr.push(order1_l2) @@ -50,7 +50,7 @@ def test_spread(self): sr.push(order2_l1) - assert sr.get(original_order) == None + assert sr.get(original_order) == None # noqa: E711 sr.push(order2_l2) From 9f70a7d102e3a68a6f1e19fa0560906f1a12c4d8 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Sun, 28 Mar 2021 10:57:58 -0400 Subject: [PATCH 07/14] fix annotation --- aat/tests/exchange/public/test_ib_spread_reconstitute.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aat/tests/exchange/public/test_ib_spread_reconstitute.py b/aat/tests/exchange/public/test_ib_spread_reconstitute.py index 2e9f3eec..ffa22b35 100644 --- a/aat/tests/exchange/public/test_ib_spread_reconstitute.py +++ b/aat/tests/exchange/public/test_ib_spread_reconstitute.py @@ -14,19 +14,19 @@ class Wrapper: sys.modules["ibapi"] = MagicMock() sys.modules["ibapi.client"] = MagicMock() -sys.modules["ibapi.client"].EClient = object +sys.modules["ibapi.client"].EClient = object # type: ignore sys.modules["ibapi.commission_report"] = MagicMock() sys.modules["ibapi.contract"] = MagicMock() sys.modules["ibapi.execution"] = MagicMock() sys.modules["ibapi.order"] = MagicMock() sys.modules["ibapi.wrapper"] = MagicMock() -sys.modules["ibapi.wrapper"].EWrapper = Wrapper +sys.modules["ibapi.wrapper"].EWrapper = Wrapper # type: ignore from aat.exchange.public.ib.spread import SpreadReconstitute # noqa: E402 class TestIBReconstituteSpread: - def test_spread(self): + def test_spread(self) -> None: sr = SpreadReconstitute() From f51c0201de0b566ceba2688a9365dbccda4b8e62 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Sun, 15 Aug 2021 22:41:34 -0400 Subject: [PATCH 08/14] bump From 682f9862e4c339664197565dc7acb5cdaadcb96f Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Mon, 22 Nov 2021 15:39:36 -0500 Subject: [PATCH 09/14] fix lint --- .../include/aat/core/order_book/order_book.hpp | 15 ++++++++++++--- aat/cpp/include/aat/python/binding.hpp | 6 ++---- aat/cpp/src/core/order_book/order_book.cpp | 6 +++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/aat/cpp/include/aat/core/order_book/order_book.hpp b/aat/cpp/include/aat/core/order_book/order_book.hpp index 020e8907..77c44efa 100644 --- a/aat/cpp/include/aat/core/order_book/order_book.hpp +++ b/aat/cpp/include/aat/core/order_book/order_book.hpp @@ -48,9 +48,18 @@ namespace core { void setCallback(std::function)> callback); - Instrument getInstrument() const { return instrument; } - ExchangeType getExchange() const { return exchange; } - std::function)> getCallback() const { return callback; } + Instrument + getInstrument() const { + return instrument; + } + ExchangeType + getExchange() const { + return exchange; + } + std::function)> + getCallback() const { + return callback; + } void reset(); diff --git a/aat/cpp/include/aat/python/binding.hpp b/aat/cpp/include/aat/python/binding.hpp index 89ee4423..2cf87c20 100644 --- a/aat/cpp/include/aat/python/binding.hpp +++ b/aat/cpp/include/aat/python/binding.hpp @@ -101,8 +101,7 @@ PYBIND11_MODULE(binding, m) { .def(py::init()) .def(py::init)>>()) .def("__repr__", &OrderBook::toString) - .def( - "__iter__", [](const OrderBook& o) { return py::make_iterator(o.begin(), o.end()); }, + .def("__iter__", [](const OrderBook& o) { return py::make_iterator(o.begin(), o.end()); }, py::keep_alive<0, 1>()) /* Essential: keep object alive while iterator exists */ .def("setCallback", &OrderBook::setCallback) .def_property("instrument", &OrderBook::getInstrument, nullptr) @@ -122,8 +121,7 @@ PYBIND11_MODULE(binding, m) { ******************************/ py::class_(m, "_PriceLevelCpp") .def(py::init()) - .def( - "__iter__", [](const PriceLevel& pl) { return py::make_iterator(pl.cbegin(), pl.cend()); }, + .def("__iter__", [](const PriceLevel& pl) { return py::make_iterator(pl.cbegin(), pl.cend()); }, py::keep_alive<0, 1>()) /* Essential: keep object alive while iterator exists */ .def("__getitem__", &PriceLevel::operator[]) .def("__bool__", &PriceLevel::operator bool) diff --git a/aat/cpp/src/core/order_book/order_book.cpp b/aat/cpp/src/core/order_book/order_book.cpp index d3e3f88d..0bc675a1 100644 --- a/aat/cpp/src/core/order_book/order_book.cpp +++ b/aat/cpp/src/core/order_book/order_book.cpp @@ -3,8 +3,7 @@ namespace aat { namespace core { - OrderBookIterator& - OrderBookIterator::operator++() { + OrderBookIterator& OrderBookIterator::operator++() { // TODO return *this; @@ -45,7 +44,8 @@ namespace core { collector.setCallback(callback); } - void OrderBook::reset() { + void + OrderBook::reset() { buy_levels = std::vector(); sell_levels = std::vector(); buys = std::unordered_map>(); From dce7cba5302350bacc0ff8fa84c4fda644ddaee9 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Tue, 14 Mar 2023 13:40:03 -0400 Subject: [PATCH 10/14] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5a6213c6..6428c1b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,3 @@ [build-system] # Minimum requirements for the build system to execute. -requires = ["setuptools", "wheel", "numpy"] +requires = ["setuptools<=60", "wheel", "numpy"] From 9364442fb7fb37c47f1be1cc7835df4537404c3c Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Tue, 14 Mar 2023 13:42:50 -0400 Subject: [PATCH 11/14] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 363214ed..e59ab729 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ ] requires_dev = [ - "black>=20.", + "black>=20", "flake8>=3.7.9", "flake8-black>=0.2.1", "mock>=3.0.5", From 1a08498fbaa62519e1c55b94c817b9b1c0c301e6 Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Tue, 14 Mar 2023 13:52:46 -0400 Subject: [PATCH 12/14] fix lint, drop windows --- .github/workflows/build.yml | 2 +- aat/core/order_book/order_book/order_book.py | 1 - aat/cpp/include/aat/python/binding.hpp | 6 ++++-- aat/cpp/src/core/order_book/order_book.cpp | 3 ++- aat/engine/dispatch/portfolio/portfolio.py | 2 -- aat/exchange/crypto/coinbase/client.py | 7 ++----- aat/exchange/synthetic/__init__.py | 1 - aat/tests/exchange/public/test_ib_spread_reconstitute.py | 1 - aat/ui/application.py | 1 - setup.py | 2 +- 10 files changed, 10 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9a3cfb57..52025c1b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [ubuntu-latest, macos-latest] python-version: [3.9] node-version: [14.x] event-name: [push] diff --git a/aat/core/order_book/order_book/order_book.py b/aat/core/order_book/order_book/order_book.py index 140093c1..e943febc 100644 --- a/aat/core/order_book/order_book/order_book.py +++ b/aat/core/order_book/order_book/order_book.py @@ -73,7 +73,6 @@ def __init__( exchange_name: Union[ExchangeType, str] = "", callback: Optional[Callable] = None, ) -> None: - self._instrument = instrument self._exchange_name: ExchangeType = ( exchange_name diff --git a/aat/cpp/include/aat/python/binding.hpp b/aat/cpp/include/aat/python/binding.hpp index 2cf87c20..89ee4423 100644 --- a/aat/cpp/include/aat/python/binding.hpp +++ b/aat/cpp/include/aat/python/binding.hpp @@ -101,7 +101,8 @@ PYBIND11_MODULE(binding, m) { .def(py::init()) .def(py::init)>>()) .def("__repr__", &OrderBook::toString) - .def("__iter__", [](const OrderBook& o) { return py::make_iterator(o.begin(), o.end()); }, + .def( + "__iter__", [](const OrderBook& o) { return py::make_iterator(o.begin(), o.end()); }, py::keep_alive<0, 1>()) /* Essential: keep object alive while iterator exists */ .def("setCallback", &OrderBook::setCallback) .def_property("instrument", &OrderBook::getInstrument, nullptr) @@ -121,7 +122,8 @@ PYBIND11_MODULE(binding, m) { ******************************/ py::class_(m, "_PriceLevelCpp") .def(py::init()) - .def("__iter__", [](const PriceLevel& pl) { return py::make_iterator(pl.cbegin(), pl.cend()); }, + .def( + "__iter__", [](const PriceLevel& pl) { return py::make_iterator(pl.cbegin(), pl.cend()); }, py::keep_alive<0, 1>()) /* Essential: keep object alive while iterator exists */ .def("__getitem__", &PriceLevel::operator[]) .def("__bool__", &PriceLevel::operator bool) diff --git a/aat/cpp/src/core/order_book/order_book.cpp b/aat/cpp/src/core/order_book/order_book.cpp index d974f97c..3d7b3722 100644 --- a/aat/cpp/src/core/order_book/order_book.cpp +++ b/aat/cpp/src/core/order_book/order_book.cpp @@ -3,7 +3,8 @@ namespace aat { namespace core { - OrderBookIterator& OrderBookIterator::operator++() { + OrderBookIterator& + OrderBookIterator::operator++() { // TODO return *this; diff --git a/aat/engine/dispatch/portfolio/portfolio.py b/aat/engine/dispatch/portfolio/portfolio.py index 24125d4a..f3d201a5 100644 --- a/aat/engine/dispatch/portfolio/portfolio.py +++ b/aat/engine/dispatch/portfolio/portfolio.py @@ -109,7 +109,6 @@ def newPosition(self, trade: Trade, strategy: "Strategy") -> None: and strategy.name() in self._active_positions_by_strategy and trade.instrument in self._active_positions_by_strategy[strategy.name()] ): - # update position cur_pos = self._active_positions_by_strategy[strategy.name()][ trade.instrument @@ -250,7 +249,6 @@ def allPositions( for position_list in self._active_positions_by_instrument.values(): for position in position_list: - if instrument and position.instrument != instrument: # Skip if not asking for this instrument continue diff --git a/aat/exchange/crypto/coinbase/client.py b/aat/exchange/crypto/coinbase/client.py index 44c9c3fc..ceccac7b 100644 --- a/aat/exchange/crypto/coinbase/client.py +++ b/aat/exchange/crypto/coinbase/client.py @@ -49,7 +49,6 @@ def __init__( passphrase: str, satoshis: bool = False, ) -> None: - self.trading_type = trading_type # if running in sandbox mode, use sandbox urls @@ -321,7 +320,7 @@ async def orderBook( self.seqnum[sub] = ob["sequence"] # type: ignore # generate an open limit order for each bid - for (bid, qty, id) in ob["bids"]: + for bid, qty, id in ob["bids"]: o = Order( float(qty) * self._multiple, float(bid), @@ -333,7 +332,7 @@ async def orderBook( yield Event(type=EventType.OPEN, target=o) # generate an open limit order for each ask - for (bid, qty, id) in ob["asks"]: + for bid, qty, id in ob["asks"]: o = Order( float(qty) * self._multiple, float(bid), @@ -484,7 +483,6 @@ async def websocket_l2(self, subscriptions: List[Instrument]): # type: ignore async for msg in ws: # only handle text messages if msg.type == aiohttp.WSMsgType.TEXT: - # load the data as json x = json.loads(msg.data) @@ -551,7 +549,6 @@ async def websocket_trades(self, subscriptions: List[Instrument]): # type: igno async for msg in ws: # only handle text messages if msg.type == aiohttp.WSMsgType.TEXT: - # load the data as json x = json.loads(msg.data) diff --git a/aat/exchange/synthetic/__init__.py b/aat/exchange/synthetic/__init__.py index 744e33df..65056409 100644 --- a/aat/exchange/synthetic/__init__.py +++ b/aat/exchange/synthetic/__init__.py @@ -88,7 +88,6 @@ def _seed(self, symbols: List[str] = None) -> None: def _seedOrders(self) -> None: # seed all orderbooks for instrument, orderbook in self._orderbooks.items(): - # pick a random startpoint, endpoint, and midpoint offset = 50 start = round(random() * offset, 2) + offset diff --git a/aat/tests/exchange/public/test_ib_spread_reconstitute.py b/aat/tests/exchange/public/test_ib_spread_reconstitute.py index ffa22b35..58659300 100644 --- a/aat/tests/exchange/public/test_ib_spread_reconstitute.py +++ b/aat/tests/exchange/public/test_ib_spread_reconstitute.py @@ -27,7 +27,6 @@ class Wrapper: class TestIBReconstituteSpread: def test_spread(self) -> None: - sr = SpreadReconstitute() original_order = Order(10, 7, Side.BUY, spread) diff --git a/aat/ui/application.py b/aat/ui/application.py index 40073c49..933faf95 100644 --- a/aat/ui/application.py +++ b/aat/ui/application.py @@ -17,7 +17,6 @@ def __init__( *args: Any, **kwargs: Any, ) -> None: - handlers = handlers or [] logging.getLogger("tornado.access").disabled = False diff --git a/setup.py b/setup.py index e59ab729..f3320dc6 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ ] requires_dev = [ - "black>=20", + "black>=23", "flake8>=3.7.9", "flake8-black>=0.2.1", "mock>=3.0.5", From dc1766bfcbd75f3d7b59165130de85dac7e0d4cd Mon Sep 17 00:00:00 2001 From: Tim Paine Date: Tue, 14 Mar 2023 14:23:00 -0400 Subject: [PATCH 13/14] bump deps, bump lint, fix mypy issues --- aat/config/parser.py | 4 +- aat/core/data/cpp.py | 6 +- aat/core/exchange/db.py | 6 +- aat/core/instrument/calendar.py | 4 +- aat/core/instrument/instrument.py | 8 +- aat/core/order_book/base.py | 2 +- aat/core/order_book/order_book/order_book.py | 2 +- aat/core/order_book/price_level/ro.py | 4 +- aat/cpp/include/aat/core/data/data.hpp | 2 +- aat/cpp/include/aat/core/data/order.hpp | 1 + aat/cpp/include/aat/core/data/trade.hpp | 1 + .../include/aat/core/exchange/exchange.hpp | 2 + .../nlohmann_json/nlohmann/adl_serializer.hpp | 62 +- .../nlohmann/byte_container_with_subtype.hpp | 103 + .../nlohmann/detail/abi_macros.hpp | 100 + .../nlohmann/detail/conversions/from_json.hpp | 312 +- .../nlohmann/detail/conversions/to_chars.hpp | 174 +- .../nlohmann/detail/conversions/to_json.hpp | 195 +- .../nlohmann/detail/exceptions.hpp | 369 +- .../nlohmann_json/nlohmann/detail/hash.hpp | 129 + .../nlohmann/detail/input/binary_reader.hpp | 1667 +++- .../nlohmann/detail/input/input_adapters.hpp | 462 +- .../nlohmann/detail/input/json_sax.hpp | 211 +- .../nlohmann/detail/input/lexer.hpp | 310 +- .../nlohmann/detail/input/parser.hpp | 253 +- .../nlohmann/detail/input/position_t.hpp | 18 +- .../detail/iterators/internal_iterator.hpp | 16 +- .../nlohmann/detail/iterators/iter_impl.hpp | 249 +- .../detail/iterators/iteration_proxy.hpp | 112 +- .../detail/iterators/iterator_traits.hpp | 28 +- .../iterators/json_reverse_iterator.hpp | 21 +- .../detail/iterators/primitive_iterator.hpp | 22 +- .../nlohmann/detail/json_pointer.hpp | 821 +- .../nlohmann/detail/json_ref.hpp | 39 +- .../nlohmann/detail/macro_scope.hpp | 379 +- .../nlohmann/detail/macro_unscope.hpp | 41 +- .../nlohmann/detail/meta/call_std/begin.hpp | 17 + .../nlohmann/detail/meta/call_std/end.hpp | 17 + .../nlohmann/detail/meta/cpp_future.hpp | 162 +- .../nlohmann/detail/meta/detected.hpp | 42 +- .../nlohmann/detail/meta/identity_tag.hpp | 21 + .../nlohmann/detail/meta/is_sax.hpp | 63 +- .../nlohmann/detail/meta/std_fs.hpp | 29 + .../nlohmann/detail/meta/type_traits.hpp | 648 +- .../nlohmann/detail/meta/void_t.hpp | 23 +- .../nlohmann/detail/output/binary_writer.hpp | 741 +- .../detail/output/output_adapters.hpp | 46 +- .../nlohmann/detail/output/serializer.hpp | 254 +- .../nlohmann/detail/string_concat.hpp | 146 + .../nlohmann/detail/string_escape.hpp | 72 + .../nlohmann_json/nlohmann/detail/value_t.hpp | 63 +- aat/cpp/third/nlohmann_json/nlohmann/json.hpp | 7391 +++++------------ .../third/nlohmann_json/nlohmann/json_fwd.hpp | 54 +- .../nlohmann_json/nlohmann/ordered_map.hpp | 359 + .../nlohmann/thirdparty/hedley/hedley.hpp | 936 ++- .../thirdparty/hedley/hedley_undef.hpp | 38 +- aat/cpp/third/pybind11/pybind11/attr.h | 393 +- aat/cpp/third/pybind11/pybind11/buffer_info.h | 173 +- aat/cpp/third/pybind11/pybind11/cast.h | 2230 ++--- aat/cpp/third/pybind11/pybind11/chrono.h | 189 +- aat/cpp/third/pybind11/pybind11/complex.h | 39 +- .../third/pybind11/pybind11/detail/class.h | 432 +- .../third/pybind11/pybind11/detail/common.h | 1136 ++- .../third/pybind11/pybind11/detail/descr.h | 116 +- aat/cpp/third/pybind11/pybind11/detail/init.h | 305 +- .../pybind11/pybind11/detail/internals.h | 510 +- .../pybind11/detail/type_caster_base.h | 1019 +++ .../third/pybind11/pybind11/detail/typeid.h | 36 +- aat/cpp/third/pybind11/pybind11/eigen.h | 597 +- .../third/pybind11/pybind11/eigen/matrix.h | 699 ++ .../third/pybind11/pybind11/eigen/tensor.h | 509 ++ aat/cpp/third/pybind11/pybind11/embed.h | 237 +- aat/cpp/third/pybind11/pybind11/eval.h | 129 +- aat/cpp/third/pybind11/pybind11/functional.h | 92 +- aat/cpp/third/pybind11/pybind11/gil.h | 239 + aat/cpp/third/pybind11/pybind11/iostream.h | 152 +- aat/cpp/third/pybind11/pybind11/numpy.h | 1500 ++-- aat/cpp/third/pybind11/pybind11/operators.h | 260 +- aat/cpp/third/pybind11/pybind11/options.h | 61 +- aat/cpp/third/pybind11/pybind11/pybind11.h | 2637 +++--- aat/cpp/third/pybind11/pybind11/pytypes.h | 1957 +++-- aat/cpp/third/pybind11/pybind11/stl.h | 318 +- .../third/pybind11/pybind11/stl/filesystem.h | 116 + aat/cpp/third/pybind11/pybind11/stl_bind.h | 751 +- .../pybind11_json/pybind11_json.hpp | 70 +- aat/engine/dispatch/order_entry.py | 20 +- aat/engine/dispatch/portfolio/manager.py | 8 +- aat/engine/dispatch/portfolio/mixin.py | 10 +- aat/engine/dispatch/portfolio/portfolio.py | 14 +- aat/engine/dispatch/risk/mixin.py | 3 +- aat/engine/dispatch/risk/risk.py | 2 +- aat/engine/dispatch/utils.py | 20 +- aat/engine/engine.py | 10 +- aat/exchange/base/market_data.py | 2 +- aat/exchange/crypto/coinbase/client.py | 2 +- aat/exchange/synthetic/__init__.py | 6 +- aat/strategy/calculations.py | 26 +- aat/strategy/portfolio.py | 10 +- aat/strategy/risk.py | 4 +- aat/strategy/strategy.py | 6 +- aat/strategy/utils.py | 40 +- aat/tests/engine/test_periodic.py | 11 +- .../public/test_ib_spread_reconstitute.py | 2 +- aat/ui/application.py | 2 +- setup.py | 1 - 105 files changed, 20711 insertions(+), 13647 deletions(-) create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/byte_container_with_subtype.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/abi_macros.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/hash.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/begin.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/end.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/meta/identity_tag.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/meta/std_fs.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/string_concat.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/detail/string_escape.hpp create mode 100644 aat/cpp/third/nlohmann_json/nlohmann/ordered_map.hpp create mode 100644 aat/cpp/third/pybind11/pybind11/detail/type_caster_base.h create mode 100644 aat/cpp/third/pybind11/pybind11/eigen/matrix.h create mode 100644 aat/cpp/third/pybind11/pybind11/eigen/tensor.h create mode 100644 aat/cpp/third/pybind11/pybind11/gil.h create mode 100644 aat/cpp/third/pybind11/pybind11/stl/filesystem.h diff --git a/aat/config/parser.py b/aat/config/parser.py index 26f10d11..26fff53b 100644 --- a/aat/config/parser.py +++ b/aat/config/parser.py @@ -5,7 +5,7 @@ import os.path import pytz from configparser import ConfigParser -from typing import Any, Dict, List, Union, TYPE_CHECKING +from typing import Optional, Any, Dict, List, Union, TYPE_CHECKING if TYPE_CHECKING: from aat.config import TradingType @@ -100,7 +100,7 @@ def getExchanges( return exchange_instances -def parseConfig(argv: list = None) -> dict: +def parseConfig(argv: Optional[list] = None) -> dict: from aat import TradingType parser = argparse.ArgumentParser() diff --git a/aat/core/data/cpp.py b/aat/core/data/cpp.py index 75fec183..e549007b 100644 --- a/aat/core/data/cpp.py +++ b/aat/core/data/cpp.py @@ -50,8 +50,8 @@ def _make_cpp_order( order_type: OrderType = OrderType.MARKET, flag: OrderFlag = OrderFlag.NONE, stop_target: Optional["Order"] = None, - id: str = None, - timestamp: datetime = None, + id: Optional[str] = None, + timestamp: Optional[datetime] = None, ) -> OrderCpp: """helper method to ensure all arguments are setup""" return OrderCpp( @@ -72,7 +72,7 @@ def _make_cpp_order( def _make_cpp_trade( id: str, timestamp: datetime, - maker_orders: List["Order"] = None, + maker_orders: Optional[List["Order"]] = None, taker_order: Optional["Order"] = None, ) -> TradeCpp: """helper method to ensure all arguments are setup""" diff --git a/aat/core/exchange/db.py b/aat/core/exchange/db.py index e2bdc3cd..b6870930 100644 --- a/aat/core/exchange/db.py +++ b/aat/core/exchange/db.py @@ -1,4 +1,4 @@ -from typing import Dict, TYPE_CHECKING +from typing import Optional, Dict, TYPE_CHECKING from ...config import InstrumentType if TYPE_CHECKING: @@ -27,7 +27,9 @@ def instruments( ) -> None: raise NotImplementedError() - def get(self, name: str = "", instrument: "Instrument" = None) -> "ExchangeType": + def get( + self, name: str = "", instrument: Optional["Instrument"] = None + ) -> "ExchangeType": if name: return self._name_map[name] raise NotImplementedError() diff --git a/aat/core/instrument/calendar.py b/aat/core/instrument/calendar.py index 45f7c9e8..654f6a81 100644 --- a/aat/core/instrument/calendar.py +++ b/aat/core/instrument/calendar.py @@ -20,8 +20,8 @@ class TradingDay(object): def __init__( self, - open_times: Union[time, Tuple[time, ...]] = None, - close_times: Union[time, Tuple[time, ...]] = None, + open_times: Optional[Union[time, Tuple[time, ...]]] = None, + close_times: Optional[Union[time, Tuple[time, ...]]] = None, ): if open_times and not isinstance(open_times, (tuple, time)): # raise exception if wrong type diff --git a/aat/core/instrument/instrument.py b/aat/core/instrument/instrument.py index b17b0362..be8cea91 100644 --- a/aat/core/instrument/instrument.py +++ b/aat/core/instrument/instrument.py @@ -303,7 +303,9 @@ def exchanges(self) -> List[ExchangeType]: def exchange(self) -> ExchangeType: return self.__exchange - def tradingLines(self, exchange: ExchangeType = None) -> List["Instrument"]: + def tradingLines( + self, exchange: Optional[ExchangeType] = None + ) -> List["Instrument"]: """Returns other exchanges that the same instrument trades on Returns: @@ -312,7 +314,9 @@ def tradingLines(self, exchange: ExchangeType = None) -> List["Instrument"]: return self._instrumentdb.instruments(self.name, self.type, exchange) def synthetics( - self, type: InstrumentType = None, exchange: ExchangeType = None + self, + type: Optional[InstrumentType] = None, + exchange: Optional[ExchangeType] = None, ) -> List["Instrument"]: """Returns other instruments with the same name diff --git a/aat/core/order_book/base.py b/aat/core/order_book/base.py index c861447d..b14752a2 100644 --- a/aat/core/order_book/base.py +++ b/aat/core/order_book/base.py @@ -36,7 +36,7 @@ def spread(self) -> float: pass @abstractmethod - def level(self, level: int = 0, price: float = None) -> Tuple: + def level(self, level: int = 0, price: Optional[float] = None) -> Tuple: pass @abstractmethod diff --git a/aat/core/order_book/order_book/order_book.py b/aat/core/order_book/order_book/order_book.py index e943febc..b53a96fd 100644 --- a/aat/core/order_book/order_book/order_book.py +++ b/aat/core/order_book/order_book/order_book.py @@ -163,7 +163,7 @@ def spread(self) -> float: tob: Dict[Side, PriceLevelRO] = self.topOfBook() return tob[Side.SELL].price - tob[Side.BUY].price - def level(self, level: int = 0, price: float = None) -> Tuple: + def level(self, level: int = 0, price: Optional[float] = None) -> Tuple: """return book level Args: diff --git a/aat/core/order_book/price_level/ro.py b/aat/core/order_book/price_level/ro.py index d8bd78f9..7ff5beed 100644 --- a/aat/core/order_book/price_level/ro.py +++ b/aat/core/order_book/price_level/ro.py @@ -1,5 +1,5 @@ from collections import deque -from typing import Deque, Dict, List, Union +from typing import Optional, Deque, Dict, List, Union from aat.core import Order @@ -18,7 +18,7 @@ def __init__( price: float, volume: float, number_of_orders: int = 0, - _orders: Deque[Order] = None, + _orders: Optional[Deque[Order]] = None, ): self._price = price self._volume = volume diff --git a/aat/cpp/include/aat/core/data/data.hpp b/aat/cpp/include/aat/core/data/data.hpp index 18cb8fcf..3a0a1490 100644 --- a/aat/cpp/include/aat/core/data/data.hpp +++ b/aat/cpp/include/aat/core/data/data.hpp @@ -30,7 +30,7 @@ namespace core { , instrument(instrument) , exchange(exchange) , data(data) {} - + virtual ~Data() {} bool operator==(const Data& other); virtual str_t toString() const; virtual json toJson() const; diff --git a/aat/cpp/include/aat/core/data/order.hpp b/aat/cpp/include/aat/core/data/order.hpp index 1645ee78..eb7a7cf1 100644 --- a/aat/cpp/include/aat/core/data/order.hpp +++ b/aat/cpp/include/aat/core/data/order.hpp @@ -18,6 +18,7 @@ namespace core { Order(str_t id, timestamp_t timestamp, double volume, double price, Side side, Instrument& instrument, ExchangeType& exchange = NullExchange, double notional = 0.0, OrderType order_type = OrderType::MARKET, OrderFlag flag = OrderFlag::NONE, std::shared_ptr stop_target = nullptr); + virtual ~Order() {} bool finished() const; void finish(); diff --git a/aat/cpp/include/aat/core/data/trade.hpp b/aat/cpp/include/aat/core/data/trade.hpp index 7209ed9c..02cc7293 100644 --- a/aat/cpp/include/aat/core/data/trade.hpp +++ b/aat/cpp/include/aat/core/data/trade.hpp @@ -32,6 +32,7 @@ namespace core { // enforce that stop target match stop type // assert(maker_orders.size() > 0); // not necessarily } + virtual ~Trade() {} double slippage() const { diff --git a/aat/cpp/include/aat/core/exchange/exchange.hpp b/aat/cpp/include/aat/core/exchange/exchange.hpp index 2d569200..aa8108da 100644 --- a/aat/cpp/include/aat/core/exchange/exchange.hpp +++ b/aat/cpp/include/aat/core/exchange/exchange.hpp @@ -20,6 +20,8 @@ namespace core { explicit ExchangeType(str_t name) : name(name) {} + virtual ~ExchangeType() {} + str_t toString() const; virtual json toJson() const; diff --git a/aat/cpp/third/nlohmann_json/nlohmann/adl_serializer.hpp b/aat/cpp/third/nlohmann_json/nlohmann/adl_serializer.hpp index eeaa1425..f77f9447 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/adl_serializer.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/adl_serializer.hpp @@ -1,49 +1,55 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include +#include #include #include +#include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN -template +/// @sa https://json.nlohmann.me/api/adl_serializer/ +template struct adl_serializer { - /*! - @brief convert a JSON value to any value type - - This function is usually called by the `get()` function of the - @ref basic_json class (either explicit or via conversion operators). - - @param[in] j JSON value to read from - @param[in,out] val value to write to - */ - template - static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + /// @brief convert a JSON value to any value type + /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/ + template + static auto from_json(BasicJsonType && j, TargetType& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } - /*! - @brief convert any value type to a JSON value - - This function is usually called by the constructors of the @ref basic_json - class. + /// @brief convert a JSON value to any value type + /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/ + template + static auto from_json(BasicJsonType && j) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) + { + return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); + } - @param[in,out] j JSON value to write to - @param[in] val value to read from - */ - template - static auto to_json(BasicJsonType& j, ValueType&& val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + /// @brief convert any value type to a JSON value + /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/ + template + static auto to_json(BasicJsonType& j, TargetType && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/byte_container_with_subtype.hpp b/aat/cpp/third/nlohmann_json/nlohmann/byte_container_with_subtype.hpp new file mode 100644 index 00000000..1031cdcf --- /dev/null +++ b/aat/cpp/third/nlohmann_json/nlohmann/byte_container_with_subtype.hpp @@ -0,0 +1,103 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +#include // uint8_t, uint64_t +#include // tie +#include // move + +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN + +/// @brief an internal type for a backed binary type +/// @sa https://json.nlohmann.me/api/byte_container_with_subtype/ +template +class byte_container_with_subtype : public BinaryType +{ + public: + using container_type = BinaryType; + using subtype_type = std::uint64_t; + + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/ + byte_container_with_subtype() noexcept(noexcept(container_type())) + : container_type() + {} + + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/ + byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b))) + : container_type(b) + {} + + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/ + byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b)))) + : container_type(std::move(b)) + {} + + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/ + byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b))) + : container_type(b) + , m_subtype(subtype_) + , m_has_subtype(true) + {} + + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/ + byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b)))) + : container_type(std::move(b)) + , m_subtype(subtype_) + , m_has_subtype(true) + {} + + bool operator==(const byte_container_with_subtype& rhs) const + { + return std::tie(static_cast(*this), m_subtype, m_has_subtype) == + std::tie(static_cast(rhs), rhs.m_subtype, rhs.m_has_subtype); + } + + bool operator!=(const byte_container_with_subtype& rhs) const + { + return !(rhs == *this); + } + + /// @brief sets the binary subtype + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/ + void set_subtype(subtype_type subtype_) noexcept + { + m_subtype = subtype_; + m_has_subtype = true; + } + + /// @brief return the binary subtype + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/ + constexpr subtype_type subtype() const noexcept + { + return m_has_subtype ? m_subtype : static_cast(-1); + } + + /// @brief return whether the value has a subtype + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/ + constexpr bool has_subtype() const noexcept + { + return m_has_subtype; + } + + /// @brief clears the binary subtype + /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/ + void clear_subtype() noexcept + { + m_subtype = 0; + m_has_subtype = false; + } + + private: + subtype_type m_subtype = 0; + bool m_has_subtype = false; +}; + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/abi_macros.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/abi_macros.hpp new file mode 100644 index 00000000..0d3108d1 --- /dev/null +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/abi_macros.hpp @@ -0,0 +1,100 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +// This file contains all macro definitions affecting or depending on the ABI + +#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK + #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH) + #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 2 + #warning "Already included a different version of the library!" + #endif + #endif +#endif + +#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_PATCH 2 // NOLINT(modernize-macro-to-enum) + +#ifndef JSON_DIAGNOSTICS + #define JSON_DIAGNOSTICS 0 +#endif + +#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0 +#endif + +#if JSON_DIAGNOSTICS + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag +#else + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS +#endif + +#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp +#else + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION + #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0 +#endif + +// Construct the namespace ABI tags component +#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b +#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \ + NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) + +#define NLOHMANN_JSON_ABI_TAGS \ + NLOHMANN_JSON_ABI_TAGS_CONCAT( \ + NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \ + NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON) + +// Construct the namespace version component +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \ + _v ## major ## _ ## minor ## _ ## patch +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) + +#if NLOHMANN_JSON_NAMESPACE_NO_VERSION +#define NLOHMANN_JSON_NAMESPACE_VERSION +#else +#define NLOHMANN_JSON_NAMESPACE_VERSION \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \ + NLOHMANN_JSON_VERSION_MINOR, \ + NLOHMANN_JSON_VERSION_PATCH) +#endif + +// Combine namespace components +#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b +#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \ + NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) + +#ifndef NLOHMANN_JSON_NAMESPACE +#define NLOHMANN_JSON_NAMESPACE \ + nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN +#define NLOHMANN_JSON_NAMESPACE_BEGIN \ + namespace nlohmann \ + { \ + inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) \ + { +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_END +#define NLOHMANN_JSON_NAMESPACE_END \ + } /* namespace (inline namespace) NOLINT(readability/namespace) */ \ + } // namespace nlohmann +#endif diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/from_json.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/from_json.hpp index c389dca7..c6299aa0 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/from_json.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/from_json.hpp @@ -1,8 +1,15 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // transform #include // array -#include // and, not #include // forward_list #include // inserter, front_inserter, end #include // map @@ -16,28 +23,31 @@ #include #include #include +#include +#include #include +#include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + template -void from_json(const BasicJsonType& j, typename std::nullptr_t& n) +inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { - if (JSON_HEDLEY_UNLIKELY(not j.is_null())) + if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j)); } n = nullptr; } // overloads for basic_json template parameters -template::value and - not std::is_same::value, - int> = 0> +template < typename BasicJsonType, typename ArithmeticType, + enable_if_t < std::is_arithmetic::value&& + !std::is_same::value, + int > = 0 > void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) @@ -58,83 +68,92 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) break; } + case value_t::null: + case value_t::object: + case value_t::array: + case value_t::string: + case value_t::boolean: + case value_t::binary: + case value_t::discarded: default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j)); } } template -void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) +inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { - if (JSON_HEDLEY_UNLIKELY(not j.is_boolean())) + if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j)); } b = *j.template get_ptr(); } template -void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) +inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { - if (JSON_HEDLEY_UNLIKELY(not j.is_string())) + if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j)); } s = *j.template get_ptr(); } template < - typename BasicJsonType, typename ConstructibleStringType, + typename BasicJsonType, typename StringType, enable_if_t < - is_constructible_string_type::value and - not std::is_same::value, - int > = 0 > -void from_json(const BasicJsonType& j, ConstructibleStringType& s) + std::is_assignable::value + && is_detected_exact::value + && !std::is_same::value + && !is_json_ref::value, int > = 0 > +inline void from_json(const BasicJsonType& j, StringType& s) { - if (JSON_HEDLEY_UNLIKELY(not j.is_string())) + if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j)); } s = *j.template get_ptr(); } template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) +inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) { get_arithmetic_value(j, val); } template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) +inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) { get_arithmetic_value(j, val); } template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) +inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) { get_arithmetic_value(j, val); } +#if !JSON_DISABLE_ENUM_SERIALIZATION template::value, int> = 0> -void from_json(const BasicJsonType& j, EnumType& e) +inline void from_json(const BasicJsonType& j, EnumType& e) { typename std::underlying_type::type val; get_arithmetic_value(j, val); e = static_cast(val); } +#endif // JSON_DISABLE_ENUM_SERIALIZATION // forward_list doesn't have an insert method template::value, int> = 0> -void from_json(const BasicJsonType& j, std::forward_list& l) + enable_if_t::value, int> = 0> +inline void from_json(const BasicJsonType& j, std::forward_list& l) { - if (JSON_HEDLEY_UNLIKELY(not j.is_array())) + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -146,19 +165,23 @@ void from_json(const BasicJsonType& j, std::forward_list& l) // valarray doesn't have an insert method template::value, int> = 0> -void from_json(const BasicJsonType& j, std::valarray& l) + enable_if_t::value, int> = 0> +inline void from_json(const BasicJsonType& j, std::valarray& l) { - if (JSON_HEDLEY_UNLIKELY(not j.is_array())) + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.resize(j.size()); - std::copy(j.begin(), j.end(), std::begin(l)); + std::transform(j.begin(), j.end(), std::begin(l), + [](const BasicJsonType & elem) + { + return elem.template get(); + }); } -template -auto from_json(const BasicJsonType& j, T (&arr)[N]) +template +auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) -> decltype(j.template get(), void()) { for (std::size_t i = 0; i < N; ++i) @@ -168,12 +191,12 @@ auto from_json(const BasicJsonType& j, T (&arr)[N]) } template -void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) +inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) { arr = *j.template get_ptr(); } -template +template auto from_json_array_impl(const BasicJsonType& j, std::array& arr, priority_tag<2> /*unused*/) -> decltype(j.template get(), void()) @@ -184,7 +207,10 @@ auto from_json_array_impl(const BasicJsonType& j, std::array& arr, } } -template +template::value, + int> = 0> auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/) -> decltype( arr.reserve(std::declval()), @@ -205,9 +231,12 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p arr = std::move(ret); } -template -void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, - priority_tag<0> /*unused*/) +template::value, + int> = 0> +inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, + priority_tag<0> /*unused*/) { using std::end; @@ -223,39 +252,68 @@ void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, arr = std::move(ret); } -template ::value and - not is_constructible_object_type::value and - not is_constructible_string_type::value and - not is_basic_json::value, - int > = 0 > - +template < typename BasicJsonType, typename ConstructibleArrayType, + enable_if_t < + is_constructible_array_type::value&& + !is_constructible_object_type::value&& + !is_constructible_string_type::value&& + !std::is_same::value&& + !is_basic_json::value, + int > = 0 > auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr) -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}), j.template get(), void()) { - if (JSON_HEDLEY_UNLIKELY(not j.is_array())) + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } from_json_array_impl(j, arr, priority_tag<3> {}); } +template < typename BasicJsonType, typename T, std::size_t... Idx > +std::array from_json_inplace_array_impl(BasicJsonType&& j, + identity_tag> /*unused*/, index_sequence /*unused*/) +{ + return { { std::forward(j).at(Idx).template get()... } }; +} + +template < typename BasicJsonType, typename T, std::size_t N > +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {})) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); + } + + return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); +} + +template +inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) + { + JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j)); + } + + bin = *j.template get_ptr(); +} + template::value, int> = 0> -void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) +inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { - if (JSON_HEDLEY_UNLIKELY(not j.is_object())) + if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j)); } ConstructibleObjectType ret; - auto inner_object = j.template get_ptr(); + const auto* inner_object = j.template get_ptr(); using value_type = typename ConstructibleObjectType::value_type; std::transform( inner_object->begin(), inner_object->end(), @@ -271,15 +329,15 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) // (BooleanType, etc..); note: Is it really necessary to provide explicit // overloads for boolean_t etc. in case of a custom BooleanType which is not // an arithmetic type? -template::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value and - not std::is_same::value, - int> = 0> -void from_json(const BasicJsonType& j, ArithmeticType& val) +template < typename BasicJsonType, typename ArithmeticType, + enable_if_t < + std::is_arithmetic::value&& + !std::is_same::value&& + !std::is_same::value&& + !std::is_same::value&& + !std::is_same::value, + int > = 0 > +inline void from_json(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) { @@ -304,86 +362,136 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) break; } + case value_t::null: + case value_t::object: + case value_t::array: + case value_t::string: + case value_t::binary: + case value_t::discarded: default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j)); } } +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get()...); +} + +template < typename BasicJsonType, class A1, class A2 > +std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +{ + return {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get()}; +} + template -void from_json(const BasicJsonType& j, std::pair& p) +inline void from_json_tuple_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { - p = {j.at(0).template get(), j.at(1).template get()}; + p = from_json_tuple_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } -template -void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence /*unused*/) +template +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<2> /*unused*/) { - t = std::make_tuple(j.at(Idx).template get::type>()...); + return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } template -void from_json(const BasicJsonType& j, std::tuple& t) +inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<3> /*unused*/) { - from_json_tuple_impl(j, t, index_sequence_for {}); + t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } -template ::value>> -void from_json(const BasicJsonType& j, std::map& m) +template +auto from_json(BasicJsonType&& j, TupleRelated&& t) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {})) { - if (JSON_HEDLEY_UNLIKELY(not j.is_array())) + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); + } + + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {}); +} + +template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, + typename = enable_if_t < !std::is_constructible < + typename BasicJsonType::string_t, Key >::value >> +inline void from_json(const BasicJsonType& j, std::map& m) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } m.clear(); for (const auto& p : j) { - if (JSON_HEDLEY_UNLIKELY(not p.is_array())) + if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } } -template ::value>> -void from_json(const BasicJsonType& j, std::unordered_map& m) +template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator, + typename = enable_if_t < !std::is_constructible < + typename BasicJsonType::string_t, Key >::value >> +inline void from_json(const BasicJsonType& j, std::unordered_map& m) { - if (JSON_HEDLEY_UNLIKELY(not j.is_array())) + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } m.clear(); for (const auto& p : j) { - if (JSON_HEDLEY_UNLIKELY(not p.is_array())) + if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } } +#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM +template +inline void from_json(const BasicJsonType& j, std_fs::path& p) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_string())) + { + JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j)); + } + p = *j.template get_ptr(); +} +#endif + struct from_json_fn { template - auto operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(from_json(j, val))) - -> decltype(from_json(j, val), void()) + auto operator()(const BasicJsonType& j, T&& val) const + noexcept(noexcept(from_json(j, std::forward(val)))) + -> decltype(from_json(j, std::forward(val))) { - return from_json(j, val); + return from_json(j, std::forward(val)); } }; + } // namespace detail +#ifndef JSON_HAS_CPP_17 /// namespace to hold default `from_json` function /// to see why this is required: /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html -namespace +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& from_json = detail::static_const::value; -} // namespace -} // namespace nlohmann +#endif +JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers) + detail::static_const::value; +#ifndef JSON_HAS_CPP_17 +} // namespace +#endif + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_chars.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_chars.hpp index d99703a5..febef932 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_chars.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_chars.hpp @@ -1,17 +1,24 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2009 Florian Loitsch +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // array -#include // assert -#include // or, and, not #include // signbit, isfinite #include // intN_t, uintN_t #include // memcpy, memmove #include // numeric_limits #include // conditional + #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { @@ -37,7 +44,7 @@ For a detailed description of the algorithm see: namespace dtoa_impl { -template +template Target reinterpret_bits(const Source source) { static_assert(sizeof(Target) == sizeof(Source), "size mismatch"); @@ -62,8 +69,8 @@ struct diyfp // f * 2^e */ static diyfp sub(const diyfp& x, const diyfp& y) noexcept { - assert(x.e == y.e); - assert(x.f >= y.f); + JSON_ASSERT(x.e == y.e); + JSON_ASSERT(x.f >= y.f); return {x.f - y.f, x.e}; } @@ -139,7 +146,7 @@ struct diyfp // f * 2^e */ static diyfp normalize(diyfp x) noexcept { - assert(x.f != 0); + JSON_ASSERT(x.f != 0); while ((x.f >> 63u) == 0) { @@ -158,8 +165,8 @@ struct diyfp // f * 2^e { const int delta = x.e - target_exponent; - assert(delta >= 0); - assert(((x.f << delta) >> delta) == x.f); + JSON_ASSERT(delta >= 0); + JSON_ASSERT(((x.f << delta) >> delta) == x.f); return {x.f << delta, target_exponent}; } @@ -178,11 +185,11 @@ boundaries. @pre value must be finite and positive */ -template +template boundaries compute_boundaries(FloatType value) { - assert(std::isfinite(value)); - assert(value > 0); + JSON_ASSERT(std::isfinite(value)); + JSON_ASSERT(value > 0); // Convert the IEEE representation into a diyfp. // @@ -201,7 +208,7 @@ boundaries compute_boundaries(FloatType value) using bits_type = typename std::conditional::type; - const std::uint64_t bits = reinterpret_bits(value); + const auto bits = static_cast(reinterpret_bits(value)); const std::uint64_t E = bits >> (kPrecision - 1); const std::uint64_t F = bits & (kHiddenBit - 1); @@ -231,7 +238,7 @@ boundaries compute_boundaries(FloatType value) // -----------------+------+------+-------------+-------------+--- (B) // v- m- v m+ v+ - const bool lower_boundary_is_closer = F == 0 and E > 1; + const bool lower_boundary_is_closer = F == 0 && E > 1; const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1); const diyfp m_minus = lower_boundary_is_closer ? diyfp(4 * v.f - 1, v.e - 2) // (B) @@ -462,18 +469,18 @@ inline cached_power get_cached_power_for_binary_exponent(int e) // k = ceil((kAlpha - e - 1) * 0.30102999566398114) // for |e| <= 1500, but doesn't require floating-point operations. // NB: log_10(2) ~= 78913 / 2^18 - assert(e >= -1500); - assert(e <= 1500); + JSON_ASSERT(e >= -1500); + JSON_ASSERT(e <= 1500); const int f = kAlpha - e - 1; const int k = (f * 78913) / (1 << 18) + static_cast(f > 0); const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep; - assert(index >= 0); - assert(static_cast(index) < kCachedPowers.size()); + JSON_ASSERT(index >= 0); + JSON_ASSERT(static_cast(index) < kCachedPowers.size()); const cached_power cached = kCachedPowers[static_cast(index)]; - assert(kAlpha <= cached.e + e + 64); - assert(kGamma >= cached.e + e + 64); + JSON_ASSERT(kAlpha <= cached.e + e + 64); + JSON_ASSERT(kGamma >= cached.e + e + 64); return cached; } @@ -491,60 +498,58 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) return 10; } // LCOV_EXCL_STOP - else if (n >= 100000000) + if (n >= 100000000) { pow10 = 100000000; return 9; } - else if (n >= 10000000) + if (n >= 10000000) { pow10 = 10000000; return 8; } - else if (n >= 1000000) + if (n >= 1000000) { pow10 = 1000000; return 7; } - else if (n >= 100000) + if (n >= 100000) { pow10 = 100000; return 6; } - else if (n >= 10000) + if (n >= 10000) { pow10 = 10000; return 5; } - else if (n >= 1000) + if (n >= 1000) { pow10 = 1000; return 4; } - else if (n >= 100) + if (n >= 100) { pow10 = 100; return 3; } - else if (n >= 10) + if (n >= 10) { pow10 = 10; return 2; } - else - { - pow10 = 1; - return 1; - } + + pow10 = 1; + return 1; } inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k) { - assert(len >= 1); - assert(dist <= delta); - assert(rest <= delta); - assert(ten_k > 0); + JSON_ASSERT(len >= 1); + JSON_ASSERT(dist <= delta); + JSON_ASSERT(rest <= delta); + JSON_ASSERT(ten_k > 0); // <--------------------------- delta ----> // <---- dist ---------> @@ -566,10 +571,10 @@ inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t d // integer arithmetic. while (rest < dist - and delta - rest >= ten_k - and (rest + ten_k < dist or dist - rest > rest + ten_k - dist)) + && delta - rest >= ten_k + && (rest + ten_k < dist || dist - rest > rest + ten_k - dist)) { - assert(buf[len - 1] != '0'); + JSON_ASSERT(buf[len - 1] != '0'); buf[len - 1]--; rest += ten_k; } @@ -597,8 +602,8 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // Grisu2 generates the digits of M+ from left to right and stops as soon as // V is in [M-,M+]. - assert(M_plus.e >= kAlpha); - assert(M_plus.e <= kGamma); + JSON_ASSERT(M_plus.e >= kAlpha); + JSON_ASSERT(M_plus.e <= kGamma); std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e) std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e) @@ -619,9 +624,9 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0] - assert(p1 > 0); + JSON_ASSERT(p1 > 0); - std::uint32_t pow10; + std::uint32_t pow10{}; const int k = find_largest_pow10(p1, pow10); // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) @@ -655,7 +660,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e) // - assert(d <= 9); + JSON_ASSERT(d <= 9); buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d // // M+ = buffer * 10^(n-1) + (r + p2 * 2^e) @@ -742,7 +747,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // // and stop as soon as 10^-m * r * 2^e <= delta * 2^e - assert(p2 > delta); + JSON_ASSERT(p2 > delta); int m = 0; for (;;) @@ -753,7 +758,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e // - assert(p2 <= (std::numeric_limits::max)() / 10); + JSON_ASSERT(p2 <= (std::numeric_limits::max)() / 10); p2 *= 10; const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e @@ -762,7 +767,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e)) // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e // - assert(d <= 9); + JSON_ASSERT(d <= 9); buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d // // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e @@ -823,8 +828,8 @@ JSON_HEDLEY_NON_NULL(1) inline void grisu2(char* buf, int& len, int& decimal_exponent, diyfp m_minus, diyfp v, diyfp m_plus) { - assert(m_plus.e == m_minus.e); - assert(m_plus.e == v.e); + JSON_ASSERT(m_plus.e == m_minus.e); + JSON_ASSERT(m_plus.e == v.e); // --------(-----------------------+-----------------------)-------- (A) // m- v m+ @@ -878,15 +883,15 @@ v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. >= max_digits10. */ -template +template JSON_HEDLEY_NON_NULL(1) void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value) { static_assert(diyfp::kPrecision >= std::numeric_limits::digits + 3, "internal error: not enough precision"); - assert(std::isfinite(value)); - assert(value > 0); + JSON_ASSERT(std::isfinite(value)); + JSON_ASSERT(value > 0); // If the neighbors (and boundaries) of 'value' are always computed for double-precision // numbers, all float's can be recovered using strtod (and strtof). However, the resulting @@ -894,7 +899,7 @@ void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value) // // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars) // says "value is converted to a string as if by std::sprintf in the default ("C") locale" - // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars' + // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars' // does. // On the other hand, the documentation for 'std::to_chars' requires that "parsing the // representation using the corresponding std::from_chars function recovers value exactly". That @@ -922,8 +927,8 @@ JSON_HEDLEY_NON_NULL(1) JSON_HEDLEY_RETURNS_NON_NULL inline char* append_exponent(char* buf, int e) { - assert(e > -1000); - assert(e < 1000); + JSON_ASSERT(e > -1000); + JSON_ASSERT(e < 1000); if (e < 0) { @@ -975,8 +980,8 @@ JSON_HEDLEY_RETURNS_NON_NULL inline char* format_buffer(char* buf, int len, int decimal_exponent, int min_exp, int max_exp) { - assert(min_exp < 0); - assert(max_exp > 0); + JSON_ASSERT(min_exp < 0); + JSON_ASSERT(max_exp > 0); const int k = len; const int n = len + decimal_exponent; @@ -985,40 +990,40 @@ inline char* format_buffer(char* buf, int len, int decimal_exponent, // k is the length of the buffer (number of decimal digits) // n is the position of the decimal point relative to the start of the buffer. - if (k <= n and n <= max_exp) + if (k <= n && n <= max_exp) { // digits[000] // len <= max_exp + 2 - std::memset(buf + k, '0', static_cast(n - k)); + std::memset(buf + k, '0', static_cast(n) - static_cast(k)); // Make it look like a floating-point number (#362, #378) buf[n + 0] = '.'; buf[n + 1] = '0'; - return buf + (n + 2); + return buf + (static_cast(n) + 2); } - if (0 < n and n <= max_exp) + if (0 < n && n <= max_exp) { // dig.its // len <= max_digits10 + 1 - assert(k > n); + JSON_ASSERT(k > n); - std::memmove(buf + (n + 1), buf + n, static_cast(k - n)); + std::memmove(buf + (static_cast(n) + 1), buf + n, static_cast(k) - static_cast(n)); buf[n] = '.'; - return buf + (k + 1); + return buf + (static_cast(k) + 1U); } - if (min_exp < n and n <= 0) + if (min_exp < n && n <= 0) { // 0.[000]digits // len <= 2 + (-min_exp - 1) + max_digits10 - std::memmove(buf + (2 + -n), buf, static_cast(k)); + std::memmove(buf + (2 + static_cast(-n)), buf, static_cast(k)); buf[0] = '0'; buf[1] = '.'; std::memset(buf + 2, '0', static_cast(-n)); - return buf + (2 + (-n) + k); + return buf + (2U + static_cast(-n) + static_cast(k)); } if (k == 1) @@ -1033,16 +1038,16 @@ inline char* format_buffer(char* buf, int len, int decimal_exponent, // d.igitsE+123 // len <= max_digits10 + 1 + 5 - std::memmove(buf + 2, buf + 1, static_cast(k - 1)); + std::memmove(buf + 2, buf + 1, static_cast(k) - 1); buf[1] = '.'; - buf += 1 + k; + buf += 1 + static_cast(k); } *buf++ = 'e'; return append_exponent(buf, n - 1); } -} // namespace dtoa_impl +} // namespace dtoa_impl /*! @brief generates a decimal representation of the floating-point number value in [first, last). @@ -1054,13 +1059,13 @@ format. Returns an iterator pointing past-the-end of the decimal representation. @note The buffer must be large enough. @note The result is NOT null-terminated. */ -template +template JSON_HEDLEY_NON_NULL(1, 2) JSON_HEDLEY_RETURNS_NON_NULL char* to_chars(char* first, const char* last, FloatType value) { static_cast(last); // maybe unused - fix warning - assert(std::isfinite(value)); + JSON_ASSERT(std::isfinite(value)); // Use signbit(value) instead of (value < 0) since signbit works for -0. if (std::signbit(value)) @@ -1069,6 +1074,10 @@ char* to_chars(char* first, const char* last, FloatType value) *first++ = '-'; } +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wfloat-equal" +#endif if (value == 0) // +-0 { *first++ = '0'; @@ -1077,8 +1086,11 @@ char* to_chars(char* first, const char* last, FloatType value) *first++ = '0'; return first; } +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif - assert(last - first >= std::numeric_limits::max_digits10); + JSON_ASSERT(last - first >= std::numeric_limits::max_digits10); // Compute v = buffer * 10^decimal_exponent. // The decimal digits are stored in the buffer, which needs to be interpreted @@ -1088,19 +1100,19 @@ char* to_chars(char* first, const char* last, FloatType value) int decimal_exponent = 0; dtoa_impl::grisu2(first, len, decimal_exponent, value); - assert(len <= std::numeric_limits::max_digits10); + JSON_ASSERT(len <= std::numeric_limits::max_digits10); // Format the buffer like printf("%.*g", prec, value) constexpr int kMinExp = -4; // Use digits10 here to increase compatibility with version 2. constexpr int kMaxExp = std::numeric_limits::digits10; - assert(last - first >= kMaxExp + 2); - assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits::max_digits10); - assert(last - first >= std::numeric_limits::max_digits10 + 6); + JSON_ASSERT(last - first >= kMaxExp + 2); + JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits::max_digits10); + JSON_ASSERT(last - first >= std::numeric_limits::max_digits10 + 6); return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp); } -} // namespace detail -} // namespace nlohmann +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_json.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_json.hpp index a1def699..b33d726b 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_json.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/conversions/to_json.hpp @@ -1,7 +1,14 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // copy -#include // or, and, not #include // begin, end #include // string #include // tuple, get @@ -11,18 +18,27 @@ #include // vector #include +#include #include +#include #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + ////////////////// // constructors // ////////////////// +/* + * Note all external_constructor<>::construct functions need to call + * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an + * allocated value (e.g., a string). See bug issue + * https://github.com/nlohmann/json/issues/2865 for more information. + */ + template struct external_constructor; template<> @@ -31,6 +47,7 @@ struct external_constructor template static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept { + j.m_value.destroy(j.m_type); j.m_type = value_t::boolean; j.m_value = b; j.assert_invariant(); @@ -43,6 +60,7 @@ struct external_constructor template static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s) { + j.m_value.destroy(j.m_type); j.m_type = value_t::string; j.m_value = s; j.assert_invariant(); @@ -51,28 +69,53 @@ struct external_constructor template static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s) { + j.m_value.destroy(j.m_type); j.m_type = value_t::string; j.m_value = std::move(s); j.assert_invariant(); } - template::value, - int> = 0> + template < typename BasicJsonType, typename CompatibleStringType, + enable_if_t < !std::is_same::value, + int > = 0 > static void construct(BasicJsonType& j, const CompatibleStringType& str) { + j.m_value.destroy(j.m_type); j.m_type = value_t::string; j.m_value.string = j.template create(str); j.assert_invariant(); } }; +template<> +struct external_constructor +{ + template + static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b) + { + j.m_value.destroy(j.m_type); + j.m_type = value_t::binary; + j.m_value = typename BasicJsonType::binary_t(b); + j.assert_invariant(); + } + + template + static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b) + { + j.m_value.destroy(j.m_type); + j.m_type = value_t::binary; + j.m_value = typename BasicJsonType::binary_t(std::move(b)); + j.assert_invariant(); + } +}; + template<> struct external_constructor { template static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept { + j.m_value.destroy(j.m_type); j.m_type = value_t::number_float; j.m_value = val; j.assert_invariant(); @@ -85,6 +128,7 @@ struct external_constructor template static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept { + j.m_value.destroy(j.m_type); j.m_type = value_t::number_unsigned; j.m_value = val; j.assert_invariant(); @@ -97,6 +141,7 @@ struct external_constructor template static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept { + j.m_value.destroy(j.m_type); j.m_type = value_t::number_integer; j.m_value = val; j.assert_invariant(); @@ -109,40 +154,49 @@ struct external_constructor template static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr) { + j.m_value.destroy(j.m_type); j.m_type = value_t::array; j.m_value = arr; + j.set_parents(); j.assert_invariant(); } template static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr) { + j.m_value.destroy(j.m_type); j.m_type = value_t::array; j.m_value = std::move(arr); + j.set_parents(); j.assert_invariant(); } - template::value, - int> = 0> + template < typename BasicJsonType, typename CompatibleArrayType, + enable_if_t < !std::is_same::value, + int > = 0 > static void construct(BasicJsonType& j, const CompatibleArrayType& arr) { using std::begin; using std::end; + + j.m_value.destroy(j.m_type); j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); + j.set_parents(); j.assert_invariant(); } template static void construct(BasicJsonType& j, const std::vector& arr) { + j.m_value.destroy(j.m_type); j.m_type = value_t::array; j.m_value = value_t::array; j.m_value.array->reserve(arr.size()); for (const bool x : arr) { j.m_value.array->push_back(x); + j.set_parent(j.m_value.array->back()); } j.assert_invariant(); } @@ -151,6 +205,7 @@ struct external_constructor enable_if_t::value, int> = 0> static void construct(BasicJsonType& j, const std::valarray& arr) { + j.m_value.destroy(j.m_type); j.m_type = value_t::array; j.m_value = value_t::array; j.m_value.array->resize(arr.size()); @@ -158,6 +213,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } + j.set_parents(); j.assert_invariant(); } }; @@ -168,28 +224,34 @@ struct external_constructor template static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj) { + j.m_value.destroy(j.m_type); j.m_type = value_t::object; j.m_value = obj; + j.set_parents(); j.assert_invariant(); } template static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj) { + j.m_value.destroy(j.m_type); j.m_type = value_t::object; j.m_value = std::move(obj); + j.set_parents(); j.assert_invariant(); } - template::value, int> = 0> + template < typename BasicJsonType, typename CompatibleObjectType, + enable_if_t < !std::is_same::value, int > = 0 > static void construct(BasicJsonType& j, const CompatibleObjectType& obj) { using std::begin; using std::end; + j.m_value.destroy(j.m_type); j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); + j.set_parents(); j.assert_invariant(); } }; @@ -200,134 +262,163 @@ struct external_constructor template::value, int> = 0> -void to_json(BasicJsonType& j, T b) noexcept +inline void to_json(BasicJsonType& j, T b) noexcept { external_constructor::construct(j, b); } +template < typename BasicJsonType, typename BoolRef, + enable_if_t < + ((std::is_same::reference, BoolRef>::value + && !std::is_same ::reference, typename BasicJsonType::boolean_t&>::value) + || (std::is_same::const_reference, BoolRef>::value + && !std::is_same ::const_reference>, + typename BasicJsonType::boolean_t >::value)) + && std::is_convertible::value, int > = 0 > +inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept +{ + external_constructor::construct(j, static_cast(b)); +} + template::value, int> = 0> -void to_json(BasicJsonType& j, const CompatibleString& s) +inline void to_json(BasicJsonType& j, const CompatibleString& s) { external_constructor::construct(j, s); } template -void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s) +inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s) { external_constructor::construct(j, std::move(s)); } template::value, int> = 0> -void to_json(BasicJsonType& j, FloatType val) noexcept +inline void to_json(BasicJsonType& j, FloatType val) noexcept { external_constructor::construct(j, static_cast(val)); } template::value, int> = 0> -void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept +inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept { external_constructor::construct(j, static_cast(val)); } template::value, int> = 0> -void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept +inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept { external_constructor::construct(j, static_cast(val)); } +#if !JSON_DISABLE_ENUM_SERIALIZATION template::value, int> = 0> -void to_json(BasicJsonType& j, EnumType e) noexcept +inline void to_json(BasicJsonType& j, EnumType e) noexcept { using underlying_type = typename std::underlying_type::type; external_constructor::construct(j, static_cast(e)); } +#endif // JSON_DISABLE_ENUM_SERIALIZATION template -void to_json(BasicJsonType& j, const std::vector& e) +inline void to_json(BasicJsonType& j, const std::vector& e) { external_constructor::construct(j, e); } -template ::value and - not is_compatible_object_type< - BasicJsonType, CompatibleArrayType>::value and - not is_compatible_string_type::value and - not is_basic_json::value, - int> = 0> -void to_json(BasicJsonType& j, const CompatibleArrayType& arr) +template < typename BasicJsonType, typename CompatibleArrayType, + enable_if_t < is_compatible_array_type::value&& + !is_compatible_object_type::value&& + !is_compatible_string_type::value&& + !std::is_same::value&& + !is_basic_json::value, + int > = 0 > +inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr) { external_constructor::construct(j, arr); } +template +inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin) +{ + external_constructor::construct(j, bin); +} + template::value, int> = 0> -void to_json(BasicJsonType& j, const std::valarray& arr) +inline void to_json(BasicJsonType& j, const std::valarray& arr) { external_constructor::construct(j, std::move(arr)); } template -void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr) +inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr) { external_constructor::construct(j, std::move(arr)); } -template::value and not is_basic_json::value, int> = 0> -void to_json(BasicJsonType& j, const CompatibleObjectType& obj) +template < typename BasicJsonType, typename CompatibleObjectType, + enable_if_t < is_compatible_object_type::value&& !is_basic_json::value, int > = 0 > +inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj) { external_constructor::construct(j, obj); } template -void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) +inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) { external_constructor::construct(j, std::move(obj)); } template < typename BasicJsonType, typename T, std::size_t N, - enable_if_t::value, - int> = 0 > -void to_json(BasicJsonType& j, const T(&arr)[N]) + enable_if_t < !std::is_constructible::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) + int > = 0 > +inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { external_constructor::construct(j, arr); } template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible::value&& std::is_constructible::value, int > = 0 > -void to_json(BasicJsonType& j, const std::pair& p) +inline void to_json(BasicJsonType& j, const std::pair& p) { j = { p.first, p.second }; } // for https://github.com/nlohmann/json/pull/1134 -template < typename BasicJsonType, typename T, - enable_if_t>::value, int> = 0> -void to_json(BasicJsonType& j, const T& b) +template>::value, int> = 0> +inline void to_json(BasicJsonType& j, const T& b) { j = { {b.key(), b.value()} }; } template -void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence /*unused*/) +inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence /*unused*/) { j = { std::get(t)... }; } template::value, int > = 0> -void to_json(BasicJsonType& j, const T& t) +inline void to_json(BasicJsonType& j, const T& t) { to_json_tuple_impl(j, t, make_index_sequence::value> {}); } +#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM +template +inline void to_json(BasicJsonType& j, const std_fs::path& p) +{ + j = p.string(); +} +#endif + struct to_json_fn { template @@ -339,9 +430,17 @@ struct to_json_fn }; } // namespace detail +#ifndef JSON_HAS_CPP_17 /// namespace to hold default `to_json` function -namespace +/// to see why this is required: +/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& to_json = detail::static_const::value; -} // namespace -} // namespace nlohmann +#endif +JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers) + detail::static_const::value; +#ifndef JSON_HAS_CPP_17 +} // namespace +#endif + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/exceptions.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/exceptions.hpp index ed836188..96d7e010 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/exceptions.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/exceptions.hpp @@ -1,68 +1,127 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once +#include // nullptr_t #include // exception #include // runtime_error #include // to_string +#include // vector +#include +#include #include #include +#include +#include +#include -namespace nlohmann -{ + +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + //////////////// // exceptions // //////////////// -/*! -@brief general exception of the @ref basic_json class - -This class is an extension of `std::exception` objects with a member @a id for -exception ids. It is used as the base class for all exceptions thrown by the -@ref basic_json class. This class can hence be used as "wildcard" to catch -exceptions. - -Subclasses: -- @ref parse_error for exceptions indicating a parse error -- @ref invalid_iterator for exceptions indicating errors with iterators -- @ref type_error for exceptions indicating executing a member function with - a wrong type -- @ref out_of_range for exceptions indicating access out of the defined range -- @ref other_error for exceptions indicating other library errors - -@internal -@note To have nothrow-copy-constructible exceptions, we internally use - `std::runtime_error` which can cope with arbitrary-length error messages. - Intermediate strings are built with static functions and then passed to - the actual constructor. -@endinternal - -@liveexample{The following code shows how arbitrary library exceptions can be -caught.,exception} - -@since version 3.0.0 -*/ +/// @brief general exception of the @ref basic_json class +/// @sa https://json.nlohmann.me/api/basic_json/exception/ class exception : public std::exception { public: /// returns the explanatory string - JSON_HEDLEY_RETURNS_NON_NULL const char* what() const noexcept override { return m.what(); } /// the id of the exception - const int id; + const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) protected: JSON_HEDLEY_NON_NULL(3) - exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} + exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing) static std::string name(const std::string& ename, int id_) { - return "[json.exception." + ename + "." + std::to_string(id_) + "] "; + return concat("[json.exception.", ename, '.', std::to_string(id_), "] "); + } + + static std::string diagnostics(std::nullptr_t /*leaf_element*/) + { + return ""; + } + + template + static std::string diagnostics(const BasicJsonType* leaf_element) + { +#if JSON_DIAGNOSTICS + std::vector tokens; + for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (¤t->m_parent->m_value.array->operator[](i) == current) + { + tokens.emplace_back(std::to_string(i)); + break; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (&element.second == current) + { + tokens.emplace_back(element.first.c_str()); + break; + } + } + break; + } + + case value_t::null: // LCOV_EXCL_LINE + case value_t::string: // LCOV_EXCL_LINE + case value_t::boolean: // LCOV_EXCL_LINE + case value_t::number_integer: // LCOV_EXCL_LINE + case value_t::number_unsigned: // LCOV_EXCL_LINE + case value_t::number_float: // LCOV_EXCL_LINE + case value_t::binary: // LCOV_EXCL_LINE + case value_t::discarded: // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE + } + } + + if (tokens.empty()) + { + return ""; + } + + auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return concat(a, '/', detail::escape(b)); + }); + return concat('(', str, ") "); +#else + static_cast(leaf_element); + return ""; +#endif } private: @@ -70,50 +129,8 @@ class exception : public std::exception std::runtime_error m; }; -/*! -@brief exception indicating a parse error - -This exception is thrown by the library when a parse error occurs. Parse errors -can occur during the deserialization of JSON text, CBOR, MessagePack, as well -as when using JSON Patch. - -Member @a byte holds the byte index of the last read character in the input -file. - -Exceptions have ids 1xx. - -name / id | example message | description ------------------------------- | --------------- | ------------------------- -json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position. -json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. -json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. -json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. -json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. -json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`. -json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. -json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. -json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number. -json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. -json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read. -json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read. -json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet). - -@note For an input with n bytes, 1 is the index of the first character and n+1 - is the index of the terminating null byte or the end of file. This also - holds true when reading a byte vector (CBOR or MessagePack). - -@liveexample{The following code shows how a `parse_error` exception can be -caught.,parse_error} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ +/// @brief exception indicating a parse error +/// @sa https://json.nlohmann.me/api/basic_json/parse_error/ class parse_error : public exception { public: @@ -126,19 +143,21 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template::value, int> = 0> + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; - return parse_error(id_, pos.chars_read_total, w.c_str()); + std::string w = concat(exception::name("parse_error", id_), "parse error", + position_string(pos), ": ", exception::diagnostics(context), what_arg); + return {id_, pos.chars_read_total, w.c_str()}; } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template::value, int> = 0> + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("parse_error", id_) + "parse error" + - (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; - return parse_error(id_, byte_, w.c_str()); + std::string w = concat(exception::name("parse_error", id_), "parse error", + (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""), + ": ", exception::diagnostics(context), what_arg); + return {id_, byte_, w.c_str()}; } /*! @@ -158,55 +177,21 @@ class parse_error : public exception static std::string position_string(const position_t& pos) { - return " at line " + std::to_string(pos.lines_read + 1) + - ", column " + std::to_string(pos.chars_read_current_line); + return concat(" at line ", std::to_string(pos.lines_read + 1), + ", column ", std::to_string(pos.chars_read_current_line)); } }; -/*! -@brief exception indicating errors with iterators - -This exception is thrown if iterators passed to a library function do not match -the expected semantics. - -Exceptions have ids 2xx. - -name / id | example message | description ------------------------------------ | --------------- | ------------------------- -json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. -json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. -json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. -json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid. -json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid. -json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range. -json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. -json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. -json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. -json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. -json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to. -json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container. -json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered. -json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin(). - -@liveexample{The following code shows how an `invalid_iterator` exception can be -caught.,invalid_iterator} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ +/// @brief exception indicating errors with iterators +/// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/ class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template::value, int> = 0> + static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; - return invalid_iterator(id_, w.c_str()); + std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg); + return {id_, w.c_str()}; } private: @@ -215,52 +200,16 @@ class invalid_iterator : public exception : exception(id_, what_arg) {} }; -/*! -@brief exception indicating executing a member function with a wrong type - -This exception is thrown in case of a type error; that is, a library function is -executed on a JSON value whose type does not match the expected semantics. - -Exceptions have ids 3xx. - -name / id | example message | description ------------------------------ | --------------- | ------------------------- -json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. -json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. -json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t &. -json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types. -json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types. -json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types. -json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types. -json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types. -json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types. -json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types. -json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types. -json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types. -json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined. -json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers. -json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive. -json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. | -json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) | - -@liveexample{The following code shows how a `type_error` exception can be -caught.,type_error} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ +/// @brief exception indicating executing a member function with a wrong type +/// @sa https://json.nlohmann.me/api/basic_json/type_error/ class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template::value, int> = 0> + static type_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("type_error", id_) + what_arg; - return type_error(id_, w.c_str()); + std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg); + return {id_, w.c_str()}; } private: @@ -268,46 +217,16 @@ class type_error : public exception type_error(int id_, const char* what_arg) : exception(id_, what_arg) {} }; -/*! -@brief exception indicating access out of the defined range - -This exception is thrown in case a library function is called on an input -parameter that exceeds the expected range, for instance in case of array -indices or nonexisting object keys. - -Exceptions have ids 4xx. - -name / id | example message | description -------------------------------- | --------------- | ------------------------- -json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1. -json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it. -json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object. -json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved. -json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. -json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF. -json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. | -json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. | -json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string | - -@liveexample{The following code shows how an `out_of_range` exception can be -caught.,out_of_range} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ +/// @brief exception indicating access out of the defined range +/// @sa https://json.nlohmann.me/api/basic_json/out_of_range/ class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template::value, int> = 0> + static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("out_of_range", id_) + what_arg; - return out_of_range(id_, w.c_str()); + std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg); + return {id_, w.c_str()}; } private: @@ -315,42 +234,22 @@ class out_of_range : public exception out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {} }; -/*! -@brief exception indicating other library errors - -This exception is thrown in case of errors that cannot be classified with the -other exception types. - -Exceptions have ids 5xx. - -name / id | example message | description ------------------------------- | --------------- | ------------------------- -json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range - -@liveexample{The following code shows how an `other_error` exception can be -caught.,other_error} - -@since version 3.0.0 -*/ +/// @brief exception indicating other library errors +/// @sa https://json.nlohmann.me/api/basic_json/other_error/ class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template::value, int> = 0> + static other_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - std::string w = exception::name("other_error", id_) + what_arg; - return other_error(id_, w.c_str()); + std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg); + return {id_, w.c_str()}; } private: JSON_HEDLEY_NON_NULL(3) other_error(int id_, const char* what_arg) : exception(id_, what_arg) {} }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/hash.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/hash.hpp new file mode 100644 index 00000000..3f05af83 --- /dev/null +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/hash.hpp @@ -0,0 +1,129 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +#include // uint8_t +#include // size_t +#include // hash + +#include +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +// boost::hash_combine +inline std::size_t combine(std::size_t seed, std::size_t h) noexcept +{ + seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U); + return seed; +} + +/*! +@brief hash a JSON value + +The hash function tries to rely on std::hash where possible. Furthermore, the +type of the JSON value is taken into account to have different hash values for +null, 0, 0U, and false, etc. + +@tparam BasicJsonType basic_json specialization +@param j JSON value to hash +@return hash value of j +*/ +template +std::size_t hash(const BasicJsonType& j) +{ + using string_t = typename BasicJsonType::string_t; + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + + const auto type = static_cast(j.type()); + switch (j.type()) + { + case BasicJsonType::value_t::null: + case BasicJsonType::value_t::discarded: + { + return combine(type, 0); + } + + case BasicJsonType::value_t::object: + { + auto seed = combine(type, j.size()); + for (const auto& element : j.items()) + { + const auto h = std::hash {}(element.key()); + seed = combine(seed, h); + seed = combine(seed, hash(element.value())); + } + return seed; + } + + case BasicJsonType::value_t::array: + { + auto seed = combine(type, j.size()); + for (const auto& element : j) + { + seed = combine(seed, hash(element)); + } + return seed; + } + + case BasicJsonType::value_t::string: + { + const auto h = std::hash {}(j.template get_ref()); + return combine(type, h); + } + + case BasicJsonType::value_t::boolean: + { + const auto h = std::hash {}(j.template get()); + return combine(type, h); + } + + case BasicJsonType::value_t::number_integer: + { + const auto h = std::hash {}(j.template get()); + return combine(type, h); + } + + case BasicJsonType::value_t::number_unsigned: + { + const auto h = std::hash {}(j.template get()); + return combine(type, h); + } + + case BasicJsonType::value_t::number_float: + { + const auto h = std::hash {}(j.template get()); + return combine(type, h); + } + + case BasicJsonType::value_t::binary: + { + auto seed = combine(type, j.get_binary().size()); + const auto h = std::hash {}(j.get_binary().has_subtype()); + seed = combine(seed, h); + seed = combine(seed, static_cast(j.get_binary().subtype())); + for (const auto byte : j.get_binary()) + { + seed = combine(seed, std::hash {}(byte)); + } + return seed; + } + + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE + return 0; // LCOV_EXCL_LINE + } +} + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/binary_reader.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/binary_reader.hpp index 1b6e0f9b..634615d3 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/binary_reader.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/binary_reader.hpp @@ -1,8 +1,15 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // generate_n #include // array -#include // assert #include // ldexp #include // size_t #include // uint8_t, uint16_t, uint32_t, uint64_t @@ -12,18 +19,43 @@ #include // numeric_limits #include // char_traits, string #include // make_pair, move +#include // vector #include #include #include +#include #include #include +#include +#include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + +/// how to treat CBOR tags +enum class cbor_tag_handler_t +{ + error, ///< throw a parse_error exception in case of a tag + ignore, ///< ignore tags + store ///< store tags as binary type +}; + +/*! +@brief determine system byte order + +@return true if and only if system's byte order is little endian + +@note from https://stackoverflow.com/a/1001328/266378 +*/ +static inline bool little_endianness(int num = 1) noexcept +{ + return *reinterpret_cast(&num) == 1; +} + + /////////////////// // binary reader // /////////////////// @@ -31,14 +63,17 @@ namespace detail /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template> +template> class binary_reader { using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; + using binary_t = typename BasicJsonType::binary_t; using json_sax_t = SAX; + using char_type = typename InputAdapterType::char_type; + using char_int_type = typename std::char_traits::int_type; public: /*! @@ -46,30 +81,31 @@ class binary_reader @param[in] adapter input adapter to read from */ - explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter)) + explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format) { (void)detail::is_sax_static_asserts {}; - assert(ia); } // make class move-only binary_reader(const binary_reader&) = delete; - binary_reader(binary_reader&&) = default; + binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) binary_reader& operator=(const binary_reader&) = delete; - binary_reader& operator=(binary_reader&&) = default; + binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~binary_reader() = default; /*! @param[in] format the binary format to parse @param[in] sax_ a SAX event processor @param[in] strict whether to expect the input to be consumed completed + @param[in] tag_handler how to treat CBOR tags - @return + @return whether parsing was successful */ JSON_HEDLEY_NON_NULL(3) bool sax_parse(const input_format_t format, json_sax_t* sax_, - const bool strict = true) + const bool strict = true, + const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { sax = sax_; bool result = false; @@ -81,7 +117,7 @@ class binary_reader break; case input_format_t::cbor: - result = parse_cbor_internal(); + result = parse_cbor_internal(true, tag_handler); break; case input_format_t::msgpack: @@ -89,17 +125,19 @@ class binary_reader break; case input_format_t::ubjson: + case input_format_t::bjdata: result = parse_ubjson_internal(); break; + case input_format_t::json: // LCOV_EXCL_LINE default: // LCOV_EXCL_LINE - assert(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } // strict mode: next byte must be EOF - if (result and strict) + if (result && strict) { - if (format == input_format_t::ubjson) + if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata) { get_ignore_noop(); } @@ -108,28 +146,16 @@ class binary_reader get(); } - if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) + if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { - return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read, + exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr)); } } return result; } - /*! - @brief determine system byte order - - @return true if and only if system's byte order is little endian - - @note from http://stackoverflow.com/a/1001328/266378 - */ - static constexpr bool little_endianess(int num = 1) noexcept - { - return *reinterpret_cast(&num) == 1; - } - private: ////////// // BSON // @@ -141,15 +167,15 @@ class binary_reader */ bool parse_bson_internal() { - std::int32_t document_size; + std::int32_t document_size{}; get_number(input_format_t::bson, document_size); - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast(-1)))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/false))) + if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false))) { return false; } @@ -159,7 +185,7 @@ class binary_reader /*! @brief Parses a C-style string from the BSON input. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @return `true` if the \x00-byte indicating the end of the string was encountered before the EOF; false` indicates an unexpected EOF. @@ -170,7 +196,7 @@ class binary_reader while (true) { get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring"))) { return false; } @@ -178,10 +204,8 @@ class binary_reader { return true; } - *out++ = static_cast(current); + *out++ = static_cast(current); } - - return true; } /*! @@ -189,7 +213,7 @@ class binary_reader input. @param[in] len The length (including the zero-byte at the end) of the string to be read. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @tparam NumberType The type of the length @a len @pre len >= 1 @@ -201,10 +225,38 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr)); + } + + return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); + } + + /*! + @brief Parses a byte array input of length @a len from the BSON input. + @param[in] len The length of the byte array to be read. + @param[in,out] result A reference to the binary variable where the read + array is to be stored. + @tparam NumberType The type of the length @a len + @pre len >= 0 + @return `true` if the byte array was successfully parsed + */ + template + bool get_bson_binary(const NumberType len, binary_t& result) + { + if (JSON_HEDLEY_UNLIKELY(len < 0)) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr)); } - return get_string(input_format_t::bson, len - static_cast(1), result) and get() != std::char_traits::eof(); + // All BSON binary values have a subtype + std::uint8_t subtype{}; + get_number(input_format_t::bson, subtype); + result.set_subtype(subtype); + + return get_binary(input_format_t::bson, len, result); } /*! @@ -217,22 +269,22 @@ class binary_reader Unsupported BSON record type 0x... @return whether a valid BSON-object/array was passed to the SAX parser */ - bool parse_bson_element_internal(const int element_type, + bool parse_bson_element_internal(const char_int_type element_type, const std::size_t element_type_parse_position) { switch (element_type) { case 0x01: // double { - double number; - return get_number(input_format_t::bson, number) and sax->number_float(static_cast(number), ""); + double number{}; + return get_number(input_format_t::bson, number) && sax->number_float(static_cast(number), ""); } case 0x02: // string { - std::int32_t len; + std::int32_t len{}; string_t value; - return get_number(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value); + return get_number(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value); } case 0x03: // object @@ -245,6 +297,13 @@ class binary_reader return parse_bson_array(); } + case 0x05: // binary + { + std::int32_t len{}; + binary_t value; + return get_number(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value); + } + case 0x08: // boolean { return sax->boolean(get() != 0); @@ -257,21 +316,23 @@ class binary_reader case 0x10: // int32 { - std::int32_t value; - return get_number(input_format_t::bson, value) and sax->number_integer(value); + std::int32_t value{}; + return get_number(input_format_t::bson, value) && sax->number_integer(value); } case 0x12: // int64 { - std::int64_t value; - return get_number(input_format_t::bson, value) and sax->number_integer(value); + std::int64_t value{}; + return get_number(input_format_t::bson, value) && sax->number_integer(value); } default: // anything else not supported (yet) { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + static_cast((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) + std::string cr_str{cr.data()}; + return sax->parse_error(element_type_parse_position, cr_str, + parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr)); } } } @@ -291,25 +352,26 @@ class binary_reader bool parse_bson_element_list(const bool is_array) { string_t key; - while (int element_type = get()) + + while (auto element_type = get()) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list"))) { return false; } const std::size_t element_type_parse_position = chars_read; - if (JSON_HEDLEY_UNLIKELY(not get_bson_cstr(key))) + if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key))) { return false; } - if (not is_array and not sax->key(key)) + if (!is_array && !sax->key(key)) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position))) + if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position))) { return false; } @@ -327,15 +389,15 @@ class binary_reader */ bool parse_bson_array() { - std::int32_t document_size; + std::int32_t document_size{}; get_number(input_format_t::bson, document_size); - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast(-1)))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/true))) + if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true))) { return false; } @@ -349,17 +411,19 @@ class binary_reader /*! @param[in] get_char whether a new character should be retrieved from the - input (true, default) or whether the last read - character should be considered instead + input (true) or whether the last read character should + be considered instead (false) + @param[in] tag_handler how CBOR tags should be treated @return whether a valid CBOR value was passed to the SAX parser */ - bool parse_cbor_internal(const bool get_char = true) + bool parse_cbor_internal(const bool get_char, + const cbor_tag_handler_t tag_handler) { switch (get_char ? get() : current) { // EOF - case std::char_traits::eof(): + case std::char_traits::eof(): return unexpect_eof(input_format_t::cbor, "value"); // Integer 0x00..0x17 (0..23) @@ -391,26 +455,26 @@ class binary_reader case 0x18: // Unsigned integer (one-byte uint8_t follows) { - std::uint8_t number; - return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + std::uint8_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); } case 0x19: // Unsigned integer (two-byte uint16_t follows) { - std::uint16_t number; - return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + std::uint16_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); } case 0x1A: // Unsigned integer (four-byte uint32_t follows) { - std::uint32_t number; - return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + std::uint32_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); } case 0x1B: // Unsigned integer (eight-byte uint64_t follows) { - std::uint64_t number; - return get_number(input_format_t::cbor, number) and sax->number_unsigned(number); + std::uint64_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); } // Negative integer -1-0x00..-1-0x17 (-1..-24) @@ -442,29 +506,64 @@ class binary_reader case 0x38: // Negative integer (one-byte uint8_t follows) { - std::uint8_t number; - return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + std::uint8_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); } case 0x39: // Negative integer -1-n (two-byte uint16_t follows) { - std::uint16_t number; - return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + std::uint16_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); } case 0x3A: // Negative integer -1-n (four-byte uint32_t follows) { - std::uint32_t number; - return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) - number); + std::uint32_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); } case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows) { - std::uint64_t number; - return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast(-1) + std::uint64_t number{}; + return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - static_cast(number)); } + // Binary data (0x00..0x17 bytes follow) + case 0x40: + case 0x41: + case 0x42: + case 0x43: + case 0x44: + case 0x45: + case 0x46: + case 0x47: + case 0x48: + case 0x49: + case 0x4A: + case 0x4B: + case 0x4C: + case 0x4D: + case 0x4E: + case 0x4F: + case 0x50: + case 0x51: + case 0x52: + case 0x53: + case 0x54: + case 0x55: + case 0x56: + case 0x57: + case 0x58: // Binary data (one-byte uint8_t for n follows) + case 0x59: // Binary data (two-byte uint16_t for n follow) + case 0x5A: // Binary data (four-byte uint32_t for n follow) + case 0x5B: // Binary data (eight-byte uint64_t for n follow) + case 0x5F: // Binary data (indefinite length) + { + binary_t b; + return get_cbor_binary(b) && sax->binary(b); + } + // UTF-8 string (0x00..0x17 bytes follow) case 0x60: case 0x61: @@ -497,7 +596,7 @@ class binary_reader case 0x7F: // UTF-8 string (indefinite length) { string_t s; - return get_cbor_string(s) and sax->string(s); + return get_cbor_string(s) && sax->string(s); } // array (0x00..0x17 data items follow) @@ -525,34 +624,35 @@ class binary_reader case 0x95: case 0x96: case 0x97: - return get_cbor_array(static_cast(static_cast(current) & 0x1Fu)); + return get_cbor_array( + conditional_static_cast(static_cast(current) & 0x1Fu), tag_handler); case 0x98: // array (one-byte uint8_t for n follows) { - std::uint8_t len; - return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + std::uint8_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); } case 0x99: // array (two-byte uint16_t for n follow) { - std::uint16_t len; - return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + std::uint16_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); } case 0x9A: // array (four-byte uint32_t for n follow) { - std::uint32_t len; - return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + std::uint32_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast(len), tag_handler); } case 0x9B: // array (eight-byte uint64_t for n follow) { - std::uint64_t len; - return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast(len)); + std::uint64_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast(len), tag_handler); } case 0x9F: // array (indefinite length) - return get_cbor_array(std::size_t(-1)); + return get_cbor_array(static_cast(-1), tag_handler); // map (0x00..0x17 pairs of data items follow) case 0xA0: @@ -579,34 +679,145 @@ class binary_reader case 0xB5: case 0xB6: case 0xB7: - return get_cbor_object(static_cast(static_cast(current) & 0x1Fu)); + return get_cbor_object(conditional_static_cast(static_cast(current) & 0x1Fu), tag_handler); case 0xB8: // map (one-byte uint8_t for n follows) { - std::uint8_t len; - return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + std::uint8_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); } case 0xB9: // map (two-byte uint16_t for n follow) { - std::uint16_t len; - return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + std::uint16_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); } case 0xBA: // map (four-byte uint32_t for n follow) { - std::uint32_t len; - return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + std::uint32_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast(len), tag_handler); } case 0xBB: // map (eight-byte uint64_t for n follow) { - std::uint64_t len; - return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast(len)); + std::uint64_t len{}; + return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast(len), tag_handler); } case 0xBF: // map (indefinite length) - return get_cbor_object(std::size_t(-1)); + return get_cbor_object(static_cast(-1), tag_handler); + + case 0xC6: // tagged item + case 0xC7: + case 0xC8: + case 0xC9: + case 0xCA: + case 0xCB: + case 0xCC: + case 0xCD: + case 0xCE: + case 0xCF: + case 0xD0: + case 0xD1: + case 0xD2: + case 0xD3: + case 0xD4: + case 0xD8: // tagged item (1 bytes follow) + case 0xD9: // tagged item (2 bytes follow) + case 0xDA: // tagged item (4 bytes follow) + case 0xDB: // tagged item (8 bytes follow) + { + switch (tag_handler) + { + case cbor_tag_handler_t::error: + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr)); + } + + case cbor_tag_handler_t::ignore: + { + // ignore binary subtype + switch (current) + { + case 0xD8: + { + std::uint8_t subtype_to_ignore{}; + get_number(input_format_t::cbor, subtype_to_ignore); + break; + } + case 0xD9: + { + std::uint16_t subtype_to_ignore{}; + get_number(input_format_t::cbor, subtype_to_ignore); + break; + } + case 0xDA: + { + std::uint32_t subtype_to_ignore{}; + get_number(input_format_t::cbor, subtype_to_ignore); + break; + } + case 0xDB: + { + std::uint64_t subtype_to_ignore{}; + get_number(input_format_t::cbor, subtype_to_ignore); + break; + } + default: + break; + } + return parse_cbor_internal(true, tag_handler); + } + + case cbor_tag_handler_t::store: + { + binary_t b; + // use binary subtype and store in binary container + switch (current) + { + case 0xD8: + { + std::uint8_t subtype{}; + get_number(input_format_t::cbor, subtype); + b.set_subtype(detail::conditional_static_cast(subtype)); + break; + } + case 0xD9: + { + std::uint16_t subtype{}; + get_number(input_format_t::cbor, subtype); + b.set_subtype(detail::conditional_static_cast(subtype)); + break; + } + case 0xDA: + { + std::uint32_t subtype{}; + get_number(input_format_t::cbor, subtype); + b.set_subtype(detail::conditional_static_cast(subtype)); + break; + } + case 0xDB: + { + std::uint64_t subtype{}; + get_number(input_format_t::cbor, subtype); + b.set_subtype(detail::conditional_static_cast(subtype)); + break; + } + default: + return parse_cbor_internal(true, tag_handler); + } + get(); + return get_cbor_binary(b) && sax->binary(b); + } + + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE + } + } case 0xF4: // false return sax->boolean(false); @@ -619,13 +830,13 @@ class binary_reader case 0xF9: // Half-Precision Float (two-byte IEEE 754) { - const int byte1_raw = get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number"))) + const auto byte1_raw = get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number"))) { return false; } - const int byte2_raw = get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number"))) + const auto byte2_raw = get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number"))) { return false; } @@ -646,8 +857,8 @@ class binary_reader { const int exp = (half >> 10u) & 0x1Fu; const unsigned int mant = half & 0x3FFu; - assert(0 <= exp and exp <= 32); - assert(mant <= 1024); + JSON_ASSERT(0 <= exp&& exp <= 32); + JSON_ASSERT(mant <= 1024); switch (exp) { case 0: @@ -667,20 +878,21 @@ class binary_reader case 0xFA: // Single-Precision Float (four-byte IEEE 754) { - float number; - return get_number(input_format_t::cbor, number) and sax->number_float(static_cast(number), ""); + float number{}; + return get_number(input_format_t::cbor, number) && sax->number_float(static_cast(number), ""); } case 0xFB: // Double-Precision Float (eight-byte IEEE 754) { - double number; - return get_number(input_format_t::cbor, number) and sax->number_float(static_cast(number), ""); + double number{}; + return get_number(input_format_t::cbor, number) && sax->number_float(static_cast(number), ""); } default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr)); } } } @@ -698,7 +910,7 @@ class binary_reader */ bool get_cbor_string(string_t& result) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string"))) { return false; } @@ -736,26 +948,26 @@ class binary_reader case 0x78: // UTF-8 string (one-byte uint8_t for n follows) { - std::uint8_t len; - return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + std::uint8_t len{}; + return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); } case 0x79: // UTF-8 string (two-byte uint16_t for n follow) { - std::uint16_t len; - return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + std::uint16_t len{}; + return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); } case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) { - std::uint32_t len; - return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + std::uint32_t len{}; + return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); } case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) { - std::uint64_t len; - return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result); + std::uint64_t len{}; + return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); } case 0x7F: // UTF-8 string (indefinite length) @@ -763,7 +975,7 @@ class binary_reader while (get() != 0xFF) { string_t chunk; - if (not get_cbor_string(chunk)) + if (!get_cbor_string(chunk)) { return false; } @@ -775,28 +987,131 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr)); + } + } + } + + /*! + @brief reads a CBOR byte array + + This function first reads starting bytes to determine the expected + byte array length and then copies this number of bytes into the byte array. + Additionally, CBOR's byte arrays with indefinite lengths are supported. + + @param[out] result created byte array + + @return whether byte array creation completed + */ + bool get_cbor_binary(binary_t& result) + { + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary"))) + { + return false; + } + + switch (current) + { + // Binary data (0x00..0x17 bytes follow) + case 0x40: + case 0x41: + case 0x42: + case 0x43: + case 0x44: + case 0x45: + case 0x46: + case 0x47: + case 0x48: + case 0x49: + case 0x4A: + case 0x4B: + case 0x4C: + case 0x4D: + case 0x4E: + case 0x4F: + case 0x50: + case 0x51: + case 0x52: + case 0x53: + case 0x54: + case 0x55: + case 0x56: + case 0x57: + { + return get_binary(input_format_t::cbor, static_cast(current) & 0x1Fu, result); + } + + case 0x58: // Binary data (one-byte uint8_t for n follows) + { + std::uint8_t len{}; + return get_number(input_format_t::cbor, len) && + get_binary(input_format_t::cbor, len, result); + } + + case 0x59: // Binary data (two-byte uint16_t for n follow) + { + std::uint16_t len{}; + return get_number(input_format_t::cbor, len) && + get_binary(input_format_t::cbor, len, result); + } + + case 0x5A: // Binary data (four-byte uint32_t for n follow) + { + std::uint32_t len{}; + return get_number(input_format_t::cbor, len) && + get_binary(input_format_t::cbor, len, result); + } + + case 0x5B: // Binary data (eight-byte uint64_t for n follow) + { + std::uint64_t len{}; + return get_number(input_format_t::cbor, len) && + get_binary(input_format_t::cbor, len, result); + } + + case 0x5F: // Binary data (indefinite length) + { + while (get() != 0xFF) + { + binary_t chunk; + if (!get_cbor_binary(chunk)) + { + return false; + } + result.insert(result.end(), chunk.begin(), chunk.end()); + } + return true; + } + + default: + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format_t::cbor, concat("expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x", last_token), "binary"), nullptr)); } } } /*! - @param[in] len the length of the array or std::size_t(-1) for an + @param[in] len the length of the array or static_cast(-1) for an array of indefinite size + @param[in] tag_handler how CBOR tags should be treated @return whether array creation completed */ - bool get_cbor_array(const std::size_t len) + bool get_cbor_array(const std::size_t len, + const cbor_tag_handler_t tag_handler) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len))) { return false; } - if (len != std::size_t(-1)) + if (len != static_cast(-1)) { for (std::size_t i = 0; i < len; ++i) { - if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) { return false; } @@ -806,7 +1121,7 @@ class binary_reader { while (get() != 0xFF) { - if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal(false))) + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler))) { return false; } @@ -817,49 +1132,54 @@ class binary_reader } /*! - @param[in] len the length of the object or std::size_t(-1) for an + @param[in] len the length of the object or static_cast(-1) for an object of indefinite size + @param[in] tag_handler how CBOR tags should be treated @return whether object creation completed */ - bool get_cbor_object(const std::size_t len) + bool get_cbor_object(const std::size_t len, + const cbor_tag_handler_t tag_handler) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len))) { return false; } - string_t key; - if (len != std::size_t(-1)) + if (len != 0) { - for (std::size_t i = 0; i < len; ++i) + string_t key; + if (len != static_cast(-1)) { - get(); - if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) + for (std::size_t i = 0; i < len; ++i) { - return false; - } + get(); + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal())) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } - } - else - { - while (get() != 0xFF) + else { - if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) + while (get() != 0xFF) { - return false; - } + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal())) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } } @@ -878,7 +1198,7 @@ class binary_reader switch (get()) { // EOF - case std::char_traits::eof(): + case std::char_traits::eof(): return unexpect_eof(input_format_t::msgpack, "value"); // positive fixint @@ -1029,7 +1349,7 @@ class binary_reader case 0x8D: case 0x8E: case 0x8F: - return get_msgpack_object(static_cast(static_cast(current) & 0x0Fu)); + return get_msgpack_object(conditional_static_cast(static_cast(current) & 0x0Fu)); // fixarray case 0x90: @@ -1048,7 +1368,7 @@ class binary_reader case 0x9D: case 0x9E: case 0x9F: - return get_msgpack_array(static_cast(static_cast(current) & 0x0Fu)); + return get_msgpack_array(conditional_static_cast(static_cast(current) & 0x0Fu)); // fixstr case 0xA0: @@ -1088,7 +1408,7 @@ class binary_reader case 0xDB: // str 32 { string_t s; - return get_msgpack_string(s) and sax->string(s); + return get_msgpack_string(s) && sax->string(s); } case 0xC0: // nil @@ -1100,88 +1420,104 @@ class binary_reader case 0xC3: // true return sax->boolean(true); + case 0xC4: // bin 8 + case 0xC5: // bin 16 + case 0xC6: // bin 32 + case 0xC7: // ext 8 + case 0xC8: // ext 16 + case 0xC9: // ext 32 + case 0xD4: // fixext 1 + case 0xD5: // fixext 2 + case 0xD6: // fixext 4 + case 0xD7: // fixext 8 + case 0xD8: // fixext 16 + { + binary_t b; + return get_msgpack_binary(b) && sax->binary(b); + } + case 0xCA: // float 32 { - float number; - return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast(number), ""); + float number{}; + return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast(number), ""); } case 0xCB: // float 64 { - double number; - return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast(number), ""); + double number{}; + return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast(number), ""); } case 0xCC: // uint 8 { - std::uint8_t number; - return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + std::uint8_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); } case 0xCD: // uint 16 { - std::uint16_t number; - return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + std::uint16_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); } case 0xCE: // uint 32 { - std::uint32_t number; - return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + std::uint32_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); } case 0xCF: // uint 64 { - std::uint64_t number; - return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number); + std::uint64_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); } case 0xD0: // int 8 { - std::int8_t number; - return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + std::int8_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_integer(number); } case 0xD1: // int 16 { - std::int16_t number; - return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + std::int16_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_integer(number); } case 0xD2: // int 32 { - std::int32_t number; - return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + std::int32_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_integer(number); } case 0xD3: // int 64 { - std::int64_t number; - return get_number(input_format_t::msgpack, number) and sax->number_integer(number); + std::int64_t number{}; + return get_number(input_format_t::msgpack, number) && sax->number_integer(number); } case 0xDC: // array 16 { - std::uint16_t len; - return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast(len)); + std::uint16_t len{}; + return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast(len)); } case 0xDD: // array 32 { - std::uint32_t len; - return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast(len)); + std::uint32_t len{}; + return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast(len)); } case 0xDE: // map 16 { - std::uint16_t len; - return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast(len)); + std::uint16_t len{}; + return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast(len)); } case 0xDF: // map 32 { - std::uint32_t len; - return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast(len)); + std::uint32_t len{}; + return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast(len)); } // negative fixint @@ -1222,7 +1558,8 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr)); } } } @@ -1239,7 +1576,7 @@ class binary_reader */ bool get_msgpack_string(string_t& result) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string"))) { return false; } @@ -1285,27 +1622,145 @@ class binary_reader case 0xD9: // str 8 { - std::uint8_t len; - return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + std::uint8_t len{}; + return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); } case 0xDA: // str 16 { - std::uint16_t len; - return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + std::uint16_t len{}; + return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); } case 0xDB: // str 32 { - std::uint32_t len; - return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result); + std::uint32_t len{}; + return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); } default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr)); + } + } + } + + /*! + @brief reads a MessagePack byte array + + This function first reads starting bytes to determine the expected + byte array length and then copies this number of bytes into a byte array. + + @param[out] result created byte array + + @return whether byte array creation completed + */ + bool get_msgpack_binary(binary_t& result) + { + // helper function to set the subtype + auto assign_and_return_true = [&result](std::int8_t subtype) + { + result.set_subtype(static_cast(subtype)); + return true; + }; + + switch (current) + { + case 0xC4: // bin 8 + { + std::uint8_t len{}; + return get_number(input_format_t::msgpack, len) && + get_binary(input_format_t::msgpack, len, result); + } + + case 0xC5: // bin 16 + { + std::uint16_t len{}; + return get_number(input_format_t::msgpack, len) && + get_binary(input_format_t::msgpack, len, result); + } + + case 0xC6: // bin 32 + { + std::uint32_t len{}; + return get_number(input_format_t::msgpack, len) && + get_binary(input_format_t::msgpack, len, result); + } + + case 0xC7: // ext 8 + { + std::uint8_t len{}; + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, len) && + get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, len, result) && + assign_and_return_true(subtype); + } + + case 0xC8: // ext 16 + { + std::uint16_t len{}; + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, len) && + get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, len, result) && + assign_and_return_true(subtype); + } + + case 0xC9: // ext 32 + { + std::uint32_t len{}; + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, len) && + get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, len, result) && + assign_and_return_true(subtype); + } + + case 0xD4: // fixext 1 + { + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, 1, result) && + assign_and_return_true(subtype); + } + + case 0xD5: // fixext 2 + { + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, 2, result) && + assign_and_return_true(subtype); + } + + case 0xD6: // fixext 4 + { + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, 4, result) && + assign_and_return_true(subtype); + } + + case 0xD7: // fixext 8 + { + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, 8, result) && + assign_and_return_true(subtype); + } + + case 0xD8: // fixext 16 + { + std::int8_t subtype{}; + return get_number(input_format_t::msgpack, subtype) && + get_binary(input_format_t::msgpack, 16, result) && + assign_and_return_true(subtype); } + + default: // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE } } @@ -1315,14 +1770,14 @@ class binary_reader */ bool get_msgpack_array(const std::size_t len) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len))) { return false; } for (std::size_t i = 0; i < len; ++i) { - if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal())) { return false; } @@ -1337,7 +1792,7 @@ class binary_reader */ bool get_msgpack_object(const std::size_t len) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len))) { return false; } @@ -1346,12 +1801,12 @@ class binary_reader for (std::size_t i = 0; i < len; ++i) { get(); - if (JSON_HEDLEY_UNLIKELY(not get_msgpack_string(key) or not sax->key(key))) + if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal())) { return false; } @@ -1398,7 +1853,7 @@ class binary_reader get(); // TODO(niels): may we ignore N here? } - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value"))) { return false; } @@ -1407,76 +1862,197 @@ class binary_reader { case 'U': { - std::uint8_t len; - return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + std::uint8_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } case 'i': { - std::int8_t len; - return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + std::int8_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } case 'I': { - std::int16_t len; - return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + std::int16_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } case 'l': { - std::int32_t len; - return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + std::int32_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } case 'L': { - std::int64_t len; - return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result); + std::int64_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } - default: - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); - } - } - - /*! - @param[out] result determined size - @return whether size determination completed - */ - bool get_ubjson_size_value(std::size_t& result) - { - switch (get_ignore_noop()) - { - case 'U': + case 'u': { - std::uint8_t number; - if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number))) + if (input_format != input_format_t::bjdata) { - return false; + break; } - result = static_cast(number); - return true; + std::uint16_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } - case 'i': + case 'm': { - std::int8_t number; - if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number))) + if (input_format != input_format_t::bjdata) { - return false; + break; } - result = static_cast(number); - return true; + std::uint32_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); } - case 'I': + case 'M': { - std::int16_t number; - if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number))) + if (input_format != input_format_t::bjdata) { - return false; + break; + } + std::uint64_t len{}; + return get_number(input_format, len) && get_string(input_format, len, result); + } + + default: + break; + } + auto last_token = get_token_string(); + std::string message; + + if (input_format != input_format_t::bjdata) + { + message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token; + } + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token; + } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr)); + } + + /*! + @param[out] dim an integer vector storing the ND array dimensions + @return whether reading ND array size vector is successful + */ + bool get_ubjson_ndarray_size(std::vector& dim) + { + std::pair size_and_type; + size_t dimlen = 0; + bool no_ndarray = true; + + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray))) + { + return false; + } + + if (size_and_type.first != npos) + { + if (size_and_type.second != 0) + { + if (size_and_type.second != 'N') + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second))) + { + return false; + } + dim.push_back(dimlen); + } + } + } + else + { + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray))) + { + return false; + } + dim.push_back(dimlen); + } + } + } + else + { + while (current != ']') + { + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current))) + { + return false; + } + dim.push_back(dimlen); + get_ignore_noop(); + } + } + return true; + } + + /*! + @param[out] result determined size + @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector + or ndarray dimension is not allowed; `false` means ndarray + is allowed; for output, `true` means an ndarray is found; + is_ndarray can only return `true` when its initial value + is `false` + @param[in] prefix type marker if already read, otherwise set to 0 + + @return whether size determination completed + */ + bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0) + { + if (prefix == 0) + { + prefix = get_ignore_noop(); + } + + switch (prefix) + { + case 'U': + { + std::uint8_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'i': + { + std::int8_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + if (number < 0) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, + exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr)); + } + result = static_cast(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char + return true; + } + + case 'I': + { + std::int16_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + if (number < 0) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, + exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr)); } result = static_cast(number); return true; @@ -1484,32 +2060,162 @@ class binary_reader case 'l': { - std::int32_t number; - if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number))) + std::int32_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) { return false; } + if (number < 0) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, + exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr)); + } result = static_cast(number); return true; } case 'L': { - std::int64_t number; - if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number))) + std::int64_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) { return false; } + if (number < 0) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, + exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr)); + } + if (!value_in_range_of(number)) + { + return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, + exception_message(input_format, "integer value overflow", "size"), nullptr)); + } result = static_cast(number); return true; } - default: + case 'u': { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint16_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + result = static_cast(number); + return true; + } + + case 'm': + { + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint32_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + result = conditional_static_cast(number); + return true; } + + case 'M': + { + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint64_t number{}; + if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number))) + { + return false; + } + if (!value_in_range_of(number)) + { + return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, + exception_message(input_format, "integer value overflow", "size"), nullptr)); + } + result = detail::conditional_static_cast(number); + return true; + } + + case '[': + { + if (input_format != input_format_t::bjdata) + { + break; + } + if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimentional vector is not allowed", "size"), nullptr)); + } + std::vector dim; + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim))) + { + return false; + } + if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector + { + result = dim.at(dim.size() - 1); + return true; + } + if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format + { + for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container + { + if ( i == 0 ) + { + result = 0; + return true; + } + } + + string_t key = "_ArraySize_"; + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size()))) + { + return false; + } + result = 1; + for (auto i : dim) + { + result *= i; + if (result == 0 || result == npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be npos as it is used to initialize size in get_ubjson_size_type() + { + return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, exception_message(input_format, "excessive ndarray size caused overflow", "size"), nullptr)); + } + if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast(i)))) + { + return false; + } + } + is_ndarray = true; + return sax->end_array(); + } + result = 0; + return true; + } + + default: + break; + } + auto last_token = get_token_string(); + std::string message; + + if (input_format != input_format_t::bjdata) + { + message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token; + } + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token; } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr)); } /*! @@ -1519,20 +2225,30 @@ class binary_reader for a more compact representation. @param[out] result pair of the size and the type + @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector @return whether pair creation completed */ - bool get_ubjson_size_type(std::pair& result) + bool get_ubjson_size_type(std::pair& result, bool inside_ndarray = false) { - result.first = string_t::npos; // size + result.first = npos; // size result.second = 0; // type + bool is_ndarray = false; get_ignore_noop(); if (current == '$') { result.second = get(); // must not ignore 'N', because 'N' maybe the type - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type"))) + if (input_format == input_format_t::bjdata + && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second))) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr)); + } + + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type"))) { return false; } @@ -1540,20 +2256,37 @@ class binary_reader get_ignore_noop(); if (JSON_HEDLEY_UNLIKELY(current != '#')) { - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value"))) { return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr)); } - return get_ubjson_size_value(result.first); + bool is_error = get_ubjson_size_value(result.first, is_ndarray); + if (input_format == input_format_t::bjdata && is_ndarray) + { + if (inside_ndarray) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read, + exception_message(input_format, "ndarray can not be recursive", "size"), nullptr)); + } + result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters + } + return is_error; } if (current == '#') { - return get_ubjson_size_value(result.first); + bool is_error = get_ubjson_size_value(result.first, is_ndarray); + if (input_format == input_format_t::bjdata && is_ndarray) + { + return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read, + exception_message(input_format, "ndarray requires both type and size", "size"), nullptr)); + } + return is_error; } return true; @@ -1563,12 +2296,12 @@ class binary_reader @param prefix the previously read or set type prefix @return whether value creation completed */ - bool get_ubjson_value(const int prefix) + bool get_ubjson_value(const char_int_type prefix) { switch (prefix) { - case std::char_traits::eof(): // EOF - return unexpect_eof(input_format_t::ubjson, "value"); + case std::char_traits::eof(): // EOF + return unexpect_eof(input_format, "value"); case 'T': // true return sax->boolean(true); @@ -1580,66 +2313,154 @@ class binary_reader case 'U': { - std::uint8_t number; - return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number); + std::uint8_t number{}; + return get_number(input_format, number) && sax->number_unsigned(number); } case 'i': { - std::int8_t number; - return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + std::int8_t number{}; + return get_number(input_format, number) && sax->number_integer(number); } case 'I': { - std::int16_t number; - return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + std::int16_t number{}; + return get_number(input_format, number) && sax->number_integer(number); } case 'l': { - std::int32_t number; - return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + std::int32_t number{}; + return get_number(input_format, number) && sax->number_integer(number); } case 'L': { - std::int64_t number; - return get_number(input_format_t::ubjson, number) and sax->number_integer(number); + std::int64_t number{}; + return get_number(input_format, number) && sax->number_integer(number); + } + + case 'u': + { + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint16_t number{}; + return get_number(input_format, number) && sax->number_unsigned(number); + } + + case 'm': + { + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint32_t number{}; + return get_number(input_format, number) && sax->number_unsigned(number); + } + + case 'M': + { + if (input_format != input_format_t::bjdata) + { + break; + } + std::uint64_t number{}; + return get_number(input_format, number) && sax->number_unsigned(number); + } + + case 'h': + { + if (input_format != input_format_t::bjdata) + { + break; + } + const auto byte1_raw = get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number"))) + { + return false; + } + const auto byte2_raw = get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number"))) + { + return false; + } + + const auto byte1 = static_cast(byte1_raw); + const auto byte2 = static_cast(byte2_raw); + + // code from RFC 7049, Appendix D, Figure 3: + // As half-precision floating-point numbers were only added + // to IEEE 754 in 2008, today's programming platforms often + // still only have limited support for them. It is very + // easy to include at least decoding support for them even + // without such support. An example of a small decoder for + // half-precision floating-point numbers in the C language + // is shown in Fig. 3. + const auto half = static_cast((byte2 << 8u) + byte1); + const double val = [&half] + { + const int exp = (half >> 10u) & 0x1Fu; + const unsigned int mant = half & 0x3FFu; + JSON_ASSERT(0 <= exp&& exp <= 32); + JSON_ASSERT(mant <= 1024); + switch (exp) + { + case 0: + return std::ldexp(mant, -24); + case 31: + return (mant == 0) + ? std::numeric_limits::infinity() + : std::numeric_limits::quiet_NaN(); + default: + return std::ldexp(mant + 1024, exp - 25); + } + }(); + return sax->number_float((half & 0x8000u) != 0 + ? static_cast(-val) + : static_cast(val), ""); } case 'd': { - float number; - return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast(number), ""); + float number{}; + return get_number(input_format, number) && sax->number_float(static_cast(number), ""); } case 'D': { - double number; - return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast(number), ""); + double number{}; + return get_number(input_format, number) && sax->number_float(static_cast(number), ""); + } + + case 'H': + { + return get_ubjson_high_precision_number(); } case 'C': // char { get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char"))) { return false; } if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr)); } - string_t s(1, static_cast(current)); + string_t s(1, static_cast(current)); return sax->string(s); } case 'S': // string { string_t s; - return get_ubjson_string(s) and sax->string(s); + return get_ubjson_string(s) && sax->string(s); } case '[': // array @@ -1649,11 +2470,10 @@ class binary_reader return get_ubjson_object(); default: // anything else - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); - } + break; } + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), nullptr)); } /*! @@ -1661,15 +2481,61 @@ class binary_reader */ bool get_ubjson_array() { - std::pair size_and_type; - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type))) + std::pair size_and_type; + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type))) { return false; } - if (size_and_type.first != string_t::npos) + // if bit-8 of size_and_type.second is set to 1, encode bjdata ndarray as an object in JData annotated array format (https://github.com/NeuroJSON/jdata): + // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]} + + if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(size_and_type.first))) + size_and_type.second &= ~(static_cast(1) << 8); // use bit 8 to indicate ndarray, here we remove the bit to restore the type marker + auto it = std::lower_bound(bjd_types_map.begin(), bjd_types_map.end(), size_and_type.second, [](const bjd_type & p, char_int_type t) + { + return p.first < t; + }); + string_t key = "_ArrayType_"; + if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second)) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr)); + } + + string_t type = it->second; // sax->string() takes a reference + if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type))) + { + return false; + } + + if (size_and_type.second == 'C') + { + size_and_type.second = 'U'; + } + + key = "_ArrayData_"; + if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) )) + { + return false; + } + + for (std::size_t i = 0; i < size_and_type.first; ++i) + { + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second))) + { + return false; + } + } + + return (sax->end_array() && sax->end_object()); + } + + if (size_and_type.first != npos) + { + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first))) { return false; } @@ -1680,7 +2546,7 @@ class binary_reader { for (std::size_t i = 0; i < size_and_type.first; ++i) { - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second))) + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second))) { return false; } @@ -1691,7 +2557,7 @@ class binary_reader { for (std::size_t i = 0; i < size_and_type.first; ++i) { - if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) { return false; } @@ -1700,14 +2566,14 @@ class binary_reader } else { - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast(-1)))) { return false; } while (current != ']') { - if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal(false))) + if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false))) { return false; } @@ -1723,16 +2589,24 @@ class binary_reader */ bool get_ubjson_object() { - std::pair size_and_type; - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type))) + std::pair size_and_type; + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type))) { return false; } + // do not accept ND-array size in objects in BJData + if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0) + { + auto last_token = get_token_string(); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, + exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr)); + } + string_t key; - if (size_and_type.first != string_t::npos) + if (size_and_type.first != npos) { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(size_and_type.first))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first))) { return false; } @@ -1741,11 +2615,11 @@ class binary_reader { for (std::size_t i = 0; i < size_and_type.first; ++i) { - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second))) + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second))) { return false; } @@ -1756,11 +2630,11 @@ class binary_reader { for (std::size_t i = 0; i < size_and_type.first; ++i) { - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) { return false; } @@ -1770,18 +2644,18 @@ class binary_reader } else { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast(-1)))) { return false; } while (current != '}') { - if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key))) + if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key))) { return false; } - if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal())) + if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) { return false; } @@ -1793,6 +2667,75 @@ class binary_reader return sax->end_object(); } + // Note, no reader for UBJSON binary types is implemented because they do + // not exist + + bool get_ubjson_high_precision_number() + { + // get size of following number string + std::size_t size{}; + bool no_ndarray = true; + auto res = get_ubjson_size_value(size, no_ndarray); + if (JSON_HEDLEY_UNLIKELY(!res)) + { + return res; + } + + // get number string + std::vector number_vector; + for (std::size_t i = 0; i < size; ++i) + { + get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number"))) + { + return false; + } + number_vector.push_back(static_cast(current)); + } + + // parse number string + using ia_type = decltype(detail::input_adapter(number_vector)); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); + const auto result_number = number_lexer.scan(); + const auto number_string = number_lexer.get_token_string(); + const auto result_remainder = number_lexer.scan(); + + using token_type = typename detail::lexer_base::token_type; + + if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) + { + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, + exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr)); + } + + switch (result_number) + { + case token_type::value_integer: + return sax->number_integer(number_lexer.get_number_integer()); + case token_type::value_unsigned: + return sax->number_unsigned(number_lexer.get_number_unsigned()); + case token_type::value_float: + return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); + case token_type::uninitialized: + case token_type::literal_true: + case token_type::literal_false: + case token_type::literal_null: + case token_type::value_string: + case token_type::begin_array: + case token_type::begin_object: + case token_type::end_array: + case token_type::end_object: + case token_type::name_separator: + case token_type::value_separator: + case token_type::parse_error: + case token_type::end_of_input: + case token_type::literal_or_value: + default: + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, + exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr)); + } + } + /////////////////////// // Utility functions // /////////////////////// @@ -1802,20 +2745,20 @@ class binary_reader This function provides the interface to the used input adapter. It does not throw in case the input reached EOF, but returns a -'ve valued - `std::char_traits::eof()` in that case. + `std::char_traits::eof()` in that case. @return character read from the input */ - int get() + char_int_type get() { ++chars_read; - return current = ia->get_character(); + return current = ia.get_character(); } /*! @return character read from the input after ignoring all 'N' entries */ - int get_ignore_noop() + char_int_type get_ignore_noop() { do { @@ -1835,25 +2778,27 @@ class binary_reader @return whether conversion completed - @note This function needs to respect the system's endianess, because + @note This function needs to respect the system's endianness, because bytes in CBOR, MessagePack, and UBJSON are stored in network order (big endian) and therefore need reordering on little endian systems. + On the other hand, BSON and BJData use little endian and should reorder + on big endian systems. */ template bool get_number(const input_format_t format, NumberType& result) { // step 1: read input into array with system's byte order - std::array vec; + std::array vec{}; for (std::size_t i = 0; i < sizeof(NumberType); ++i) { get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "number"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number"))) { return false; } // reverse byte order prior to conversion if necessary - if (is_little_endian != InputIsLittleEndian) + if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata)) { vec[sizeof(NumberType) - i - 1] = static_cast(current); } @@ -1888,15 +2833,49 @@ class binary_reader string_t& result) { bool success = true; - std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + for (NumberType i = 0; i < len; i++) + { + get(); + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) + { + success = false; + break; + } + result.push_back(static_cast(current)); + } + return success; + } + + /*! + @brief create a byte array by reading bytes from the input + + @tparam NumberType the type of the number + @param[in] format the current format (for diagnostics) + @param[in] len number of bytes to read + @param[out] result byte array created by reading @a len bytes + + @return whether byte array creation completed + + @note We can not reserve @a len bytes for the result, because @a len + may be too large. Usually, @ref unexpect_eof() detects the end of + the input before we run out of memory. + */ + template + bool get_binary(const input_format_t format, + const NumberType len, + binary_t& result) + { + bool success = true; + for (NumberType i = 0; i < len; i++) { get(); - if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "string"))) + if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) { success = false; + break; } - return static_cast(current); - }); + result.push_back(static_cast(current)); + } return success; } @@ -1908,10 +2887,10 @@ class binary_reader JSON_HEDLEY_NON_NULL(3) bool unexpect_eof(const input_format_t format, const char* context) const { - if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) + if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr)); } return true; } @@ -1922,7 +2901,7 @@ class binary_reader std::string get_token_string() const { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); + static_cast((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) return std::string{cr.data()}; } @@ -1956,28 +2935,76 @@ class binary_reader error_msg += "BSON"; break; + case input_format_t::bjdata: + error_msg += "BJData"; + break; + + case input_format_t::json: // LCOV_EXCL_LINE default: // LCOV_EXCL_LINE - assert(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } - return error_msg + " " + context + ": " + detail; + return concat(error_msg, ' ', context, ": ", detail); } private: + static JSON_INLINE_VARIABLE constexpr std::size_t npos = static_cast(-1); + /// input adapter - input_adapter_t ia = nullptr; + InputAdapterType ia; /// the current character - int current = std::char_traits::eof(); + char_int_type current = std::char_traits::eof(); /// the number of characters read std::size_t chars_read = 0; - /// whether we can assume little endianess - const bool is_little_endian = little_endianess(); + /// whether we can assume little endianness + const bool is_little_endian = little_endianness(); + + /// input format + const input_format_t input_format = input_format_t::json; /// the SAX parser json_sax_t* sax = nullptr; + + // excluded markers in bjdata optimized type +#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \ + make_array('F', 'H', 'N', 'S', 'T', 'Z', '[', '{') + +#define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \ + make_array( \ + bjd_type{'C', "char"}, \ + bjd_type{'D', "double"}, \ + bjd_type{'I', "int16"}, \ + bjd_type{'L', "int64"}, \ + bjd_type{'M', "uint64"}, \ + bjd_type{'U', "uint8"}, \ + bjd_type{'d', "single"}, \ + bjd_type{'i', "int8"}, \ + bjd_type{'l', "int32"}, \ + bjd_type{'m', "uint32"}, \ + bjd_type{'u', "uint16"}) + + JSON_PRIVATE_UNLESS_TESTED: + // lookup tables + // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes) + const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers = + JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_; + + using bjd_type = std::pair; + // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes) + const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map = + JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_; + +#undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ +#undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ }; + +#ifndef JSON_HAS_CPP_17 + template + constexpr std::size_t binary_reader::npos; +#endif + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/input_adapters.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/input_adapters.hpp index 9512a771..cf53b1d5 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/input_adapters.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/input_adapters.hpp @@ -1,11 +1,16 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // array -#include // assert #include // size_t -#include //FILE * #include // strlen -#include // istream #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -13,61 +18,50 @@ #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#ifndef JSON_NO_IO + #include // FILE * + #include // istream +#endif // JSON_NO_IO + #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + /// the supported input formats -enum class input_format_t { json, cbor, msgpack, ubjson, bson }; +enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata }; //////////////////// // input adapters // //////////////////// -/*! -@brief abstract input adapter interface - -Produces a stream of std::char_traits::int_type characters from a -std::istream, a buffer, or some other input type. Accepts the return of -exactly one non-EOF character for future input. The int_type characters -returned consist of all valid char values as positive values (typically -unsigned char), plus an EOF value outside that range, specified by the value -of the function std::char_traits::eof(). This value is typically -1, but -could be any arbitrary value which is not a valid char value. -*/ -struct input_adapter_protocol -{ - /// get a character [0,255] or std::char_traits::eof(). - virtual std::char_traits::int_type get_character() = 0; - virtual ~input_adapter_protocol() = default; -}; - -/// a type to simplify interfaces -using input_adapter_t = std::shared_ptr; - +#ifndef JSON_NO_IO /*! Input adapter for stdio file access. This adapter read only 1 byte and do not use any buffer. This adapter is a very low level adapter. */ -class file_input_adapter : public input_adapter_protocol +class file_input_adapter { public: + using char_type = char; + JSON_HEDLEY_NON_NULL(2) - explicit file_input_adapter(std::FILE* f) noexcept + explicit file_input_adapter(std::FILE* f) noexcept : m_file(f) - {} + { + JSON_ASSERT(m_file != nullptr); + } // make class move-only file_input_adapter(const file_input_adapter&) = delete; - file_input_adapter(file_input_adapter&&) = default; + file_input_adapter(file_input_adapter&&) noexcept = default; file_input_adapter& operator=(const file_input_adapter&) = delete; - file_input_adapter& operator=(file_input_adapter&&) = default; - ~file_input_adapter() override = default; + file_input_adapter& operator=(file_input_adapter&&) = delete; + ~file_input_adapter() = default; - std::char_traits::int_type get_character() noexcept override + std::char_traits::int_type get_character() noexcept { return std::fgetc(m_file); } @@ -87,92 +81,111 @@ characters following those used in parsing the JSON input. Clears the std::istream flags; any input errors (e.g., EOF) will be detected by the first subsequent call for input from the std::istream. */ -class input_stream_adapter : public input_adapter_protocol +class input_stream_adapter { public: - ~input_stream_adapter() override + using char_type = char; + + ~input_stream_adapter() { // clear stream flags; we use underlying streambuf I/O, do not // maintain ifstream flags, except eof - is.clear(is.rdstate() & std::ios::eofbit); + if (is != nullptr) + { + is->clear(is->rdstate() & std::ios::eofbit); + } } explicit input_stream_adapter(std::istream& i) - : is(i), sb(*i.rdbuf()) + : is(&i), sb(i.rdbuf()) {} // delete because of pointer members input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete; - input_stream_adapter(input_stream_adapter&&) = delete; input_stream_adapter& operator=(input_stream_adapter&&) = delete; + input_stream_adapter(input_stream_adapter&& rhs) noexcept + : is(rhs.is), sb(rhs.sb) + { + rhs.is = nullptr; + rhs.sb = nullptr; + } + // std::istream/std::streambuf use std::char_traits::to_int_type, to // ensure that std::char_traits::eof() and the character 0xFF do not - // end up as the same value, eg. 0xFFFFFFFF. - std::char_traits::int_type get_character() override + // end up as the same value, e.g. 0xFFFFFFFF. + std::char_traits::int_type get_character() { - auto res = sb.sbumpc(); + auto res = sb->sbumpc(); // set eof manually, as we don't use the istream interface. - if (res == EOF) + if (JSON_HEDLEY_UNLIKELY(res == std::char_traits::eof())) { - is.clear(is.rdstate() | std::ios::eofbit); + is->clear(is->rdstate() | std::ios::eofbit); } return res; } private: /// the associated input stream - std::istream& is; - std::streambuf& sb; + std::istream* is = nullptr; + std::streambuf* sb = nullptr; }; +#endif // JSON_NO_IO -/// input adapter for buffer input -class input_buffer_adapter : public input_adapter_protocol +// General-purpose iterator-based adapter. It might not be as fast as +// theoretically possible for some containers, but it is extremely versatile. +template +class iterator_input_adapter { public: - input_buffer_adapter(const char* b, const std::size_t l) noexcept - : cursor(b), limit(b == nullptr ? nullptr : (b + l)) - {} + using char_type = typename std::iterator_traits::value_type; - // delete because of pointer members - input_buffer_adapter(const input_buffer_adapter&) = delete; - input_buffer_adapter& operator=(input_buffer_adapter&) = delete; - input_buffer_adapter(input_buffer_adapter&&) = delete; - input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; - ~input_buffer_adapter() override = default; + iterator_input_adapter(IteratorType first, IteratorType last) + : current(std::move(first)), end(std::move(last)) + {} - std::char_traits::int_type get_character() noexcept override + typename std::char_traits::int_type get_character() { - if (JSON_HEDLEY_LIKELY(cursor < limit)) + if (JSON_HEDLEY_LIKELY(current != end)) { - assert(cursor != nullptr and limit != nullptr); - return std::char_traits::to_int_type(*(cursor++)); + auto result = std::char_traits::to_int_type(*current); + std::advance(current, 1); + return result; } - return std::char_traits::eof(); + return std::char_traits::eof(); } private: - /// pointer to the current character - const char* cursor; - /// pointer past the last character - const char* const limit; + IteratorType current; + IteratorType end; + + template + friend struct wide_string_input_helper; + + bool empty() const + { + return current == end; + } }; -template -struct wide_string_input_helper + +template +struct wide_string_input_helper; + +template +struct wide_string_input_helper { // UTF-32 - static void fill_buffer(const WideStringType& str, - size_t& current_wchar, + static void fill_buffer(BaseInputAdapter& input, std::array::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled) { utf8_bytes_index = 0; - if (current_wchar == str.size()) + if (JSON_HEDLEY_UNLIKELY(input.empty())) { utf8_bytes[0] = std::char_traits::eof(); utf8_bytes_filled = 1; @@ -180,7 +193,7 @@ struct wide_string_input_helper else { // get the current character - const auto wc = static_cast(str[current_wchar++]); + const auto wc = input.get_character(); // UTF-32 to UTF-8 encoding if (wc < 0x80) @@ -190,23 +203,23 @@ struct wide_string_input_helper } else if (wc <= 0x7FF) { - utf8_bytes[0] = static_cast::int_type>(0xC0u | ((wc >> 6u) & 0x1Fu)); - utf8_bytes[1] = static_cast::int_type>(0x80u | (wc & 0x3Fu)); + utf8_bytes[0] = static_cast::int_type>(0xC0u | ((static_cast(wc) >> 6u) & 0x1Fu)); + utf8_bytes[1] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); utf8_bytes_filled = 2; } else if (wc <= 0xFFFF) { - utf8_bytes[0] = static_cast::int_type>(0xE0u | ((wc >> 12u) & 0x0Fu)); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((wc >> 6u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | (wc & 0x3Fu)); + utf8_bytes[0] = static_cast::int_type>(0xE0u | ((static_cast(wc) >> 12u) & 0x0Fu)); + utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); + utf8_bytes[2] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); utf8_bytes_filled = 3; } else if (wc <= 0x10FFFF) { - utf8_bytes[0] = static_cast::int_type>(0xF0u | ((wc >> 18u) & 0x07u)); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((wc >> 12u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | ((wc >> 6u) & 0x3Fu)); - utf8_bytes[3] = static_cast::int_type>(0x80u | (wc & 0x3Fu)); + utf8_bytes[0] = static_cast::int_type>(0xF0u | ((static_cast(wc) >> 18u) & 0x07u)); + utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 12u) & 0x3Fu)); + utf8_bytes[2] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); + utf8_bytes[3] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); utf8_bytes_filled = 4; } else @@ -219,19 +232,18 @@ struct wide_string_input_helper } }; -template -struct wide_string_input_helper +template +struct wide_string_input_helper { // UTF-16 - static void fill_buffer(const WideStringType& str, - size_t& current_wchar, + static void fill_buffer(BaseInputAdapter& input, std::array::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled) { utf8_bytes_index = 0; - if (current_wchar == str.size()) + if (JSON_HEDLEY_UNLIKELY(input.empty())) { utf8_bytes[0] = std::char_traits::eof(); utf8_bytes_filled = 1; @@ -239,7 +251,7 @@ struct wide_string_input_helper else { // get the current character - const auto wc = static_cast(str[current_wchar++]); + const auto wc = input.get_character(); // UTF-16 to UTF-8 encoding if (wc < 0x80) @@ -249,23 +261,23 @@ struct wide_string_input_helper } else if (wc <= 0x7FF) { - utf8_bytes[0] = static_cast::int_type>(0xC0u | ((wc >> 6u))); - utf8_bytes[1] = static_cast::int_type>(0x80u | (wc & 0x3Fu)); + utf8_bytes[0] = static_cast::int_type>(0xC0u | ((static_cast(wc) >> 6u))); + utf8_bytes[1] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); utf8_bytes_filled = 2; } - else if (0xD800 > wc or wc >= 0xE000) + else if (0xD800 > wc || wc >= 0xE000) { - utf8_bytes[0] = static_cast::int_type>(0xE0u | ((wc >> 12u))); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((wc >> 6u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | (wc & 0x3Fu)); + utf8_bytes[0] = static_cast::int_type>(0xE0u | ((static_cast(wc) >> 12u))); + utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); + utf8_bytes[2] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); utf8_bytes_filled = 3; } else { - if (current_wchar < str.size()) + if (JSON_HEDLEY_UNLIKELY(!input.empty())) { - const auto wc2 = static_cast(str[current_wchar++]); - const auto charcode = 0x10000u + (((wc & 0x3FFu) << 10u) | (wc2 & 0x3FFu)); + const auto wc2 = static_cast(input.get_character()); + const auto charcode = 0x10000u + (((static_cast(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu)); utf8_bytes[0] = static_cast::int_type>(0xF0u | (charcode >> 18u)); utf8_bytes[1] = static_cast::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu)); utf8_bytes[2] = static_cast::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu)); @@ -274,8 +286,6 @@ struct wide_string_input_helper } else { - // unknown character - ++current_wchar; utf8_bytes[0] = static_cast::int_type>(wc); utf8_bytes_filled = 1; } @@ -284,44 +294,42 @@ struct wide_string_input_helper } }; -template -class wide_string_input_adapter : public input_adapter_protocol +// Wraps another input apdater to convert wide character types into individual bytes. +template +class wide_string_input_adapter { public: - explicit wide_string_input_adapter(const WideStringType& w) noexcept - : str(w) - {} + using char_type = char; - std::char_traits::int_type get_character() noexcept override + wide_string_input_adapter(BaseInputAdapter base) + : base_adapter(base) {} + + typename std::char_traits::int_type get_character() noexcept { // check if buffer needs to be filled if (utf8_bytes_index == utf8_bytes_filled) { - fill_buffer(); + fill_buffer(); - assert(utf8_bytes_filled > 0); - assert(utf8_bytes_index == 0); + JSON_ASSERT(utf8_bytes_filled > 0); + JSON_ASSERT(utf8_bytes_index == 0); } // use buffer - assert(utf8_bytes_filled > 0); - assert(utf8_bytes_index < utf8_bytes_filled); + JSON_ASSERT(utf8_bytes_filled > 0); + JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled); return utf8_bytes[utf8_bytes_index++]; } private: + BaseInputAdapter base_adapter; + template void fill_buffer() { - wide_string_input_helper::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled); + wide_string_input_helper::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled); } - /// the wstring to process - const WideStringType& str; - - /// index of the current wchar in str - std::size_t current_wchar = 0; - /// a buffer for UTF-8 bytes std::array::int_type, 4> utf8_bytes = {{0, 0, 0, 0}}; @@ -331,112 +339,156 @@ class wide_string_input_adapter : public input_adapter_protocol std::size_t utf8_bytes_filled = 0; }; -class input_adapter + +template +struct iterator_input_adapter_factory { - public: - // native support - JSON_HEDLEY_NON_NULL(2) - input_adapter(std::FILE* file) - : ia(std::make_shared(file)) {} - /// input adapter for input stream - input_adapter(std::istream& i) - : ia(std::make_shared(i)) {} + using iterator_type = IteratorType; + using char_type = typename std::iterator_traits::value_type; + using adapter_type = iterator_input_adapter; + + static adapter_type create(IteratorType first, IteratorType last) + { + return adapter_type(std::move(first), std::move(last)); + } +}; + +template +struct is_iterator_of_multibyte +{ + using value_type = typename std::iterator_traits::value_type; + enum + { + value = sizeof(value_type) > 1 + }; +}; - /// input adapter for input stream - input_adapter(std::istream&& i) - : ia(std::make_shared(i)) {} +template +struct iterator_input_adapter_factory::value>> +{ + using iterator_type = IteratorType; + using char_type = typename std::iterator_traits::value_type; + using base_adapter_type = iterator_input_adapter; + using adapter_type = wide_string_input_adapter; - input_adapter(const std::wstring& ws) - : ia(std::make_shared>(ws)) {} + static adapter_type create(IteratorType first, IteratorType last) + { + return adapter_type(base_adapter_type(std::move(first), std::move(last))); + } +}; - input_adapter(const std::u16string& ws) - : ia(std::make_shared>(ws)) {} +// General purpose iterator-based input +template +typename iterator_input_adapter_factory::adapter_type input_adapter(IteratorType first, IteratorType last) +{ + using factory_type = iterator_input_adapter_factory; + return factory_type::create(first, last); +} - input_adapter(const std::u32string& ws) - : ia(std::make_shared>(ws)) {} +// Convenience shorthand from container to iterator +// Enables ADL on begin(container) and end(container) +// Encloses the using declarations in namespace for not to leak them to outside scope - /// input adapter for buffer - template::value and - std::is_integral::type>::value and - sizeof(typename std::remove_pointer::type) == 1, - int>::type = 0> - input_adapter(CharT b, std::size_t l) - : ia(std::make_shared(reinterpret_cast(b), l)) {} +namespace container_input_adapter_factory_impl +{ - // derived support +using std::begin; +using std::end; - /// input adapter for string literal - template::value and - std::is_integral::type>::value and - sizeof(typename std::remove_pointer::type) == 1, - int>::type = 0> - input_adapter(CharT b) - : input_adapter(reinterpret_cast(b), - std::strlen(reinterpret_cast(b))) {} +template +struct container_input_adapter_factory {}; + +template +struct container_input_adapter_factory< ContainerType, + void_t()), end(std::declval()))>> + { + using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); + + static adapter_type create(const ContainerType& container) +{ + return input_adapter(begin(container), end(container)); +} + }; + +} // namespace container_input_adapter_factory_impl + +template +typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) +{ + return container_input_adapter_factory_impl::container_input_adapter_factory::create(container); +} + +#ifndef JSON_NO_IO +// Special cases with fast paths +inline file_input_adapter input_adapter(std::FILE* file) +{ + return file_input_adapter(file); +} + +inline input_stream_adapter input_adapter(std::istream& stream) +{ + return input_stream_adapter(stream); +} + +inline input_stream_adapter input_adapter(std::istream&& stream) +{ + return input_stream_adapter(stream); +} +#endif // JSON_NO_IO + +using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); + +// Null-delimited strings, and the like. +template < typename CharT, + typename std::enable_if < + std::is_pointer::value&& + !std::is_array::value&& + std::is_integral::type>::value&& + sizeof(typename std::remove_pointer::type) == 1, + int >::type = 0 > +contiguous_bytes_input_adapter input_adapter(CharT b) +{ + auto length = std::strlen(reinterpret_cast(b)); + const auto* ptr = reinterpret_cast(b); + return input_adapter(ptr, ptr + length); +} + +template +auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) +{ + return input_adapter(array, array + N); +} + +// This class only handles inputs of input_buffer_adapter type. +// It's required so that expressions like {ptr, len} can be implicitly cast +// to the correct adapter. +class span_input_adapter +{ + public: + template < typename CharT, + typename std::enable_if < + std::is_pointer::value&& + std::is_integral::type>::value&& + sizeof(typename std::remove_pointer::type) == 1, + int >::type = 0 > + span_input_adapter(CharT b, std::size_t l) + : ia(reinterpret_cast(b), reinterpret_cast(b) + l) {} - /// input adapter for iterator range with contiguous storage template::iterator_category, std::random_access_iterator_tag>::value, int>::type = 0> - input_adapter(IteratorType first, IteratorType last) - { -#ifndef NDEBUG - // assertion to check that the iterator range is indeed contiguous, - // see http://stackoverflow.com/a/35008842/266378 for more discussion - const auto is_contiguous = std::accumulate( - first, last, std::pair(true, 0), - [&first](std::pair res, decltype(*first) val) - { - res.first &= (val == *(std::next(std::addressof(*first), res.second++))); - return res; - }).first; - assert(is_contiguous); -#endif - - // assertion to check that each element is 1 byte long - static_assert( - sizeof(typename iterator_traits::value_type) == 1, - "each element in the iterator range must have the size of 1 byte"); - - const auto len = static_cast(std::distance(first, last)); - if (JSON_HEDLEY_LIKELY(len > 0)) - { - // there is at least one element: use the address of first - ia = std::make_shared(reinterpret_cast(&(*first)), len); - } - else - { - // the address of first cannot be used: use nullptr - ia = std::make_shared(nullptr, len); - } - } - - /// input adapter for array - template - input_adapter(T (&array)[N]) - : input_adapter(std::begin(array), std::end(array)) {} + span_input_adapter(IteratorType first, IteratorType last) + : ia(input_adapter(first, last)) {} - /// input adapter for contiguous container - template::value and - std::is_base_of()))>::iterator_category>::value, - int>::type = 0> - input_adapter(const ContiguousContainer& c) - : input_adapter(std::begin(c), std::end(c)) {} - - operator input_adapter_t() + contiguous_bytes_input_adapter&& get() { - return ia; + return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) } private: - /// the actual adapter - input_adapter_t ia = nullptr; + contiguous_bytes_input_adapter ia; }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/json_sax.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/json_sax.hpp index 606b7862..5bd5c51c 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/json_sax.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/json_sax.hpp @@ -1,6 +1,13 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once -#include // assert #include #include // string #include // move @@ -8,9 +15,9 @@ #include #include +#include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN /*! @brief SAX interface @@ -23,14 +30,11 @@ input. template struct json_sax { - /// type for (signed) integers using number_integer_t = typename BasicJsonType::number_integer_t; - /// type for unsigned integers using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - /// type for floating-point numbers using number_float_t = typename BasicJsonType::number_float_t; - /// type for strings using string_t = typename BasicJsonType::string_t; + using binary_t = typename BasicJsonType::binary_t; /*! @brief a null value was read @@ -60,7 +64,7 @@ struct json_sax virtual bool number_unsigned(number_unsigned_t val) = 0; /*! - @brief an floating-point number was read + @brief a floating-point number was read @param[in] val floating-point value @param[in] s raw token value @return whether parsing should proceed @@ -68,13 +72,21 @@ struct json_sax virtual bool number_float(number_float_t val, const string_t& s) = 0; /*! - @brief a string was read + @brief a string value was read @param[in] val string value @return whether parsing should proceed - @note It is safe to move the passed string. + @note It is safe to move the passed string value. */ virtual bool string(string_t& val) = 0; + /*! + @brief a binary value was read + @param[in] val binary value + @return whether parsing should proceed + @note It is safe to move the passed binary value. + */ + virtual bool binary(binary_t& val) = 0; + /*! @brief the beginning of an object was read @param[in] elements number of object elements or -1 if unknown @@ -122,6 +134,11 @@ struct json_sax const std::string& last_token, const detail::exception& ex) = 0; + json_sax() = default; + json_sax(const json_sax&) = default; + json_sax(json_sax&&) noexcept = default; + json_sax& operator=(const json_sax&) = default; + json_sax& operator=(json_sax&&) noexcept = default; virtual ~json_sax() = default; }; @@ -149,9 +166,10 @@ class json_sax_dom_parser using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; + using binary_t = typename BasicJsonType::binary_t; /*! - @param[in, out] r reference to a JSON value that is manipulated while + @param[in,out] r reference to a JSON value that is manipulated while parsing @param[in] allow_exceptions_ whether parse errors yield exceptions */ @@ -161,9 +179,9 @@ class json_sax_dom_parser // make class move-only json_sax_dom_parser(const json_sax_dom_parser&) = delete; - json_sax_dom_parser(json_sax_dom_parser&&) = default; + json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete; - json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; + json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_parser() = default; bool null() @@ -202,14 +220,19 @@ class json_sax_dom_parser return true; } + bool binary(binary_t& val) + { + handle_value(std::move(val)); + return true; + } + bool start_object(std::size_t len) { ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); - if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + if (JSON_HEDLEY_UNLIKELY(len != static_cast(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back())); } return true; @@ -217,6 +240,9 @@ class json_sax_dom_parser bool key(string_t& val) { + JSON_ASSERT(!ref_stack.empty()); + JSON_ASSERT(ref_stack.back()->is_object()); + // add null at given key and store the reference for later object_element = &(ref_stack.back()->m_value.object->operator[](val)); return true; @@ -224,6 +250,10 @@ class json_sax_dom_parser bool end_object() { + JSON_ASSERT(!ref_stack.empty()); + JSON_ASSERT(ref_stack.back()->is_object()); + + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -232,10 +262,9 @@ class json_sax_dom_parser { ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); - if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + if (JSON_HEDLEY_UNLIKELY(len != static_cast(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back())); } return true; @@ -243,34 +272,23 @@ class json_sax_dom_parser bool end_array() { + JSON_ASSERT(!ref_stack.empty()); + JSON_ASSERT(ref_stack.back()->is_array()); + + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } + template bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const detail::exception& ex) + const Exception& ex) { errored = true; + static_cast(ex); if (allow_exceptions) { - // determine the proper exception type from the id - switch ((ex.id / 100) % 100) - { - case 1: - JSON_THROW(*static_cast(&ex)); - case 4: - JSON_THROW(*static_cast(&ex)); - // LCOV_EXCL_START - case 2: - JSON_THROW(*static_cast(&ex)); - case 3: - JSON_THROW(*static_cast(&ex)); - case 5: - JSON_THROW(*static_cast(&ex)); - default: - assert(false); - // LCOV_EXCL_STOP - } + JSON_THROW(ex); } return false; } @@ -297,7 +315,7 @@ class json_sax_dom_parser return &root; } - assert(ref_stack.back()->is_array() or ref_stack.back()->is_object()); + JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object()); if (ref_stack.back()->is_array()) { @@ -305,8 +323,8 @@ class json_sax_dom_parser return &(ref_stack.back()->m_value.array->back()); } - assert(ref_stack.back()->is_object()); - assert(object_element); + JSON_ASSERT(ref_stack.back()->is_object()); + JSON_ASSERT(object_element); *object_element = BasicJsonType(std::forward(v)); return object_element; } @@ -331,6 +349,7 @@ class json_sax_dom_callback_parser using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; + using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; @@ -344,9 +363,9 @@ class json_sax_dom_callback_parser // make class move-only json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_callback_parser() = default; bool null() @@ -385,6 +404,12 @@ class json_sax_dom_callback_parser return true; } + bool binary(binary_t& val) + { + handle_value(std::move(val)); + return true; + } + bool start_object(std::size_t len) { // check callback for object start @@ -395,9 +420,9 @@ class json_sax_dom_callback_parser ref_stack.push_back(val.second); // check object limit - if (ref_stack.back() and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back())); } return true; @@ -412,7 +437,7 @@ class json_sax_dom_callback_parser key_keep_stack.push_back(keep); // add discarded value at given key and store the reference for later - if (keep and ref_stack.back()) + if (keep && ref_stack.back()) { object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded); } @@ -422,18 +447,25 @@ class json_sax_dom_callback_parser bool end_object() { - if (ref_stack.back() and not callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + if (ref_stack.back()) { - // discard object - *ref_stack.back() = discarded; + if (!callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + { + // discard object + *ref_stack.back() = discarded; + } + else + { + ref_stack.back()->set_parents(); + } } - assert(not ref_stack.empty()); - assert(not keep_stack.empty()); + JSON_ASSERT(!ref_stack.empty()); + JSON_ASSERT(!keep_stack.empty()); ref_stack.pop_back(); keep_stack.pop_back(); - if (not ref_stack.empty() and ref_stack.back() and ref_stack.back()->is_object()) + if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured()) { // remove discarded value for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it) @@ -458,9 +490,9 @@ class json_sax_dom_callback_parser ref_stack.push_back(val.second); // check array limit - if (ref_stack.back() and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size())) + if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back())); } return true; @@ -473,20 +505,24 @@ class json_sax_dom_callback_parser if (ref_stack.back()) { keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); - if (not keep) + if (keep) + { + ref_stack.back()->set_parents(); + } + else { // discard array *ref_stack.back() = discarded; } } - assert(not ref_stack.empty()); - assert(not keep_stack.empty()); + JSON_ASSERT(!ref_stack.empty()); + JSON_ASSERT(!keep_stack.empty()); ref_stack.pop_back(); keep_stack.pop_back(); // remove discarded value - if (not keep and not ref_stack.empty() and ref_stack.back()->is_array()) + if (!keep && !ref_stack.empty() && ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->pop_back(); } @@ -494,30 +530,15 @@ class json_sax_dom_callback_parser return true; } + template bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const detail::exception& ex) + const Exception& ex) { errored = true; + static_cast(ex); if (allow_exceptions) { - // determine the proper exception type from the id - switch ((ex.id / 100) % 100) - { - case 1: - JSON_THROW(*static_cast(&ex)); - case 4: - JSON_THROW(*static_cast(&ex)); - // LCOV_EXCL_START - case 2: - JSON_THROW(*static_cast(&ex)); - case 3: - JSON_THROW(*static_cast(&ex)); - case 5: - JSON_THROW(*static_cast(&ex)); - default: - assert(false); - // LCOV_EXCL_STOP - } + JSON_THROW(ex); } return false; } @@ -546,11 +567,11 @@ class json_sax_dom_callback_parser template std::pair handle_value(Value&& v, const bool skip_callback = false) { - assert(not keep_stack.empty()); + JSON_ASSERT(!keep_stack.empty()); // do not handle this value if we know it would be added to a discarded // container - if (not keep_stack.back()) + if (!keep_stack.back()) { return {false, nullptr}; } @@ -559,10 +580,10 @@ class json_sax_dom_callback_parser auto value = BasicJsonType(std::forward(v)); // check callback - const bool keep = skip_callback or callback(static_cast(ref_stack.size()), parse_event_t::value, value); + const bool keep = skip_callback || callback(static_cast(ref_stack.size()), parse_event_t::value, value); // do not handle this value if we just learnt it shall be discarded - if (not keep) + if (!keep) { return {false, nullptr}; } @@ -575,34 +596,34 @@ class json_sax_dom_callback_parser // skip this value if we already decided to skip the parent // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360) - if (not ref_stack.back()) + if (!ref_stack.back()) { return {false, nullptr}; } // we now only expect arrays and objects - assert(ref_stack.back()->is_array() or ref_stack.back()->is_object()); + JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object()); // array if (ref_stack.back()->is_array()) { - ref_stack.back()->m_value.array->push_back(std::move(value)); + ref_stack.back()->m_value.array->emplace_back(std::move(value)); return {true, &(ref_stack.back()->m_value.array->back())}; } // object - assert(ref_stack.back()->is_object()); + JSON_ASSERT(ref_stack.back()->is_object()); // check if we should store an element for the current key - assert(not key_keep_stack.empty()); + JSON_ASSERT(!key_keep_stack.empty()); const bool store_element = key_keep_stack.back(); key_keep_stack.pop_back(); - if (not store_element) + if (!store_element) { return {false, nullptr}; } - assert(object_element); + JSON_ASSERT(object_element); *object_element = std::move(value); return {true, object_element}; } @@ -635,6 +656,7 @@ class json_sax_acceptor using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; + using binary_t = typename BasicJsonType::binary_t; bool null() { @@ -666,7 +688,12 @@ class json_sax_acceptor return true; } - bool start_object(std::size_t /*unused*/ = std::size_t(-1)) + bool binary(binary_t& /*unused*/) + { + return true; + } + + bool start_object(std::size_t /*unused*/ = static_cast(-1)) { return true; } @@ -681,7 +708,7 @@ class json_sax_acceptor return true; } - bool start_array(std::size_t /*unused*/ = std::size_t(-1)) + bool start_array(std::size_t /*unused*/ = static_cast(-1)) { return true; } @@ -696,6 +723,6 @@ class json_sax_acceptor return false; } }; -} // namespace detail -} // namespace nlohmann +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/lexer.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/lexer.hpp index 0843d749..72e99510 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/lexer.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/lexer.hpp @@ -1,3 +1,11 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // array @@ -14,27 +22,17 @@ #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + /////////// // lexer // /////////// -/*! -@brief lexical analysis - -This class organizes the lexical analysis during JSON deserialization. -*/ template -class lexer +class lexer_base { - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - public: /// token types for the parser enum class token_type @@ -75,9 +73,9 @@ class lexer return "null literal"; case token_type::value_string: return "string literal"; - case lexer::token_type::value_unsigned: - case lexer::token_type::value_integer: - case lexer::token_type::value_float: + case token_type::value_unsigned: + case token_type::value_integer: + case token_type::value_float: return "number literal"; case token_type::begin_array: return "'['"; @@ -103,15 +101,36 @@ class lexer // LCOV_EXCL_STOP } } +}; +/*! +@brief lexical analysis - explicit lexer(detail::input_adapter_t&& adapter) - : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} +This class organizes the lexical analysis during JSON deserialization. +*/ +template +class lexer : public lexer_base +{ + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + using char_type = typename InputAdapterType::char_type; + using char_int_type = typename std::char_traits::int_type; + + public: + using token_type = typename lexer_base::token_type; + + explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept + : ia(std::move(adapter)) + , ignore_comments(ignore_comments_) + , decimal_point_char(static_cast(get_decimal_point())) + {} // delete because of pointer members lexer(const lexer&) = delete; - lexer(lexer&&) = delete; + lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = delete; + lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~lexer() = default; private: @@ -123,8 +142,8 @@ class lexer JSON_HEDLEY_PURE static char get_decimal_point() noexcept { - const auto loc = localeconv(); - assert(loc != nullptr); + const auto* loc = localeconv(); + JSON_ASSERT(loc != nullptr); return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point); } @@ -150,7 +169,7 @@ class lexer int get_codepoint() { // this function only makes sense after reading `\u` - assert(current == 'u'); + JSON_ASSERT(current == 'u'); int codepoint = 0; const auto factors = { 12u, 8u, 4u, 0u }; @@ -158,15 +177,15 @@ class lexer { get(); - if (current >= '0' and current <= '9') + if (current >= '0' && current <= '9') { codepoint += static_cast((static_cast(current) - 0x30u) << factor); } - else if (current >= 'A' and current <= 'F') + else if (current >= 'A' && current <= 'F') { codepoint += static_cast((static_cast(current) - 0x37u) << factor); } - else if (current >= 'a' and current <= 'f') + else if (current >= 'a' && current <= 'f') { codepoint += static_cast((static_cast(current) - 0x57u) << factor); } @@ -176,7 +195,7 @@ class lexer } } - assert(0x0000 <= codepoint and codepoint <= 0xFFFF); + JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF); return codepoint; } @@ -195,15 +214,15 @@ class lexer @return true if and only if no range violation was detected */ - bool next_byte_in_range(std::initializer_list ranges) + bool next_byte_in_range(std::initializer_list ranges) { - assert(ranges.size() == 2 or ranges.size() == 4 or ranges.size() == 6); + JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6); add(current); for (auto range = ranges.begin(); range != ranges.end(); ++range) { get(); - if (JSON_HEDLEY_LIKELY(*range <= current and current <= *(++range))) + if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) { add(current); } @@ -220,7 +239,7 @@ class lexer /*! @brief scan a string literal - This function scans a string according to Sect. 7 of RFC 7159. While + This function scans a string according to Sect. 7 of RFC 8259. While scanning, bytes are escaped and copied into buffer token_buffer. Then the function returns successfully, token_buffer is *not* null-terminated (as it may contain \0 bytes), and token_buffer.size() is the number of bytes in the @@ -238,7 +257,7 @@ class lexer reset(); // we entered the function by reading an open quote - assert(current == '\"'); + JSON_ASSERT(current == '\"'); while (true) { @@ -246,7 +265,7 @@ class lexer switch (get()) { // end of file while parsing string - case std::char_traits::eof(): + case std::char_traits::eof(): { error_message = "invalid string: missing closing quote"; return token_type::parse_error; @@ -309,10 +328,10 @@ class lexer } // check if code point is a high surrogate - if (0xD800 <= codepoint1 and codepoint1 <= 0xDBFF) + if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF) { // expect next \uxxxx entry - if (JSON_HEDLEY_LIKELY(get() == '\\' and get() == 'u')) + if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u')) { const int codepoint2 = get_codepoint(); @@ -323,7 +342,7 @@ class lexer } // check if codepoint2 is a low surrogate - if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 and codepoint2 <= 0xDFFF)) + if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF)) { // overwrite codepoint codepoint = static_cast( @@ -332,25 +351,25 @@ class lexer // low surrogate occupies the least significant 15 bits + static_cast(codepoint2) // there is still the 0xD800, 0xDC00 and 0x10000 noise - // in the result so we have to subtract with: + // in the result, so we have to subtract with: // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00 - 0x35FDC00u); } else { - error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; + error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF"; return token_type::parse_error; } } else { - error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; + error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF"; return token_type::parse_error; } } else { - if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF)) + if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF)) { error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF"; return token_type::parse_error; @@ -358,34 +377,34 @@ class lexer } // result of the above calculation yields a proper codepoint - assert(0x00 <= codepoint and codepoint <= 0x10FFFF); + JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF); // translate codepoint into bytes if (codepoint < 0x80) { // 1-byte characters: 0xxxxxxx (ASCII) - add(codepoint); + add(static_cast(codepoint)); } else if (codepoint <= 0x7FF) { // 2-byte characters: 110xxxxx 10xxxxxx - add(static_cast(0xC0u | (static_cast(codepoint) >> 6u))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); + add(static_cast(0xC0u | (static_cast(codepoint) >> 6u))); + add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); } else if (codepoint <= 0xFFFF) { // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx - add(static_cast(0xE0u | (static_cast(codepoint) >> 12u))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); + add(static_cast(0xE0u | (static_cast(codepoint) >> 12u))); + add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); + add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); } else { // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - add(static_cast(0xF0u | (static_cast(codepoint) >> 18u))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 12u) & 0x3Fu))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); + add(static_cast(0xF0u | (static_cast(codepoint) >> 18u))); + add(static_cast(0x80u | ((static_cast(codepoint) >> 12u) & 0x3Fu))); + add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); + add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); } break; @@ -725,7 +744,7 @@ class lexer case 0xDE: case 0xDF: { - if (JSON_HEDLEY_UNLIKELY(not next_byte_in_range({0x80, 0xBF}))) + if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF}))) { return token_type::parse_error; } @@ -735,7 +754,7 @@ class lexer // U+0800..U+0FFF: bytes E0 A0..BF 80..BF case 0xE0: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -759,7 +778,7 @@ class lexer case 0xEE: case 0xEF: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -769,7 +788,7 @@ class lexer // U+D000..U+D7FF: bytes ED 80..9F 80..BF case 0xED: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0x9F, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -779,7 +798,7 @@ class lexer // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF case 0xF0: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -791,7 +810,7 @@ class lexer case 0xF2: case 0xF3: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -801,7 +820,7 @@ class lexer // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF case 0xF4: { - if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF})))) + if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF})))) { return token_type::parse_error; } @@ -818,6 +837,77 @@ class lexer } } + /*! + * @brief scan a comment + * @return whether comment could be scanned successfully + */ + bool scan_comment() + { + switch (get()) + { + // single-line comments skip input until a newline or EOF is read + case '/': + { + while (true) + { + switch (get()) + { + case '\n': + case '\r': + case std::char_traits::eof(): + case '\0': + return true; + + default: + break; + } + } + } + + // multi-line comments skip input until */ is read + case '*': + { + while (true) + { + switch (get()) + { + case std::char_traits::eof(): + case '\0': + { + error_message = "invalid comment; missing closing '*/'"; + return false; + } + + case '*': + { + switch (get()) + { + case '/': + return true; + + default: + { + unget(); + continue; + } + } + } + + default: + continue; + } + } + } + + // unexpected character after reading '/' + default: + { + error_message = "invalid comment; expecting '/' or '*' after '/'"; + return false; + } + } + } + JSON_HEDLEY_NON_NULL(2) static void strtof(float& f, const char* str, char** endptr) noexcept { @@ -839,10 +929,10 @@ class lexer /*! @brief scan a number literal - This function scans a string according to Sect. 6 of RFC 7159. + This function scans a string according to Sect. 6 of RFC 8259. The function is realized with a deterministic finite state machine derived - from the grammar described in RFC 7159. Starting in state "init", the + from the grammar described in RFC 8259. Starting in state "init", the input is read and used to determined the next state. Only state "done" accepts the number. State "error" is a trap state to model errors. In the table below, "anything" means any character but the ones listed before. @@ -853,7 +943,7 @@ class lexer minus | zero | any1 | [error] | [error] | [error] | [error] | [error] zero | done | done | exponent | done | done | decimal1 | done any1 | any1 | any1 | exponent | done | done | decimal1 | done - decimal1 | decimal2 | [error] | [error] | [error] | [error] | [error] | [error] + decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error] decimal2 | decimal2 | decimal2 | exponent | done | done | done | done exponent | any2 | any2 | [error] | sign | sign | [error] | [error] sign | any2 | any2 | [error] | [error] | [error] | [error] | [error] @@ -916,7 +1006,7 @@ class lexer // all other characters are rejected outside scan_number() default: // LCOV_EXCL_LINE - assert(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } scan_number_minus: @@ -1154,7 +1244,7 @@ class lexer // we are done scanning a number) unget(); - char* endptr = nullptr; + char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) errno = 0; // try to parse integers first and fall back to floats @@ -1163,7 +1253,7 @@ class lexer const auto x = std::strtoull(token_buffer.data(), &endptr, 10); // we checked the number format before - assert(endptr == token_buffer.data() + token_buffer.size()); + JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); if (errno == 0) { @@ -1179,7 +1269,7 @@ class lexer const auto x = std::strtoll(token_buffer.data(), &endptr, 10); // we checked the number format before - assert(endptr == token_buffer.data() + token_buffer.size()); + JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); if (errno == 0) { @@ -1196,7 +1286,7 @@ class lexer strtof(value_float, token_buffer.data(), &endptr); // we checked the number format before - assert(endptr == token_buffer.data() + token_buffer.size()); + JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); return token_type::value_float; } @@ -1207,13 +1297,13 @@ class lexer @param[in] return_type the token type to return on success */ JSON_HEDLEY_NON_NULL(2) - token_type scan_literal(const char* literal_text, const std::size_t length, + token_type scan_literal(const char_type* literal_text, const std::size_t length, token_type return_type) { - assert(current == literal_text[0]); + JSON_ASSERT(std::char_traits::to_char_type(current) == literal_text[0]); for (std::size_t i = 1; i < length; ++i) { - if (JSON_HEDLEY_UNLIKELY(get() != literal_text[i])) + if (JSON_HEDLEY_UNLIKELY(std::char_traits::to_char_type(get()) != literal_text[i])) { error_message = "invalid literal"; return token_type::parse_error; @@ -1231,7 +1321,7 @@ class lexer { token_buffer.clear(); token_string.clear(); - token_string.push_back(std::char_traits::to_char_type(current)); + token_string.push_back(std::char_traits::to_char_type(current)); } /* @@ -1244,7 +1334,7 @@ class lexer @return character read from the input */ - std::char_traits::int_type get() + char_int_type get() { ++position.chars_read_total; ++position.chars_read_current_line; @@ -1256,12 +1346,12 @@ class lexer } else { - current = ia->get_character(); + current = ia.get_character(); } - if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) + if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) { - token_string.push_back(std::char_traits::to_char_type(current)); + token_string.push_back(std::char_traits::to_char_type(current)); } if (current == '\n') @@ -1300,17 +1390,17 @@ class lexer --position.chars_read_current_line; } - if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) + if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) { - assert(not token_string.empty()); + JSON_ASSERT(!token_string.empty()); token_string.pop_back(); } } /// add a character to token_buffer - void add(int c) + void add(char_int_type c) { - token_buffer.push_back(std::char_traits::to_char_type(c)); + token_buffer.push_back(static_cast(c)); } public: @@ -1361,17 +1451,17 @@ class lexer std::string result; for (const auto c : token_string) { - if ('\x00' <= c and c <= '\x1F') + if (static_cast(c) <= '\x1F') { // escape control characters std::array cs{{}}; - (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); + static_cast((std::snprintf)(cs.data(), cs.size(), "", static_cast(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) result += cs.data(); } else { // add character as is - result.push_back(c); + result.push_back(static_cast(c)); } } @@ -1398,7 +1488,7 @@ class lexer if (get() == 0xEF) { // check if we completely parse the BOM - return get() == 0xBB and get() == 0xBF; + return get() == 0xBB && get() == 0xBF; } // the first character is not the beginning of the BOM; unget it to @@ -1407,21 +1497,38 @@ class lexer return true; } + void skip_whitespace() + { + do + { + get(); + } + while (current == ' ' || current == '\t' || current == '\n' || current == '\r'); + } + token_type scan() { // initially, skip the BOM - if (position.chars_read_total == 0 and not skip_bom()) + if (position.chars_read_total == 0 && !skip_bom()) { error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given"; return token_type::parse_error; } // read next character and ignore whitespace - do + skip_whitespace(); + + // ignore comments + while (ignore_comments && current == '/') { - get(); + if (!scan_comment()) + { + return token_type::parse_error; + } + + // skip following whitespace + skip_whitespace(); } - while (current == ' ' or current == '\t' or current == '\n' or current == '\r'); switch (current) { @@ -1441,11 +1548,20 @@ class lexer // literals case 't': - return scan_literal("true", 4, token_type::literal_true); + { + std::array true_literal = {{static_cast('t'), static_cast('r'), static_cast('u'), static_cast('e')}}; + return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true); + } case 'f': - return scan_literal("false", 5, token_type::literal_false); + { + std::array false_literal = {{static_cast('f'), static_cast('a'), static_cast('l'), static_cast('s'), static_cast('e')}}; + return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false); + } case 'n': - return scan_literal("null", 4, token_type::literal_null); + { + std::array null_literal = {{static_cast('n'), static_cast('u'), static_cast('l'), static_cast('l')}}; + return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null); + } // string case '\"': @@ -1468,7 +1584,7 @@ class lexer // end of input (the null byte is needed when parsing from // string literals) case '\0': - case std::char_traits::eof(): + case std::char_traits::eof(): return token_type::end_of_input; // error @@ -1480,10 +1596,13 @@ class lexer private: /// input adapter - detail::input_adapter_t ia = nullptr; + InputAdapterType ia; + + /// whether comments should be ignored (true) or signaled as errors (false) + const bool ignore_comments = false; /// the current character - std::char_traits::int_type current = std::char_traits::eof(); + char_int_type current = std::char_traits::eof(); /// whether the next get() call should just return current bool next_unget = false; @@ -1492,7 +1611,7 @@ class lexer position_t position {}; /// raw input token string (for error messages) - std::vector token_string {}; + std::vector token_string {}; /// buffer for variable-length tokens (numbers, strings) string_t token_buffer {}; @@ -1506,7 +1625,8 @@ class lexer number_float_t value_float = 0; /// the decimal point - const char decimal_point_char = '.'; + const char_int_type decimal_point_char = '.'; }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/parser.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/parser.hpp index 8d4febcb..8acbd4fc 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/parser.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/parser.hpp @@ -1,6 +1,13 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once -#include // assert #include // isfinite #include // uint8_t #include // function @@ -14,56 +21,60 @@ #include #include #include +#include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { //////////// // parser // //////////// +enum class parse_event_t : std::uint8_t +{ + /// the parser read `{` and started to process a JSON object + object_start, + /// the parser read `}` and finished processing a JSON object + object_end, + /// the parser read `[` and started to process a JSON array + array_start, + /// the parser read `]` and finished processing a JSON array + array_end, + /// the parser read a key of a value in an object + key, + /// the parser finished reading a JSON value + value +}; + +template +using parser_callback_t = + std::function; + /*! @brief syntax analysis -This class implements a recursive decent parser. +This class implements a recursive descent parser. */ -template +template class parser { using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; + using lexer_t = lexer; using token_type = typename lexer_t::token_type; public: - enum class parse_event_t : uint8_t - { - /// the parser read `{` and started to process a JSON object - object_start, - /// the parser read `}` and finished processing a JSON object - object_end, - /// the parser read `[` and started to process a JSON array - array_start, - /// the parser read `]` and finished processing a JSON array - array_end, - /// the parser read a key of a value in an object - key, - /// the parser finished reading a JSON value - value - }; - - using parser_callback_t = - std::function; - /// a parser reading from an input adapter - explicit parser(detail::input_adapter_t&& adapter, - const parser_callback_t cb = nullptr, - const bool allow_exceptions_ = true) - : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) + explicit parser(InputAdapterType&& adapter, + const parser_callback_t cb = nullptr, + const bool allow_exceptions_ = true, + const bool skip_comments = false) + : callback(cb) + , m_lexer(std::move(adapter), skip_comments) + , allow_exceptions(allow_exceptions_) { // read first token get_token(); @@ -85,15 +96,14 @@ class parser { json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read - if (strict and (get_token() != token_type::end_of_input)) + if (strict && (get_token() != token_type::end_of_input)) { sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), nullptr)); } // in case of an error, return discarded value @@ -114,15 +124,13 @@ class parser { json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read - if (strict and (get_token() != token_type::end_of_input)) + if (strict && (get_token() != token_type::end_of_input)) { sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr)); } // in case of an error, return discarded value @@ -132,6 +140,8 @@ class parser return; } } + + result.assert_invariant(); } /*! @@ -146,7 +156,7 @@ class parser return sax_parse(&sax_acceptor, strict); } - template + template JSON_HEDLEY_NON_NULL(2) bool sax_parse(SAX* sax, const bool strict = true) { @@ -154,19 +164,18 @@ class parser const bool result = sax_parse_internal(sax); // strict mode: next byte must be EOF - if (result and strict and (get_token() != token_type::end_of_input)) + if (result && strict && (get_token() != token_type::end_of_input)) { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr)); } return result; } private: - template + template JSON_HEDLEY_NON_NULL(2) bool sax_parse_internal(SAX* sax) { @@ -178,14 +187,14 @@ class parser while (true) { - if (not skip_to_state_evaluation) + if (!skip_to_state_evaluation) { // invariant: get_token() was called before each iteration switch (last_token) { case token_type::begin_object: { - if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_object(static_cast(-1)))) { return false; } @@ -193,7 +202,7 @@ class parser // closing } -> we are done if (get_token() == token_type::end_object) { - if (JSON_HEDLEY_UNLIKELY(not sax->end_object())) + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) { return false; } @@ -205,10 +214,9 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr)); } - if (JSON_HEDLEY_UNLIKELY(not sax->key(m_lexer.get_string()))) + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { return false; } @@ -218,8 +226,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr)); } // remember we are now inside an object @@ -232,7 +239,7 @@ class parser case token_type::begin_array: { - if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1)))) + if (JSON_HEDLEY_UNLIKELY(!sax->start_array(static_cast(-1)))) { return false; } @@ -240,7 +247,7 @@ class parser // closing ] -> we are done if (get_token() == token_type::end_array) { - if (JSON_HEDLEY_UNLIKELY(not sax->end_array())) + if (JSON_HEDLEY_UNLIKELY(!sax->end_array())) { return false; } @@ -258,14 +265,14 @@ class parser { const auto res = m_lexer.get_number_float(); - if (JSON_HEDLEY_UNLIKELY(not std::isfinite(res))) + if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res))) { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr)); } - if (JSON_HEDLEY_UNLIKELY(not sax->number_float(res, m_lexer.get_string()))) + if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) { return false; } @@ -275,7 +282,7 @@ class parser case token_type::literal_false: { - if (JSON_HEDLEY_UNLIKELY(not sax->boolean(false))) + if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false))) { return false; } @@ -284,7 +291,7 @@ class parser case token_type::literal_null: { - if (JSON_HEDLEY_UNLIKELY(not sax->null())) + if (JSON_HEDLEY_UNLIKELY(!sax->null())) { return false; } @@ -293,7 +300,7 @@ class parser case token_type::literal_true: { - if (JSON_HEDLEY_UNLIKELY(not sax->boolean(true))) + if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true))) { return false; } @@ -302,7 +309,7 @@ class parser case token_type::value_integer: { - if (JSON_HEDLEY_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer()))) + if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer()))) { return false; } @@ -311,7 +318,7 @@ class parser case token_type::value_string: { - if (JSON_HEDLEY_UNLIKELY(not sax->string(m_lexer.get_string()))) + if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string()))) { return false; } @@ -320,7 +327,7 @@ class parser case token_type::value_unsigned: { - if (JSON_HEDLEY_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned()))) + if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned()))) { return false; } @@ -332,16 +339,21 @@ class parser // using "uninitialized" to avoid "expected" message return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr)); } + case token_type::uninitialized: + case token_type::end_array: + case token_type::end_object: + case token_type::name_separator: + case token_type::value_separator: + case token_type::end_of_input: + case token_type::literal_or_value: default: // the last token was unexpected { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr)); } } } @@ -370,7 +382,7 @@ class parser // closing ] if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array)) { - if (JSON_HEDLEY_UNLIKELY(not sax->end_array())) + if (JSON_HEDLEY_UNLIKELY(!sax->end_array())) { return false; } @@ -379,7 +391,7 @@ class parser // new value, we need to evaluate the new state first. // By setting skip_to_state_evaluation to false, we // are effectively jumping to the beginning of this if. - assert(not states.empty()); + JSON_ASSERT(!states.empty()); states.pop_back(); skip_to_state_evaluation = true; continue; @@ -387,65 +399,61 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr)); } - else // object - { - // comma -> next value - if (get_token() == token_type::value_separator) - { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); - } - if (JSON_HEDLEY_UNLIKELY(not sax->key(m_lexer.get_string()))) - { - return false; - } + // states.back() is false -> object - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); - } + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr)); + } - // parse values - get_token(); - continue; + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) + { + return false; } - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) { - if (JSON_HEDLEY_UNLIKELY(not sax->end_object())) - { - return false; - } + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr)); + } - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - assert(not states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; + // parse values + get_token(); + continue; + } + + // closing } + if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + { + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) + { + return false; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + JSON_ASSERT(!states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; } + + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr)); } } @@ -459,26 +467,26 @@ class parser { std::string error_msg = "syntax error "; - if (not context.empty()) + if (!context.empty()) { - error_msg += "while parsing " + context + " "; + error_msg += concat("while parsing ", context, ' '); } error_msg += "- "; if (last_token == token_type::parse_error) { - error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + - m_lexer.get_token_string() + "'"; + error_msg += concat(m_lexer.get_error_message(), "; last read: '", + m_lexer.get_token_string(), '\''); } else { - error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token)); + error_msg += concat("unexpected ", lexer_t::token_type_name(last_token)); } if (expected != token_type::uninitialized) { - error_msg += "; expected " + std::string(lexer_t::token_type_name(expected)); + error_msg += concat("; expected ", lexer_t::token_type_name(expected)); } return error_msg; @@ -486,7 +494,7 @@ class parser private: /// callback function - const parser_callback_t callback = nullptr; + const parser_callback_t callback = nullptr; /// the type of the last read token token_type last_token = token_type::uninitialized; /// the lexer @@ -494,5 +502,6 @@ class parser /// whether to throw exceptions in case of errors const bool allow_exceptions = true; }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/position_t.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/position_t.hpp index 14e9649f..396db0e1 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/input/position_t.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/input/position_t.hpp @@ -1,11 +1,21 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // size_t -namespace nlohmann -{ +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + /// struct to capture the start position of the current token struct position_t { @@ -23,5 +33,5 @@ struct position_t } }; -} // namespace detail -} // namespace nlohmann +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/internal_iterator.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/internal_iterator.hpp index 2c81f723..13a212c8 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/internal_iterator.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/internal_iterator.hpp @@ -1,11 +1,20 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once +#include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + /*! @brief an iterator value @@ -21,5 +30,6 @@ template struct internal_iterator /// generic iterator for all other types primitive_iterator_t primitive_iterator {}; }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iter_impl.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iter_impl.hpp index 3a362971..3f5a9901 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iter_impl.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iter_impl.hpp @@ -1,6 +1,13 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once -#include // not #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const @@ -12,10 +19,10 @@ #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + // forward declare, to be able to friend it later on template class iteration_proxy; template class iteration_proxy_value; @@ -37,10 +44,12 @@ This class implements a both iterators (iterator and const_iterator) for the iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593) */ template -class iter_impl +class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions) { + /// the iterator with BasicJsonType of different const-ness + using other_iter_impl = iter_impl::value, typename std::remove_const::type, const BasicJsonType>::type>; /// allow basic_json to access private members - friend iter_impl::value, typename std::remove_const::type, const BasicJsonType>::type>; + friend other_iter_impl; friend BasicJsonType; friend iteration_proxy; friend iteration_proxy_value; @@ -50,9 +59,12 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); + // superficial check for the LegacyBidirectionalIterator named requirement + static_assert(std::is_base_of::value + && std::is_base_of::iterator_category>::value, + "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement."); public: - /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17. /// The C++ Standard has never required user-defined iterators to derive from std::iterator. /// A user-defined iterator should provide publicly accessible typedefs named @@ -74,8 +86,10 @@ class iter_impl typename BasicJsonType::const_reference, typename BasicJsonType::reference>::type; - /// default constructor iter_impl() = default; + ~iter_impl() = default; + iter_impl(iter_impl&&) noexcept = default; + iter_impl& operator=(iter_impl&&) noexcept = default; /*! @brief constructor for a given JSON instance @@ -85,7 +99,7 @@ class iter_impl */ explicit iter_impl(pointer object) noexcept : m_object(object) { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -101,6 +115,14 @@ class iter_impl break; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { m_it.primitive_iterator = primitive_iterator_t(); @@ -137,8 +159,11 @@ class iter_impl */ iter_impl& operator=(const iter_impl& other) noexcept { - m_object = other.m_object; - m_it = other.m_it; + if (&other != this) + { + m_object = other.m_object; + m_it = other.m_it; + } return *this; } @@ -157,21 +182,21 @@ class iter_impl @return const/non-const iterator @note It is not checked whether @a other is initialized. */ - iter_impl& operator=(const iter_impl::type>& other) noexcept + iter_impl& operator=(const iter_impl::type>& other) noexcept // NOLINT(cert-oop54-cpp) { m_object = other.m_object; m_it = other.m_it; return *this; } - private: + JSON_PRIVATE_UNLESS_TESTED: /*! @brief set the iterator to the first value @pre The iterator is initialized; i.e. `m_object != nullptr`. */ void set_begin() noexcept { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -194,6 +219,13 @@ class iter_impl break; } + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { m_it.primitive_iterator.set_begin(); @@ -208,7 +240,7 @@ class iter_impl */ void set_end() noexcept { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -224,6 +256,14 @@ class iter_impl break; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { m_it.primitive_iterator.set_end(); @@ -239,25 +279,32 @@ class iter_impl */ reference operator*() const { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: { - assert(m_it.object_iterator != m_object->m_value.object->end()); + JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end()); return m_it.object_iterator->second; } case value_t::array: { - assert(m_it.array_iterator != m_object->m_value.array->end()); + JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end()); return *m_it.array_iterator; } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); - + JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object)); + + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin())) @@ -265,7 +312,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object)); } } } @@ -276,22 +323,30 @@ class iter_impl */ pointer operator->() const { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: { - assert(m_it.object_iterator != m_object->m_value.object->end()); + JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end()); return &(m_it.object_iterator->second); } case value_t::array: { - assert(m_it.array_iterator != m_object->m_value.array->end()); + JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end()); return &*m_it.array_iterator; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin())) @@ -299,7 +354,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object)); } } } @@ -308,7 +363,7 @@ class iter_impl @brief post-increment (it++) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator++(int) + iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp) { auto result = *this; ++(*this); @@ -321,7 +376,7 @@ class iter_impl */ iter_impl& operator++() { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -337,6 +392,14 @@ class iter_impl break; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { ++m_it.primitive_iterator; @@ -351,7 +414,7 @@ class iter_impl @brief post-decrement (it--) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator--(int) + iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp) { auto result = *this; --(*this); @@ -364,7 +427,7 @@ class iter_impl */ iter_impl& operator--() { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -380,6 +443,14 @@ class iter_impl break; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { --m_it.primitive_iterator; @@ -391,18 +462,19 @@ class iter_impl } /*! - @brief comparison: equal + @brief comparison: equal @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - bool operator==(const iter_impl& other) const + template < typename IterImpl, detail::enable_if_t < (std::is_same::value || std::is_same::value), std::nullptr_t > = nullptr > + bool operator==(const IterImpl& other) const { // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object)); } - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { @@ -412,22 +484,31 @@ class iter_impl case value_t::array: return (m_it.array_iterator == other.m_it.array_iterator); + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: return (m_it.primitive_iterator == other.m_it.primitive_iterator); } } /*! - @brief comparison: not equal + @brief comparison: not equal @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - bool operator!=(const iter_impl& other) const + template < typename IterImpl, detail::enable_if_t < (std::is_same::value || std::is_same::value), std::nullptr_t > = nullptr > + bool operator!=(const IterImpl& other) const { - return not operator==(other); + return !operator==(other); } /*! - @brief comparison: smaller + @brief comparison: smaller @pre The iterator is initialized; i.e. `m_object != nullptr`. */ bool operator<(const iter_impl& other) const @@ -435,63 +516,71 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object)); } - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object)); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: return (m_it.primitive_iterator < other.m_it.primitive_iterator); } } /*! - @brief comparison: less than or equal + @brief comparison: less than or equal @pre The iterator is initialized; i.e. `m_object != nullptr`. */ bool operator<=(const iter_impl& other) const { - return not other.operator < (*this); + return !other.operator < (*this); } /*! - @brief comparison: greater than + @brief comparison: greater than @pre The iterator is initialized; i.e. `m_object != nullptr`. */ bool operator>(const iter_impl& other) const { - return not operator<=(other); + return !operator<=(other); } /*! - @brief comparison: greater than or equal + @brief comparison: greater than or equal @pre The iterator is initialized; i.e. `m_object != nullptr`. */ bool operator>=(const iter_impl& other) const { - return not operator<(other); + return !operator<(other); } /*! - @brief add to iterator + @brief add to iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ iter_impl& operator+=(difference_type i) { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object)); case value_t::array: { @@ -499,6 +588,14 @@ class iter_impl break; } + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { m_it.primitive_iterator += i; @@ -510,7 +607,7 @@ class iter_impl } /*! - @brief subtract from iterator + @brief subtract from iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ iter_impl& operator-=(difference_type i) @@ -519,7 +616,7 @@ class iter_impl } /*! - @brief add to iterator + @brief add to iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ iter_impl operator+(difference_type i) const @@ -530,7 +627,7 @@ class iter_impl } /*! - @brief addition of distance and iterator + @brief addition of distance and iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ friend iter_impl operator+(difference_type i, const iter_impl& it) @@ -541,7 +638,7 @@ class iter_impl } /*! - @brief subtract from iterator + @brief subtract from iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ iter_impl operator-(difference_type i) const @@ -552,45 +649,60 @@ class iter_impl } /*! - @brief return difference + @brief return difference @pre The iterator is initialized; i.e. `m_object != nullptr`. */ difference_type operator-(const iter_impl& other) const { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object)); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: return m_it.primitive_iterator - other.m_it.primitive_iterator; } } /*! - @brief access to successor + @brief access to successor @pre The iterator is initialized; i.e. `m_object != nullptr`. */ reference operator[](difference_type n) const { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object)); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); - + JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object)); + + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: { if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n)) @@ -598,29 +710,29 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object)); } } } /*! - @brief return the key of an object iterator + @brief return the key of an object iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ const typename object_t::key_type& key() const { - assert(m_object != nullptr); + JSON_ASSERT(m_object != nullptr); if (JSON_HEDLEY_LIKELY(m_object->is_object())) { return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object)); } /*! - @brief return the value of an iterator + @brief return the value of an iterator @pre The iterator is initialized; i.e. `m_object != nullptr`. */ reference value() const @@ -628,11 +740,12 @@ class iter_impl return operator*(); } - private: + JSON_PRIVATE_UNLESS_TESTED: /// associated JSON instance pointer m_object = nullptr; /// the actual iterator of the associated instance internal_iterator::type> m_it {}; }; -} // namespace detail -} // namespace nlohmann + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iteration_proxy.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iteration_proxy.hpp index c61d9629..659cd06f 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iteration_proxy.hpp @@ -1,35 +1,51 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // size_t #include // input_iterator_tag #include // string, to_string #include // tuple_size, get, tuple_element +#include // move + +#if JSON_HAS_RANGES + #include // enable_borrowed_range +#endif +#include #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + template void int_to_string( string_type& target, std::size_t value ) { - target = std::to_string(value); + // For ADL + using std::to_string; + target = to_string(value); } -template class iteration_proxy_value +template class iteration_proxy_value { public: using difference_type = std::ptrdiff_t; using value_type = iteration_proxy_value; - using pointer = value_type * ; - using reference = value_type & ; + using pointer = value_type *; + using reference = value_type &; using iterator_category = std::input_iterator_tag; using string_type = typename std::remove_cv< typename std::remove_reference().key() ) >::type >::type; private: /// the iterator - IteratorType anchor; + IteratorType anchor{}; /// an index for arrays (used to create key names) std::size_t array_index = 0; /// last stringified array index @@ -37,13 +53,30 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str = ""; + string_type empty_str{}; public: - explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} + explicit iteration_proxy_value() = default; + explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0) + noexcept(std::is_nothrow_move_constructible::value + && std::is_nothrow_default_constructible::value) + : anchor(std::move(it)) + , array_index(array_index_) + {} + + iteration_proxy_value(iteration_proxy_value const&) = default; + iteration_proxy_value& operator=(iteration_proxy_value const&) = default; + // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions + iteration_proxy_value(iteration_proxy_value&&) + noexcept(std::is_nothrow_move_constructible::value + && std::is_nothrow_move_constructible::value) = default; + iteration_proxy_value& operator=(iteration_proxy_value&&) + noexcept(std::is_nothrow_move_assignable::value + && std::is_nothrow_move_assignable::value) = default; + ~iteration_proxy_value() = default; /// dereference operator (needed for range-based for) - iteration_proxy_value& operator*() + const iteration_proxy_value& operator*() const { return *this; } @@ -57,6 +90,14 @@ template class iteration_proxy_value return *this; } + iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp) + { + auto tmp = iteration_proxy_value(anchor, array_index); + ++anchor; + ++array_index; + return tmp; + } + /// equality operator (needed for InputIterator) bool operator==(const iteration_proxy_value& o) const { @@ -72,7 +113,7 @@ template class iteration_proxy_value /// return key of the iterator const string_type& key() const { - assert(anchor.m_object != nullptr); + JSON_ASSERT(anchor.m_object != nullptr); switch (anchor.m_object->type()) { @@ -92,6 +133,14 @@ template class iteration_proxy_value return anchor.key(); // use an empty key for all primitive types + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: default: return empty_str; } @@ -109,29 +158,38 @@ template class iteration_proxy { private: /// the container to iterate - typename IteratorType::reference container; + typename IteratorType::pointer container = nullptr; public: + explicit iteration_proxy() = default; + /// construct iteration proxy from a container explicit iteration_proxy(typename IteratorType::reference cont) noexcept - : container(cont) {} + : container(&cont) {} + + iteration_proxy(iteration_proxy const&) = default; + iteration_proxy& operator=(iteration_proxy const&) = default; + iteration_proxy(iteration_proxy&&) noexcept = default; + iteration_proxy& operator=(iteration_proxy&&) noexcept = default; + ~iteration_proxy() = default; /// return iterator begin (needed for range-based for) - iteration_proxy_value begin() noexcept + iteration_proxy_value begin() const noexcept { - return iteration_proxy_value(container.begin()); + return iteration_proxy_value(container->begin()); } /// return iterator end (needed for range-based for) - iteration_proxy_value end() noexcept + iteration_proxy_value end() const noexcept { - return iteration_proxy_value(container.end()); + return iteration_proxy_value(container->end()); } }; + // Structured Bindings Support // For further reference see https://blog.tartanllama.xyz/structured-bindings/ // And see https://github.com/nlohmann/json/pull/1391 -template = 0> +template = 0> auto get(const nlohmann::detail::iteration_proxy_value& i) -> decltype(i.key()) { return i.key(); @@ -139,13 +197,14 @@ auto get(const nlohmann::detail::iteration_proxy_value& i) -> decl // Structured Bindings Support // For further reference see https://blog.tartanllama.xyz/structured-bindings/ // And see https://github.com/nlohmann/json/pull/1391 -template = 0> +template = 0> auto get(const nlohmann::detail::iteration_proxy_value& i) -> decltype(i.value()) { return i.value(); } + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END // The Addition to the STD Namespace is required to add // Structured Bindings Support to the iteration_proxy_value class @@ -153,16 +212,17 @@ auto get(const nlohmann::detail::iteration_proxy_value& i) -> decl // And see https://github.com/nlohmann/json/pull/1391 namespace std { + #if defined(__clang__) // Fix: https://github.com/nlohmann/json/issues/1401 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wmismatched-tags" #endif -template +template class tuple_size<::nlohmann::detail::iteration_proxy_value> : public std::integral_constant {}; -template +template class tuple_element> { public: @@ -173,4 +233,10 @@ class tuple_element> #if defined(__clang__) #pragma clang diagnostic pop #endif -} // namespace std + +} // namespace std + +#if JSON_HAS_RANGES + template + inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy> = true; +#endif diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iterator_traits.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iterator_traits.hpp index 4cced80c..34a20eee 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iterator_traits.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/iterator_traits.hpp @@ -1,18 +1,27 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // random_access_iterator_tag +#include #include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { -template + +template struct iterator_types {}; -template +template struct iterator_types < It, void_t +template struct iterator_traits { }; -template +template struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> : iterator_types { }; -template +template struct iterator_traits::value>> { using iterator_category = std::random_access_iterator_tag; @@ -47,5 +56,6 @@ struct iterator_traits::value>> using pointer = T*; using reference = T&; }; -} // namespace detail -} // namespace nlohmann + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/json_reverse_iterator.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/json_reverse_iterator.hpp index f3b5b5db..eb450e98 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/json_reverse_iterator.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/json_reverse_iterator.hpp @@ -1,13 +1,23 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // ptrdiff_t #include // reverse_iterator #include // declval -namespace nlohmann -{ +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + ////////////////////// // reverse_iterator // ////////////////////// @@ -48,7 +58,7 @@ class json_reverse_iterator : public std::reverse_iterator explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {} /// post-increment (it++) - json_reverse_iterator const operator++(int) + json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp) { return static_cast(base_iterator::operator++(1)); } @@ -60,7 +70,7 @@ class json_reverse_iterator : public std::reverse_iterator } /// post-decrement (it--) - json_reverse_iterator const operator--(int) + json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp) { return static_cast(base_iterator::operator--(1)); } @@ -115,5 +125,6 @@ class json_reverse_iterator : public std::reverse_iterator return it.operator * (); } }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/primitive_iterator.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/primitive_iterator.hpp index 28d6f1a6..0bc3ca80 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/primitive_iterator.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/iterators/primitive_iterator.hpp @@ -1,12 +1,22 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // ptrdiff_t #include // numeric_limits -namespace nlohmann -{ +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + /* @brief an iterator for primitive JSON types @@ -23,6 +33,7 @@ class primitive_iterator_t static constexpr difference_type begin_value = 0; static constexpr difference_type end_value = begin_value + 1; + JSON_PRIVATE_UNLESS_TESTED: /// iterator as signed integer type difference_type m_it = (std::numeric_limits::min)(); @@ -84,7 +95,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator++(int) noexcept + primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp) { auto result = *this; ++m_it; @@ -97,7 +108,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator--(int) noexcept + primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp) { auto result = *this; --m_it; @@ -116,5 +127,6 @@ class primitive_iterator_t return *this; } }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/json_pointer.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/json_pointer.hpp index 87af3423..3f69bcdf 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/json_pointer.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/json_pointer.hpp @@ -1,8 +1,21 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include // all_of -#include // assert #include // isdigit +#include // errno, ERANGE +#include // strtoull +#ifndef JSON_NO_IO + #include // ostream +#endif // JSON_NO_IO +#include // max #include // accumulate #include // string #include // move @@ -10,89 +23,78 @@ #include #include +#include +#include #include -namespace nlohmann -{ -template +NLOHMANN_JSON_NAMESPACE_BEGIN + +/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document +/// @sa https://json.nlohmann.me/api/json_pointer/ +template class json_pointer { // allow basic_json to access private members NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; - public: - /*! - @brief create JSON pointer - - Create a JSON pointer according to the syntax described in - [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). - - @param[in] s string representing the JSON pointer; if omitted, the empty - string is assumed which references the whole JSON value + template + friend class json_pointer; - @throw parse_error.107 if the given JSON pointer @a s is nonempty and does - not begin with a slash (`/`); see example below + template + struct string_t_helper + { + using type = T; + }; - @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is - not followed by `0` (representing `~`) or `1` (representing `/`); see - example below + NLOHMANN_BASIC_JSON_TPL_DECLARATION + struct string_t_helper + { + using type = StringType; + }; - @liveexample{The example shows the construction several valid JSON pointers - as well as the exceptional behavior.,json_pointer} + public: + // for backwards compatibility accept BasicJsonType + using string_t = typename string_t_helper::type; - @since version 2.0.0 - */ - explicit json_pointer(const std::string& s = "") + /// @brief create JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/ + explicit json_pointer(const string_t& s = "") : reference_tokens(split(s)) {} - /*! - @brief return a string representation of the JSON pointer - - @invariant For each JSON pointer `ptr`, it holds: - @code {.cpp} - ptr == json_pointer(ptr.to_string()); - @endcode - - @return a string representation of the JSON pointer - - @liveexample{The example shows the result of `to_string`.,json_pointer__to_string} - - @since version 2.0.0 - */ - std::string to_string() const + /// @brief return a string representation of the JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/to_string/ + string_t to_string() const { return std::accumulate(reference_tokens.begin(), reference_tokens.end(), - std::string{}, - [](const std::string & a, const std::string & b) + string_t{}, + [](const string_t& a, const string_t& b) { - return a + "/" + escape(b); + return detail::concat(a, '/', detail::escape(b)); }); } - /// @copydoc to_string() - operator std::string() const + /// @brief return a string representation of the JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/ + JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string()) + operator string_t() const { return to_string(); } - /*! - @brief append another JSON pointer at the end of this JSON pointer - - @param[in] ptr JSON pointer to append - @return JSON pointer with @a ptr appended - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Linear in the length of @a ptr. - - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, const json_pointer&) for a binary operator +#ifndef JSON_NO_IO + /// @brief write string representation of the JSON pointer to stream + /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/ + friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr) + { + o << ptr.to_string(); + return o; + } +#endif - @since version 3.6.0 - */ + /// @brief append another JSON pointer at the end of this JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ json_pointer& operator/=(const json_pointer& ptr) { reference_tokens.insert(reference_tokens.end(), @@ -101,123 +103,45 @@ class json_pointer return *this; } - /*! - @brief append an unescaped reference token at the end of this JSON pointer - - @param[in] token reference token to append - @return JSON pointer with @a token appended without escaping @a token - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Amortized constant. - - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, std::size_t) for a binary operator - - @since version 3.6.0 - */ - json_pointer& operator/=(std::string token) + /// @brief append an unescaped reference token at the end of this JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ + json_pointer& operator/=(string_t token) { push_back(std::move(token)); return *this; } - /*! - @brief append an array index at the end of this JSON pointer - - @param[in] array_index array index to append - @return JSON pointer with @a array_index appended - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Amortized constant. - - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/(const json_pointer&, std::string) for a binary operator - - @since version 3.6.0 - */ - json_pointer& operator/=(std::size_t array_index) + /// @brief append an array index at the end of this JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/ + json_pointer& operator/=(std::size_t array_idx) { - return *this /= std::to_string(array_index); + return *this /= std::to_string(array_idx); } - /*! - @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer - - @param[in] lhs JSON pointer - @param[in] rhs JSON pointer - @return a new JSON pointer with @a rhs appended to @a lhs - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a lhs and @a rhs. - - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - - @since version 3.6.0 - */ + /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/ friend json_pointer operator/(const json_pointer& lhs, const json_pointer& rhs) { return json_pointer(lhs) /= rhs; } - /*! - @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer - - @param[in] ptr JSON pointer - @param[in] token reference token - @return a new JSON pointer with unescaped @a token appended to @a ptr - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a ptr. - - @sa @ref operator/=(std::string) to append a reference token - - @since version 3.6.0 - */ - friend json_pointer operator/(const json_pointer& ptr, std::string token) + /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/ + friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param) { - return json_pointer(ptr) /= std::move(token); + return json_pointer(lhs) /= std::move(token); } - /*! - @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer - - @param[in] ptr JSON pointer - @param[in] array_index array index - @return a new JSON pointer with @a array_index appended to @a ptr - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a ptr. - - @sa @ref operator/=(std::size_t) to append an array index - - @since version 3.6.0 - */ - friend json_pointer operator/(const json_pointer& ptr, std::size_t array_index) + /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/ + friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx) { - return json_pointer(ptr) /= array_index; + return json_pointer(lhs) /= array_idx; } - /*! - @brief returns the parent of this JSON pointer - - @return parent of this JSON pointer; in case this JSON pointer is the root, - the root itself is returned - - @complexity Linear in the length of the JSON pointer. - - @liveexample{The example shows the result of `parent_pointer` for different - JSON Pointers.,json_pointer__parent_pointer} - - @since version 3.6.0 - */ + /// @brief returns the parent of this JSON pointer + /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/ json_pointer parent_pointer() const { if (empty()) @@ -230,90 +154,46 @@ class json_pointer return res; } - /*! - @brief remove last reference token - - @pre not `empty()` - - @liveexample{The example shows the usage of `pop_back`.,json_pointer__pop_back} - - @complexity Constant. - - @throw out_of_range.405 if JSON pointer has no parent - - @since version 3.6.0 - */ + /// @brief remove last reference token + /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/ void pop_back() { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr)); } reference_tokens.pop_back(); } - /*! - @brief return last reference token - - @pre not `empty()` - @return last reference token - - @liveexample{The example shows the usage of `back`.,json_pointer__back} - - @complexity Constant. - - @throw out_of_range.405 if JSON pointer has no parent - - @since version 3.6.0 - */ - const std::string& back() const + /// @brief return last reference token + /// @sa https://json.nlohmann.me/api/json_pointer/back/ + const string_t& back() const { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr)); } return reference_tokens.back(); } - /*! - @brief append an unescaped token at the end of the reference pointer - - @param[in] token token to add - - @complexity Amortized constant. - - @liveexample{The example shows the result of `push_back` for different - JSON Pointers.,json_pointer__push_back} - - @since version 3.6.0 - */ - void push_back(const std::string& token) + /// @brief append an unescaped token at the end of the reference pointer + /// @sa https://json.nlohmann.me/api/json_pointer/push_back/ + void push_back(const string_t& token) { reference_tokens.push_back(token); } - /// @copydoc push_back(const std::string&) - void push_back(std::string&& token) + /// @brief append an unescaped token at the end of the reference pointer + /// @sa https://json.nlohmann.me/api/json_pointer/push_back/ + void push_back(string_t&& token) { reference_tokens.push_back(std::move(token)); } - /*! - @brief return whether pointer points to the root document - - @return true iff the JSON pointer points to the root document - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example shows the result of `empty` for different JSON - Pointers.,json_pointer__empty} - - @since version 3.6.0 - */ + /// @brief return whether pointer points to the root document + /// @sa https://json.nlohmann.me/api/json_pointer/empty/ bool empty() const noexcept { return reference_tokens.empty(); @@ -325,27 +205,55 @@ class json_pointer @return integer representation of @a s + @throw parse_error.106 if an array index begins with '0' + @throw parse_error.109 if an array index begins not with a digit @throw out_of_range.404 if string @a s could not be converted to an integer + @throw out_of_range.410 if an array index exceeds size_type */ - static int array_index(const std::string& s) + template + static typename BasicJsonType::size_type array_index(const string_t& s) { - std::size_t processed_chars = 0; - const int res = std::stoi(s, &processed_chars); + using size_type = typename BasicJsonType::size_type; - // check if the string was completely read - if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr)); } - return res; + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) + { + JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr)); + } + + const char* p = s.c_str(); + char* p_end = nullptr; + errno = 0; // strtoull doesn't reset errno + unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int) + if (p == p_end // invalid input or empty string + || errno == ERANGE // out of range + || JSON_HEDLEY_UNLIKELY(static_cast(p_end - p) != s.size())) // incomplete read + { + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr)); + } + + // only triggered on special platforms (like 32bit), see also + // https://github.com/nlohmann/json/pull/2203 + if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) + { + JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE + } + + return static_cast(res); } + JSON_PRIVATE_UNLESS_TESTED: json_pointer top() const { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr)); } json_pointer result = *this; @@ -353,6 +261,7 @@ class json_pointer return result; } + private: /*! @brief create and return a reference to the pointed to value @@ -361,10 +270,10 @@ class json_pointer @throw parse_error.109 if array index is not a number @throw type_error.313 if value cannot be unflattened */ + template BasicJsonType& get_and_create(BasicJsonType& j) const { - using size_type = typename BasicJsonType::size_type; - auto result = &j; + auto* result = &j; // in case no reference tokens exist, return a reference to the JSON value // j which will be overwritten by a primitive value @@ -397,14 +306,7 @@ class json_pointer case detail::value_t::array: { // create an entry in the array - JSON_TRY - { - result = &result->operator[](static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + result = &result->operator[](array_index(reference_token)); break; } @@ -414,8 +316,15 @@ class json_pointer an error situation, because primitive values may only occur as single value; that is, with an empty list of reference tokens. */ + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j)); } } @@ -441,9 +350,9 @@ class json_pointer @throw parse_error.109 if an array index was not a number @throw out_of_range.404 if the JSON pointer can not be resolved */ + template BasicJsonType& get_unchecked(BasicJsonType* ptr) const { - using size_type = typename BasicJsonType::size_type; for (const auto& reference_token : reference_tokens) { // convert null values to arrays or objects before continuing @@ -458,7 +367,7 @@ class json_pointer }); // change value to array for numbers or "-" or to object otherwise - *ptr = (nums or reference_token == "-") + *ptr = (nums || reference_token == "-") ? detail::value_t::array : detail::value_t::object; } @@ -474,14 +383,6 @@ class json_pointer case detail::value_t::array: { - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) - { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + reference_token + - "' must not begin with '0'")); - } - if (reference_token == "-") { // explicitly treat "-" as index beyond the end @@ -490,21 +391,21 @@ class json_pointer else { // convert array index to number; unchecked access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[](array_index(reference_token)); } break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr)); } } @@ -517,9 +418,9 @@ class json_pointer @throw out_of_range.402 if the array index '-' is used @throw out_of_range.404 if the JSON pointer can not be resolved */ + template BasicJsonType& get_checked(BasicJsonType* ptr) const { - using size_type = typename BasicJsonType::size_type; for (const auto& reference_token : reference_tokens) { switch (ptr->type()) @@ -536,33 +437,26 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); - } - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) - { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + reference_token + - "' must not begin with '0'")); + JSON_THROW(detail::out_of_range::create(402, detail::concat( + "array index '-' (", std::to_string(ptr->m_value.array->size()), + ") is out of range"), ptr)); } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(array_index(reference_token)); break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr)); } } @@ -582,9 +476,9 @@ class json_pointer @throw out_of_range.402 if the array index '-' is used @throw out_of_range.404 if the JSON pointer can not be resolved */ + template const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const { - using size_type = typename BasicJsonType::size_type; for (const auto& reference_token : reference_tokens) { switch (ptr->type()) @@ -601,34 +495,24 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); - } - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) - { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + reference_token + - "' must not begin with '0'")); + JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_value.array->size()), ") is out of range"), ptr)); } // use unchecked array access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[](array_index(reference_token)); break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr)); } } @@ -641,9 +525,9 @@ class json_pointer @throw out_of_range.402 if the array index '-' is used @throw out_of_range.404 if the JSON pointer can not be resolved */ + template const BasicJsonType& get_checked(const BasicJsonType* ptr) const { - using size_type = typename BasicJsonType::size_type; for (const auto& reference_token : reference_tokens) { switch (ptr->type()) @@ -660,33 +544,26 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); - } - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) - { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + reference_token + - "' must not begin with '0'")); + JSON_THROW(detail::out_of_range::create(402, detail::concat( + "array index '-' (", std::to_string(ptr->m_value.array->size()), + ") is out of range"), ptr)); } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(array_index(reference_token)); break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr)); } } @@ -697,16 +574,16 @@ class json_pointer @throw parse_error.106 if an array index begins with '0' @throw parse_error.109 if an array index was not a number */ + template bool contains(const BasicJsonType* ptr) const { - using size_type = typename BasicJsonType::size_type; for (const auto& reference_token : reference_tokens) { switch (ptr->type()) { case detail::value_t::object: { - if (not ptr->contains(reference_token)) + if (!ptr->contains(reference_token)) { // we did not find the key in the object return false; @@ -723,34 +600,47 @@ class json_pointer // "-" always fails the range check return false; } - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0')) + if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9"))) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + reference_token + - "' must not begin with '0'")); + // invalid char + return false; } - - JSON_TRY + if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1)) { - const auto idx = static_cast(array_index(reference_token)); - if (idx >= ptr->size()) + if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9'))) { - // index out of range + // first char should be between '1' and '9' return false; } - - ptr = &ptr->operator[](idx); - break; + for (std::size_t i = 1; i < reference_token.size(); i++) + { + if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9'))) + { + // other char should be between '0' and '9' + return false; + } + } } - JSON_CATCH(std::invalid_argument&) + + const auto idx = array_index(reference_token); + if (idx >= ptr->size()) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); + // index out of range + return false; } + + ptr = &ptr->operator[](idx); break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: { // we do not expect primitive values if there is still a @@ -773,9 +663,9 @@ class json_pointer @throw parse_error.107 if the pointer is not empty or begins with '/' @throw parse_error.108 if character '~' is not followed by '0' or '1' */ - static std::vector split(const std::string& reference_string) + static std::vector split(const string_t& reference_string) { - std::vector result; + std::vector result; // special case: empty reference string -> no reference tokens if (reference_string.empty()) @@ -786,9 +676,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); } // extract the reference tokens: @@ -799,11 +687,11 @@ class json_pointer std::size_t slash = reference_string.find_first_of('/', 1), // set the beginning of the first reference token start = 1; - // we can stop if start == 0 (if slash == std::string::npos) + // we can stop if start == 0 (if slash == string_t::npos) start != 0; // set the beginning of the next reference token - // (will eventually be 0 if slash == std::string::npos) - start = (slash == std::string::npos) ? 0 : slash + 1, + // (will eventually be 0 if slash == string_t::npos) + start = (slash == string_t::npos) ? 0 : slash + 1, // find next slash slash = reference_string.find_first_of('/', start)) { @@ -813,67 +701,29 @@ class json_pointer // check reference tokens are properly escaped for (std::size_t pos = reference_token.find_first_of('~'); - pos != std::string::npos; + pos != string_t::npos; pos = reference_token.find_first_of('~', pos + 1)) { - assert(reference_token[pos] == '~'); + JSON_ASSERT(reference_token[pos] == '~'); // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 or - (reference_token[pos + 1] != '0' and + if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || + (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } } // finally, store the reference token - unescape(reference_token); + detail::unescape(reference_token); result.push_back(reference_token); } return result; } - /*! - @brief replace all occurrences of a substring by another string - - @param[in,out] s the string to manipulate; changed so that all - occurrences of @a f are replaced with @a t - @param[in] f the substring to replace with @a t - @param[in] t the string to replace @a f - - @pre The search string @a f must not be empty. **This precondition is - enforced with an assertion.** - - @since version 2.0.0 - */ - static void replace_substring(std::string& s, const std::string& f, - const std::string& t) - { - assert(not f.empty()); - for (auto pos = s.find(f); // find first occurrence of f - pos != std::string::npos; // make sure f was found - s.replace(pos, f.size(), t), // replace with t, and - pos = s.find(f, pos + t.size())) // find next occurrence of f - {} - } - - /// escape "~" to "~0" and "/" to "~1" - static std::string escape(std::string s) - { - replace_substring(s, "~", "~0"); - replace_substring(s, "/", "~1"); - return s; - } - - /// unescape "~1" to tilde and "~0" to slash (order is important!) - static void unescape(std::string& s) - { - replace_substring(s, "~1", "/"); - replace_substring(s, "~0", "~"); - } - + private: /*! @param[in] reference_string the reference string to the current value @param[in] value the value to consider @@ -881,7 +731,8 @@ class json_pointer @note Empty objects or arrays are flattened to `null`. */ - static void flatten(const std::string& reference_string, + template + static void flatten(const string_t& reference_string, const BasicJsonType& value, BasicJsonType& result) { @@ -899,7 +750,7 @@ class json_pointer // iterate array and use index as reference string for (std::size_t i = 0; i < value.m_value.array->size(); ++i) { - flatten(reference_string + "/" + std::to_string(i), + flatten(detail::concat(reference_string, '/', std::to_string(i)), value.m_value.array->operator[](i), result); } } @@ -918,12 +769,20 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_value.object) { - flatten(reference_string + "/" + escape(element.first), element.second, result); + flatten(detail::concat(reference_string, '/', detail::escape(element.first)), element.second, result); } } break; } + case detail::value_t::null: + case detail::value_t::string: + case detail::value_t::boolean: + case detail::value_t::number_integer: + case detail::value_t::number_unsigned: + case detail::value_t::number_float: + case detail::value_t::binary: + case detail::value_t::discarded: default: { // add primitive value with its reference string @@ -943,12 +802,13 @@ class json_pointer @throw type_error.315 if object values are not primitive @throw type_error.313 if value cannot be unflattened */ + template static BasicJsonType unflatten(const BasicJsonType& value) { - if (JSON_HEDLEY_UNLIKELY(not value.is_object())) + if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value)); } BasicJsonType result; @@ -956,9 +816,9 @@ class json_pointer // iterate the JSON object values for (const auto& element : *value.m_value.object) { - if (JSON_HEDLEY_UNLIKELY(not element.second.is_primitive())) + if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second)); } // assign value to reference pointed to by JSON pointer; Note that if @@ -971,41 +831,158 @@ class json_pointer return result; } - /*! - @brief compares two JSON pointers for equality - - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is equal to @a rhs - - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ - friend bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept + // can't use conversion operator because of ambiguity + json_pointer convert() const& { - return lhs.reference_tokens == rhs.reference_tokens; + json_pointer result; + result.reference_tokens = reference_tokens; + return result; } - /*! - @brief compares two JSON pointers for inequality + json_pointer convert()&& + { + json_pointer result; + result.reference_tokens = std::move(reference_tokens); + return result; + } - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is not equal @a rhs + public: +#if JSON_HAS_THREE_WAY_COMPARISON + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + bool operator==(const json_pointer& rhs) const noexcept + { + return reference_tokens == rhs.reference_tokens; + } - @complexity Linear in the length of the JSON pointer + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer)) + bool operator==(const string_t& rhs) const + { + return *this == json_pointer(rhs); + } - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ - friend bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept + /// @brief 3-way compares two JSON pointers + template + std::strong_ordering operator<=>(const json_pointer& rhs) const noexcept // *NOPAD* { - return not (lhs == rhs); + return reference_tokens <=> rhs.reference_tokens; // *NOPAD* } +#else + /// @brief compares two JSON pointers for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept; + + /// @brief compares JSON pointer and string for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const json_pointer& lhs, + const StringType& rhs); + + /// @brief compares string and JSON pointer for equality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator==(const StringType& lhs, + const json_pointer& rhs); + + /// @brief compares two JSON pointers for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept; + + /// @brief compares JSON pointer and string for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const json_pointer& lhs, + const StringType& rhs); + + /// @brief compares string and JSON pointer for inequality + /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/ + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator!=(const StringType& lhs, + const json_pointer& rhs); + + /// @brief compares two JSON pointer for less-than + template + // NOLINTNEXTLINE(readability-redundant-declaration) + friend bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept; +#endif + private: /// the reference tokens - std::vector reference_tokens; + std::vector reference_tokens; }; -} // namespace nlohmann + +#if !JSON_HAS_THREE_WAY_COMPARISON +// functions cannot be defined inside class due to ODR violations +template +inline bool operator==(const json_pointer& lhs, + const json_pointer& rhs) noexcept +{ + return lhs.reference_tokens == rhs.reference_tokens; +} + +template::string_t> +JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer)) +inline bool operator==(const json_pointer& lhs, + const StringType& rhs) +{ + return lhs == json_pointer(rhs); +} + +template::string_t> +JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer)) +inline bool operator==(const StringType& lhs, + const json_pointer& rhs) +{ + return json_pointer(lhs) == rhs; +} + +template +inline bool operator!=(const json_pointer& lhs, + const json_pointer& rhs) noexcept +{ + return !(lhs == rhs); +} + +template::string_t> +JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer)) +inline bool operator!=(const json_pointer& lhs, + const StringType& rhs) +{ + return !(lhs == rhs); +} + +template::string_t> +JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer)) +inline bool operator!=(const StringType& lhs, + const json_pointer& rhs) +{ + return !(lhs == rhs); +} + +template +inline bool operator<(const json_pointer& lhs, + const json_pointer& rhs) noexcept +{ + return lhs.reference_tokens < rhs.reference_tokens; +} +#endif + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/json_ref.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/json_ref.hpp index c8dec733..47911fb5 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/json_ref.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/json_ref.hpp @@ -1,14 +1,23 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include #include +#include #include -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + template class json_ref { @@ -16,26 +25,26 @@ class json_ref using value_type = BasicJsonType; json_ref(value_type&& value) - : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true) + : owned_value(std::move(value)) {} json_ref(const value_type& value) - : value_ref(const_cast(&value)), is_rvalue(false) + : value_ref(&value) {} json_ref(std::initializer_list init) - : owned_value(init), value_ref(&owned_value), is_rvalue(true) + : owned_value(init) {} template < class... Args, enable_if_t::value, int> = 0 > json_ref(Args && ... args) - : owned_value(std::forward(args)...), value_ref(&owned_value), - is_rvalue(true) {} + : owned_value(std::forward(args)...) + {} // class should be movable only - json_ref(json_ref&&) = default; + json_ref(json_ref&&) noexcept = default; json_ref(const json_ref&) = delete; json_ref& operator=(const json_ref&) = delete; json_ref& operator=(json_ref&&) = delete; @@ -43,27 +52,27 @@ class json_ref value_type moved_or_copied() const { - if (is_rvalue) + if (value_ref == nullptr) { - return std::move(*value_ref); + return std::move(owned_value); } return *value_ref; } value_type const& operator*() const { - return *static_cast(value_ref); + return value_ref ? *value_ref : owned_value; } value_type const* operator->() const { - return static_cast(value_ref); + return &** this; } private: mutable value_type owned_value = nullptr; - value_type* value_ref = nullptr; - const bool is_rvalue; + value_type const* value_ref = nullptr; }; + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_scope.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_scope.hpp index 27dddc6b..6248bea1 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_scope.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_scope.hpp @@ -1,11 +1,22 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once -#include // pair +#include // declval, pair +#include #include -// This file contains all internal macro definitions +// This file contains all internal macro definitions (except those affecting ABI) // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them +#include + // exclude unsupported compilers #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) #if defined(__clang__) @@ -20,26 +31,128 @@ #endif // C++ language standard detection -#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 -#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define JSON_HAS_CPP_14 +// if the user manually specified the used c++ version this is skipped +#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) + #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) + #define JSON_HAS_CPP_20 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 + #endif + // the cpp 11 flag is always specified because it is the minimal required version + #define JSON_HAS_CPP_11 +#endif + +#ifdef __has_include + #if __has_include() + #include + #endif +#endif + +#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM) + #ifdef JSON_HAS_CPP_17 + #if defined(__cpp_lib_filesystem) + #define JSON_HAS_FILESYSTEM 1 + #elif defined(__cpp_lib_experimental_filesystem) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif !defined(__has_include) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #endif + + // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/ + #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__clang_major__) && __clang_major__ < 7 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support + #if defined(_MSC_VER) && _MSC_VER < 1914 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before iOS 13 + #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before macOS Catalina + #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + #endif +#endif + +#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_FILESYSTEM + #define JSON_HAS_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_THREE_WAY_COMPARISON + #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \ + && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L + #define JSON_HAS_THREE_WAY_COMPARISON 1 + #else + #define JSON_HAS_THREE_WAY_COMPARISON 0 + #endif +#endif + +#ifndef JSON_HAS_RANGES + // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error + #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427 + #define JSON_HAS_RANGES 0 + #elif defined(__cpp_lib_ranges) + #define JSON_HAS_RANGES 1 + #else + #define JSON_HAS_RANGES 0 + #endif #endif -// disable float-equal warnings on GCC/clang -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wfloat-equal" +#ifdef JSON_HAS_CPP_17 + #define JSON_INLINE_VARIABLE inline +#else + #define JSON_INLINE_VARIABLE +#endif + +#if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address) + #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else + #define JSON_NO_UNIQUE_ADDRESS #endif // disable documentation warnings on clang #if defined(__clang__) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wdocumentation" + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdocumentation" + #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" #endif -// allow to disable exceptions +// allow disabling exceptions #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try @@ -73,6 +186,19 @@ #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER #endif +// allow overriding assert +#if !defined(JSON_ASSERT) + #include // assert + #define JSON_ASSERT(x) assert(x) +#endif + +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + /*! @brief macro to briefly define a mapping between an enum and JSON @def NLOHMANN_JSON_SERIALIZE_ENUM @@ -113,9 +239,230 @@ class StringType, class BooleanType, class NumberIntegerType, \ class NumberUnsignedType, class NumberFloatType, \ template class AllocatorType, \ - template class JSONSerializer> + template class JSONSerializer, \ + class BinaryType> #define NLOHMANN_BASIC_JSON_TPL \ basic_json + AllocatorType, JSONSerializer, BinaryType> + +// Macros to simplify conversion from/to types + +#define NLOHMANN_JSON_EXPAND( x ) x +#define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME +#define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \ + NLOHMANN_JSON_PASTE64, \ + NLOHMANN_JSON_PASTE63, \ + NLOHMANN_JSON_PASTE62, \ + NLOHMANN_JSON_PASTE61, \ + NLOHMANN_JSON_PASTE60, \ + NLOHMANN_JSON_PASTE59, \ + NLOHMANN_JSON_PASTE58, \ + NLOHMANN_JSON_PASTE57, \ + NLOHMANN_JSON_PASTE56, \ + NLOHMANN_JSON_PASTE55, \ + NLOHMANN_JSON_PASTE54, \ + NLOHMANN_JSON_PASTE53, \ + NLOHMANN_JSON_PASTE52, \ + NLOHMANN_JSON_PASTE51, \ + NLOHMANN_JSON_PASTE50, \ + NLOHMANN_JSON_PASTE49, \ + NLOHMANN_JSON_PASTE48, \ + NLOHMANN_JSON_PASTE47, \ + NLOHMANN_JSON_PASTE46, \ + NLOHMANN_JSON_PASTE45, \ + NLOHMANN_JSON_PASTE44, \ + NLOHMANN_JSON_PASTE43, \ + NLOHMANN_JSON_PASTE42, \ + NLOHMANN_JSON_PASTE41, \ + NLOHMANN_JSON_PASTE40, \ + NLOHMANN_JSON_PASTE39, \ + NLOHMANN_JSON_PASTE38, \ + NLOHMANN_JSON_PASTE37, \ + NLOHMANN_JSON_PASTE36, \ + NLOHMANN_JSON_PASTE35, \ + NLOHMANN_JSON_PASTE34, \ + NLOHMANN_JSON_PASTE33, \ + NLOHMANN_JSON_PASTE32, \ + NLOHMANN_JSON_PASTE31, \ + NLOHMANN_JSON_PASTE30, \ + NLOHMANN_JSON_PASTE29, \ + NLOHMANN_JSON_PASTE28, \ + NLOHMANN_JSON_PASTE27, \ + NLOHMANN_JSON_PASTE26, \ + NLOHMANN_JSON_PASTE25, \ + NLOHMANN_JSON_PASTE24, \ + NLOHMANN_JSON_PASTE23, \ + NLOHMANN_JSON_PASTE22, \ + NLOHMANN_JSON_PASTE21, \ + NLOHMANN_JSON_PASTE20, \ + NLOHMANN_JSON_PASTE19, \ + NLOHMANN_JSON_PASTE18, \ + NLOHMANN_JSON_PASTE17, \ + NLOHMANN_JSON_PASTE16, \ + NLOHMANN_JSON_PASTE15, \ + NLOHMANN_JSON_PASTE14, \ + NLOHMANN_JSON_PASTE13, \ + NLOHMANN_JSON_PASTE12, \ + NLOHMANN_JSON_PASTE11, \ + NLOHMANN_JSON_PASTE10, \ + NLOHMANN_JSON_PASTE9, \ + NLOHMANN_JSON_PASTE8, \ + NLOHMANN_JSON_PASTE7, \ + NLOHMANN_JSON_PASTE6, \ + NLOHMANN_JSON_PASTE5, \ + NLOHMANN_JSON_PASTE4, \ + NLOHMANN_JSON_PASTE3, \ + NLOHMANN_JSON_PASTE2, \ + NLOHMANN_JSON_PASTE1)(__VA_ARGS__)) +#define NLOHMANN_JSON_PASTE2(func, v1) func(v1) +#define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2) +#define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3) +#define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4) +#define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5) +#define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6) +#define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7) +#define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8) +#define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9) +#define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10) +#define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) +#define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) +#define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) +#define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) +#define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) +#define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) +#define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) +#define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) +#define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) +#define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) +#define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) +#define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) +#define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) +#define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) +#define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) +#define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) +#define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) +#define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) +#define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) +#define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) +#define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) +#define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) +#define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) +#define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) +#define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) +#define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) +#define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) +#define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) +#define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) +#define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) +#define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) +#define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) +#define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) +#define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) +#define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) +#define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) +#define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) +#define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) +#define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) +#define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) +#define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) +#define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) +#define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) +#define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) +#define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) +#define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) +#define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) +#define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) +#define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) +#define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) +#define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) +#define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) +#define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) + +#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1; +#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); +#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + + +// inspired from https://stackoverflow.com/a/26745591 +// allows to call any std function as if (e.g. with begin): +// using std::begin; begin(x); +// +// it allows using the detected idiom to retrieve the return type +// of such an expression +#define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \ + namespace detail { \ + using std::std_name; \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + } \ + \ + namespace detail2 { \ + struct std_name##_tag \ + { \ + }; \ + \ + template \ + std_name##_tag std_name(T&&...); \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + \ + template \ + struct would_call_std_##std_name \ + { \ + static constexpr auto const value = ::nlohmann::detail:: \ + is_detected_exact::value; \ + }; \ + } /* namespace detail2 */ \ + \ + template \ + struct would_call_std_##std_name : detail2::would_call_std_##std_name \ + { \ + } + +#ifndef JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_USE_IMPLICIT_CONVERSIONS 1 +#endif + +#if JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_EXPLICIT +#else + #define JSON_EXPLICIT explicit +#endif + +#ifndef JSON_DISABLE_ENUM_SERIALIZATION + #define JSON_DISABLE_ENUM_SERIALIZATION 0 +#endif + +#ifndef JSON_USE_GLOBAL_UDLS + #define JSON_USE_GLOBAL_UDLS 1 +#endif diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_unscope.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_unscope.hpp index 80b293e7..4a871f0c 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_unscope.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/macro_unscope.hpp @@ -1,21 +1,44 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once -// restore GCC/clang diagnostic settings -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic pop -#endif +// restore clang diagnostic settings #if defined(__clang__) - #pragma GCC diagnostic pop + #pragma clang diagnostic pop #endif // clean up +#undef JSON_ASSERT #undef JSON_INTERNAL_CATCH -#undef JSON_CATCH #undef JSON_THROW -#undef JSON_TRY -#undef JSON_HAS_CPP_14 -#undef JSON_HAS_CPP_17 +#undef JSON_PRIVATE_UNLESS_TESTED #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL +#undef JSON_EXPLICIT +#undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL +#undef JSON_INLINE_VARIABLE +#undef JSON_NO_UNIQUE_ADDRESS +#undef JSON_DISABLE_ENUM_SERIALIZATION +#undef JSON_USE_GLOBAL_UDLS + +#ifndef JSON_TEST_KEEP_MACROS + #undef JSON_CATCH + #undef JSON_TRY + #undef JSON_HAS_CPP_11 + #undef JSON_HAS_CPP_14 + #undef JSON_HAS_CPP_17 + #undef JSON_HAS_CPP_20 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #undef JSON_HAS_THREE_WAY_COMPARISON + #undef JSON_HAS_RANGES + #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON +#endif #include diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/begin.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/begin.hpp new file mode 100644 index 00000000..27d36c66 --- /dev/null +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/begin.hpp @@ -0,0 +1,17 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin); + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/end.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/end.hpp new file mode 100644 index 00000000..d10bf833 --- /dev/null +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/call_std/end.hpp @@ -0,0 +1,17 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end); + +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/cpp_future.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/cpp_future.hpp index 948cd4fb..22f25140 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/cpp_future.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/cpp_future.hpp @@ -1,51 +1,150 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-FileCopyrightText: 2018 The Abseil Authors +// SPDX-License-Identifier: MIT + #pragma once -#include // not +#include // array #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include // index_sequence, make_index_sequence, index_sequence_for -namespace nlohmann -{ +#include + +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + +template +using uncvref_t = typename std::remove_cv::type>::type; + +#ifdef JSON_HAS_CPP_14 + +// the following utilities are natively available in C++14 +using std::enable_if_t; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +#else + // alias templates to reduce boilerplate template using enable_if_t = typename std::enable_if::type; -template -using uncvref_t = typename std::remove_cv::type>::type; +// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h +// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. -// implementation of C++14 index_sequence and affiliates -// source: https://stackoverflow.com/a/32223343 -template -struct index_sequence +//// START OF CODE FROM GOOGLE ABSEIL + +// integer_sequence +// +// Class template representing a compile-time integer sequence. An instantiation +// of `integer_sequence` has a sequence of integers encoded in its +// type through its template arguments (which is a common need when +// working with C++11 variadic templates). `absl::integer_sequence` is designed +// to be a drop-in replacement for C++14's `std::integer_sequence`. +// +// Example: +// +// template< class T, T... Ints > +// void user_function(integer_sequence); +// +// int main() +// { +// // user_function's `T` will be deduced to `int` and `Ints...` +// // will be deduced to `0, 1, 2, 3, 4`. +// user_function(make_integer_sequence()); +// } +template +struct integer_sequence { - using type = index_sequence; - using value_type = std::size_t; + using value_type = T; static constexpr std::size_t size() noexcept { return sizeof...(Ints); } }; -template -struct merge_and_renumber; +// index_sequence +// +// A helper template for an `integer_sequence` of `size_t`, +// `absl::index_sequence` is designed to be a drop-in replacement for C++14's +// `std::index_sequence`. +template +using index_sequence = integer_sequence; -template -struct merge_and_renumber, index_sequence> - : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; +namespace utility_internal +{ -template -struct make_index_sequence - : merge_and_renumber < typename make_index_sequence < N / 2 >::type, - typename make_index_sequence < N - N / 2 >::type > {}; +template +struct Extend; -template<> struct make_index_sequence<0> : index_sequence<> {}; -template<> struct make_index_sequence<1> : index_sequence<0> {}; +// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. +template +struct Extend, SeqSize, 0> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; +}; -template +template +struct Extend, SeqSize, 1> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; +}; + +// Recursion helper for 'make_integer_sequence'. +// 'Gen::type' is an alias for 'integer_sequence'. +template +struct Gen +{ + using type = + typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; +}; + +template +struct Gen +{ + using type = integer_sequence; +}; + +} // namespace utility_internal + +// Compile-time sequences of integers + +// make_integer_sequence +// +// This template alias is equivalent to +// `integer_sequence`, and is designed to be a drop-in +// replacement for C++14's `std::make_integer_sequence`. +template +using make_integer_sequence = typename utility_internal::Gen::type; + +// make_index_sequence +// +// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, +// and is designed to be a drop-in replacement for C++14's +// `std::make_index_sequence`. +template +using make_index_sequence = make_integer_sequence; + +// index_sequence_for +// +// Converts a typename pack into an index sequence of the same length, and +// is designed to be a drop-in replacement for C++14's +// `std::index_sequence_for()` +template using index_sequence_for = make_index_sequence; +//// END OF CODE FROM GOOGLE ABSEIL + +#endif + // dispatch utility (taken from ranges-v3) template struct priority_tag : priority_tag < N - 1 > {}; template<> struct priority_tag<0> {}; @@ -54,10 +153,19 @@ template<> struct priority_tag<0> {}; template struct static_const { - static constexpr T value{}; + static JSON_INLINE_VARIABLE constexpr T value{}; }; -template -constexpr T static_const::value; +#ifndef JSON_HAS_CPP_17 + template + constexpr T static_const::value; +#endif + +template +inline constexpr std::array make_array(Args&& ... args) +{ + return std::array {{static_cast(std::forward(args))...}}; +} + } // namespace detail -} // namespace nlohmann +NLOHMANN_JSON_NAMESPACE_END diff --git a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/detected.hpp b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/detected.hpp index 5b52460a..b2f6db9f 100644 --- a/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/detected.hpp +++ b/aat/cpp/third/nlohmann_json/nlohmann/detail/meta/detected.hpp @@ -1,14 +1,22 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.2 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann +// SPDX-License-Identifier: MIT + #pragma once #include #include -// http://en.cppreference.com/w/cpp/experimental/is_detected -namespace nlohmann -{ +NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { + +// https://en.cppreference.com/w/cpp/experimental/is_detected struct nonesuch { nonesuch() = delete; @@ -19,40 +27,44 @@ struct nonesuch void operator=(nonesuch&&) = delete; }; -template class Op, - class... Args> +template class Op, + class... Args> struct detector { using value_t = std::false_type; using type = Default; }; -template class Op, class... Args> +template class Op, class... Args> struct detector>, Op, Args...> { using value_t = std::true_type; using type = Op; }; -template